Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date and time functions using a DS3231 RTC connected via I2C and Wire lib
- #include "RTClib.h"
- RTC_DS3231 rtc;
- byte Year;
- byte Month;
- byte Date;
- //byte DoW;
- byte Hour;
- byte Minute;
- byte Second;
- byte last_second;
- unsigned long timer;
- char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
- void setup ()
- {
- Serial.begin(115200);
- if (! rtc.begin())
- {
- Serial.println("Couldn't find RTC");
- Serial.flush();
- abort();
- }
- if (rtc.lostPower())
- {
- Serial.println("RTC lost power, let's set the time!");
- //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
- }
- rtc.setAlarm1 (DateTime (2020, 06, 23, 13, 01, 30), DS3231_A1_Day);
- }
- void GetDateStuff(byte& Year, byte& Month, byte& Day,
- byte& Hour, byte& Minute, byte& Second)
- {
- // Call this if you notice something coming in on
- // the serial port. The stuff coming in should be in
- // the order YYMMDDwHHMMSS, with an 'x' at the end.
- boolean GotString = false;
- char InChar;
- byte Temp1, Temp2;
- char InString[20];
- byte j = 0;
- while (!GotString)
- {
- if (Serial.available())
- {
- InChar = Serial.read();
- InString[j] = InChar;
- j += 1;
- if (InChar == 'x')
- {
- GotString = true;
- }
- }
- }
- Serial.println(InString);
- // Read Year first
- Temp1 = (byte)InString[0] - 48;
- Temp2 = (byte)InString[1] - 48;
- Year = Temp1 * 10 + Temp2;
- // now month
- Temp1 = (byte)InString[2] - 48;
- Temp2 = (byte)InString[3] - 48;
- Month = Temp1 * 10 + Temp2;
- // now date
- Temp1 = (byte)InString[4] - 48;
- Temp2 = (byte)InString[5] - 48;
- Day = Temp1 * 10 + Temp2;
- // now Day of Week
- //DoW = (byte)InString[6] - 48;
- // now Hour
- Temp1 = (byte)InString[6] - 48;
- Temp2 = (byte)InString[7] - 48;
- Hour = Temp1 * 10 + Temp2;
- // now Minute
- Temp1 = (byte)InString[8] - 48;
- Temp2 = (byte)InString[9] - 48;
- Minute = Temp1 * 10 + Temp2;
- // now Second
- Temp1 = (byte)InString[10] - 48;
- Temp2 = (byte)InString[11] - 48;
- Second = Temp1 * 10 + Temp2;
- }
- void Print_Time()
- {
- DateTime now = rtc.now();
- if (now.day() < 10) Serial.print('0');
- Serial.print(now.day());
- Serial.print('/');
- if (now.month() < 10) Serial.print('0');
- Serial.print(now.month());
- Serial.print('/');
- Serial.print(now.year());
- Serial.print(" (");
- Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
- Serial.print(") ");
- if (now.hour() < 10) Serial.print('0');
- Serial.print(now.hour());
- Serial.print(':');
- if (now.minute() < 10) Serial.print('0');
- Serial.print(now.minute());
- Serial.print(':');
- if (now.second() < 10) Serial.print('0');
- Serial.print(now.second());
- Serial.println();
- }
- void loop ()
- {
- if (Serial.available())
- {
- GetDateStuff(Year, Month, Date, Hour, Minute, Second);
- rtc.adjust(DateTime(Year, Month, Date, Hour, Minute, Second));
- Serial.flush();
- }
- if (rtc.alarmFired(1))
- {
- Serial.println("wake up!");
- rtc.clearAlarm(1);
- }
- DateTime now = rtc.now();
- if (now.second() != last_second)
- {
- last_second = now.second();
- Print_Time();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement