Advertisement
TheArduinoGuy

DS3231 Alarms

Jun 29th, 2020
1,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Date and time functions using a DS3231 RTC connected via I2C and Wire lib
  2. #include "RTClib.h"
  3.  
  4. RTC_DS3231 rtc;
  5.  
  6. byte Year;
  7. byte Month;
  8. byte Date;
  9. //byte DoW;
  10. byte Hour;
  11. byte Minute;
  12. byte Second;
  13. byte last_second;
  14.  
  15. unsigned long timer;
  16.  
  17. char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  18.  
  19. void setup ()
  20. {
  21.     Serial.begin(115200);
  22.  
  23.  
  24.     if (! rtc.begin())
  25.     {
  26.         Serial.println("Couldn't find RTC");
  27.         Serial.flush();
  28.         abort();
  29.     }
  30.  
  31.     if (rtc.lostPower())
  32.     {
  33.         Serial.println("RTC lost power, let's set the time!");
  34.         //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  35.  
  36.  
  37.     }
  38.     rtc.setAlarm1 (DateTime (2020, 06, 23, 13, 01, 30), DS3231_A1_Day);
  39. }
  40.  
  41. void GetDateStuff(byte& Year, byte& Month, byte& Day,
  42.                   byte& Hour, byte& Minute, byte& Second)
  43. {
  44.     // Call this if you notice something coming in on
  45.     // the serial port. The stuff coming in should be in
  46.     // the order YYMMDDwHHMMSS, with an 'x' at the end.
  47.     boolean GotString = false;
  48.     char InChar;
  49.     byte Temp1, Temp2;
  50.     char InString[20];
  51.  
  52.     byte j = 0;
  53.     while (!GotString)
  54.     {
  55.         if (Serial.available())
  56.         {
  57.             InChar = Serial.read();
  58.             InString[j] = InChar;
  59.             j += 1;
  60.             if (InChar == 'x')
  61.             {
  62.                 GotString = true;
  63.             }
  64.         }
  65.     }
  66.     Serial.println(InString);
  67.     // Read Year first
  68.     Temp1 = (byte)InString[0] - 48;
  69.     Temp2 = (byte)InString[1] - 48;
  70.     Year = Temp1 * 10 + Temp2;
  71.     // now month
  72.     Temp1 = (byte)InString[2] - 48;
  73.     Temp2 = (byte)InString[3] - 48;
  74.     Month = Temp1 * 10 + Temp2;
  75.     // now date
  76.     Temp1 = (byte)InString[4] - 48;
  77.     Temp2 = (byte)InString[5] - 48;
  78.     Day = Temp1 * 10 + Temp2;
  79.     // now Day of Week
  80.     //DoW = (byte)InString[6] - 48;
  81.     // now Hour
  82.     Temp1 = (byte)InString[6] - 48;
  83.     Temp2 = (byte)InString[7] - 48;
  84.     Hour = Temp1 * 10 + Temp2;
  85.     // now Minute
  86.     Temp1 = (byte)InString[8] - 48;
  87.     Temp2 = (byte)InString[9] - 48;
  88.     Minute = Temp1 * 10 + Temp2;
  89.     // now Second
  90.     Temp1 = (byte)InString[10] - 48;
  91.     Temp2 = (byte)InString[11] - 48;
  92.     Second = Temp1 * 10 + Temp2;
  93. }
  94.  
  95. void Print_Time()
  96. {
  97.     DateTime now = rtc.now();
  98.  
  99.     if (now.day() < 10) Serial.print('0');
  100.     Serial.print(now.day());
  101.     Serial.print('/');
  102.     if (now.month() < 10) Serial.print('0');
  103.     Serial.print(now.month());
  104.     Serial.print('/');
  105.     Serial.print(now.year());
  106.  
  107.     Serial.print(" (");
  108.     Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  109.     Serial.print(") ");
  110.     if (now.hour() < 10) Serial.print('0');
  111.     Serial.print(now.hour());
  112.     Serial.print(':');
  113.     if (now.minute() < 10) Serial.print('0');
  114.     Serial.print(now.minute());
  115.     Serial.print(':');
  116.     if (now.second() < 10) Serial.print('0');
  117.     Serial.print(now.second());
  118.     Serial.println();
  119. }
  120.  
  121. void loop ()
  122. {
  123.     if (Serial.available())
  124.     {
  125.         GetDateStuff(Year, Month, Date, Hour, Minute, Second);
  126.         rtc.adjust(DateTime(Year, Month, Date, Hour, Minute, Second));
  127.         Serial.flush();
  128.     }
  129.  
  130.  
  131.     if (rtc.alarmFired(1))
  132.     {
  133.         Serial.println("wake up!");
  134.         rtc.clearAlarm(1);
  135.     }
  136.  
  137.     DateTime now = rtc.now();
  138.     if (now.second() != last_second)
  139.     {
  140.         last_second = now.second();
  141.         Print_Time();
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement