Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include "DS3231.h"
- char weekDay[][4] = {"Min","Sen","Sel","Rab","Kam","Jum","Sab"};
- DS3231 RTC;
- boolean dataLengkap = false;
- static int dataBufferIndex = 0;
- char SendPayload[22] = "";
- char serialBuffer[22] = "";
- void setup(){
- Serial.begin(9600);
- Wire.begin();
- RTC.begin();}
- void loop(){
- //ketika data yang diterima sudah lengkap, maka
- if(dataLengkap){
- strcat(SendPayload,serialBuffer);
- String dtku=String(SendPayload);
- //pisahkan data berdasarkan tanda koma
- int satu = dtku.indexOf(',');
- int dua = dtku.indexOf(',',satu + 1);
- int tiga = dtku.indexOf(',',dua + 1);
- int empat = dtku.indexOf(',',tiga + 1);
- int lima = dtku.indexOf(',',empat + 1);
- int enam = dtku.indexOf(',',lima + 1);
- //namai data sesuai posisinya
- String tahun=dtku.substring(0,satu);
- String bulan=dtku.substring(satu+1,dua);
- String tanggal=dtku.substring(dua+1,tiga);
- String jam=dtku.substring(tiga+1,empat);
- String menit=dtku.substring(empat+1,lima);
- String detik=dtku.substring(lima+1,enam);
- String hari=dtku.substring(enam+1);
- //ubah data dari string menjadi integer
- int m = tahun.toInt();
- int n = bulan.toInt();
- int o = tanggal.toInt();
- int p = jam.toInt();
- int q = menit.toInt();
- int r = detik.toInt();
- int s = hari.toInt();
- //masukkan data ke dalam instruksi pengaturan RTC
- DateTime dt(m,n,o,p,q,r,s);
- RTC.adjust(dt);
- dataLengkap=false;
- SendPayload[0] = 0;
- dataBufferIndex = 0;}
- else{
- DateTime now = RTC.now();
- Serial.print(now.year(), DEC);
- Serial.print('/');
- Serial.print(now.month(), DEC);
- Serial.print('/');
- Serial.print(now.date(), DEC);
- Serial.print(' ');
- Serial.print(now.hour(), DEC);
- Serial.print(':');
- Serial.print(now.minute(), DEC);
- Serial.print(':');
- Serial.print(now.second(), DEC);
- Serial.print(' ');
- Serial.println(weekDay[now.dayOfWeek()]);
- delay(1000);}}
- void serialEvent() {
- //bagian ini untuk menerima data dari LabVIEW
- while (Serial.available() > 0 ) {
- char incomingByte = Serial.read();
- if(incomingByte=='\n'){
- serialBuffer[dataBufferIndex] = 0;
- dataLengkap = true;}
- else {
- serialBuffer[dataBufferIndex++] = incomingByte;
- serialBuffer[dataBufferIndex] = 0;}}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement