Advertisement
dianartanto

sinkronwaktudarilabview

Jun 22nd, 2015
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "DS3231.h"
  3. char weekDay[][4] = {"Min","Sen","Sel","Rab","Kam","Jum","Sab"};
  4. DS3231 RTC;
  5. boolean dataLengkap = false;  
  6. static int dataBufferIndex = 0;
  7. char SendPayload[22] = "";
  8. char serialBuffer[22] = "";
  9.  
  10. void setup(){
  11.   Serial.begin(9600);
  12.     Wire.begin();
  13.     RTC.begin();}
  14.  
  15. void loop(){
  16.   //ketika data yang diterima sudah lengkap, maka
  17.   if(dataLengkap){
  18.     strcat(SendPayload,serialBuffer);
  19.     String dtku=String(SendPayload);
  20.     //pisahkan data berdasarkan tanda koma
  21.     int satu = dtku.indexOf(',');
  22.     int dua = dtku.indexOf(',',satu + 1);
  23.     int tiga = dtku.indexOf(',',dua + 1);
  24.     int empat = dtku.indexOf(',',tiga + 1);
  25.     int lima = dtku.indexOf(',',empat + 1);
  26.     int enam = dtku.indexOf(',',lima + 1);
  27.     //namai data sesuai posisinya
  28.     String tahun=dtku.substring(0,satu);
  29.     String bulan=dtku.substring(satu+1,dua);
  30.     String tanggal=dtku.substring(dua+1,tiga);
  31.     String jam=dtku.substring(tiga+1,empat);
  32.     String menit=dtku.substring(empat+1,lima);
  33.     String detik=dtku.substring(lima+1,enam);
  34.     String hari=dtku.substring(enam+1);  
  35.     //ubah data dari string menjadi integer  
  36.     int m = tahun.toInt();
  37.     int n = bulan.toInt();
  38.     int o = tanggal.toInt();
  39.     int p = jam.toInt();
  40.     int q = menit.toInt();
  41.     int r = detik.toInt();
  42.     int s = hari.toInt();
  43.     //masukkan data ke dalam instruksi pengaturan RTC
  44.     DateTime dt(m,n,o,p,q,r,s);
  45.     RTC.adjust(dt);
  46.     dataLengkap=false;
  47.     SendPayload[0] = 0;
  48.     dataBufferIndex = 0;}
  49.     else{
  50.     DateTime now = RTC.now();
  51.     Serial.print(now.year(), DEC);
  52.     Serial.print('/');
  53.     Serial.print(now.month(), DEC);
  54.     Serial.print('/');
  55.     Serial.print(now.date(), DEC);
  56.     Serial.print(' ');
  57.     Serial.print(now.hour(), DEC);
  58.     Serial.print(':');
  59.     Serial.print(now.minute(), DEC);
  60.     Serial.print(':');
  61.     Serial.print(now.second(), DEC);
  62.     Serial.print(' ');
  63.     Serial.println(weekDay[now.dayOfWeek()]);
  64.     delay(1000);}}
  65.  
  66. void serialEvent() {
  67.   //bagian ini untuk menerima data dari LabVIEW
  68.   while (Serial.available() > 0 ) {
  69.       char incomingByte = Serial.read();    
  70.       if(incomingByte=='\n'){
  71.           serialBuffer[dataBufferIndex] = 0;
  72.           dataLengkap = true;}
  73.       else {
  74.           serialBuffer[dataBufferIndex++] = incomingByte;
  75.           serialBuffer[dataBufferIndex] = 0;}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement