Skip to content

Commit fafe9d9

Browse files
committed
update rolling RTC example to handle overflows
1 parent 0611142 commit fafe9d9

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

libraries/RTC/examples/Example5_Rolling_Alarms/Example5_Rolling_Alarms.ino

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Author: Adam Garbo and Nathan Seidle
2+
Author: rserranosmith, Adam Garbo, and Nathan Seidle
33
Created: June 3rdrd, 2020
44
55
This example demonstrates how to read and set rolling RTC alarms. Each time
@@ -47,7 +47,10 @@ void setup()
4747
6: Alarm match every minute (hundredths, seconds)
4848
7: Alarm match every second (hundredths)
4949
*/
50-
rtc.setAlarmMode(6); // Set the RTC alarm to match on minutes rollover
50+
rtc.setAlarmMode(1); // Set the RTC alarm interval to a year, not particularly important if you are rolling alarms
51+
// typical use case for this would be "I want an alarm every day at XX:YY:ZZ.WWW time" if this
52+
// was the case you would want to 4 (every day). since we are setting a new alarm when the previous
53+
// is triggered we would want this to just be larger than the largest period between alarms
5154
rtc.attachInterrupt(); // Attach RTC alarm interrupt
5255

5356
// Print the RTC's alarm date and time
@@ -65,14 +68,16 @@ void loop()
6568
// Clear alarm flag
6669
alarmFlag = false;
6770

71+
int secs = (rtc.seconds + alarmSeconds);
72+
int mins = (rtc.minute + alarmMinutes + (int)(secs/60));
73+
int hours = (rtc.hour + alarmHours + (int)(mins/60));
6874
// Set the RTC's rolling alarm
6975
rtc.setAlarm(0,
70-
(rtc.seconds + alarmSeconds) % 60,
71-
(rtc.minute + alarmMinutes) % 60,
72-
(rtc.hour + alarmHours) % 24,
76+
secs % 60,
77+
mins % 60,
78+
hours % 24,
7379
rtc.dayOfMonth,
7480
rtc.month);
75-
rtc.setAlarmMode(6);
7681

7782
// Print next RTC alarm date and time
7883
Serial.print("Next rolling alarm: "); printAlarm();

0 commit comments

Comments
 (0)