Hi everyone
I am currently using the Arduino Zero in a project which involves certain actions being performed at regular intervals. At the moment, I am using the RTCZero library to ensure that an event happens after certain lengths of time:
#define INTERVAL_SECS 5 // could be 5 seconds but could also be hours later
#include <RTCZero.h>
void setup() {
rtc.begin(); // initialize RTC
// Set the time
rtc.setTime(12, 45, 59);
// Set the date
rtc.setDate(03, 01, 2021);
// set the initial alarm alarm
rtc.setAlarmEpoch( rtc.getEpoch() + INTERVAL_SECS ); // set alarm by adding the interval to the epoch
rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS);
rtc.attachInterrupt(ISR); // ISR called when alarm rings
}
volatile bool alarmRinging = false;
void ISR()
{
rtc.detachInterrupt(); // temporarily detach interrupt so ISR not interrupted
alarmRinging = true; // set the flag so action taken in the loop (not the ISR)
rtc.setAlarmEpoch( rtc.getEpoch() + INTERVAL_SECS ); // reset alarm by adding the interval to the epoch. Arguably this line should be at the end of the conditional block in the loop.
rtc.enableAlarm(rtc.MATCH_YYMMDDHHMMSS);
rtc.attachInterrupt(ISR); //reattach interrupt
}
void loop() {
if (alarmRinging)
{
// do something
// then turn the flag off
alarmRinging = false;
}
}
This seems to work (although grateful for any apparent flaws).
What I would like the project to also do is schedule an alarm at a fixed time every day (e.g. 23:15). I understand that this can be done as follows:
rtc.setAlarmTime(23, 15, 00);
rtc.enableAlarm(rtc.MATCH_HHMMSS);
However, I think that this would override the first alarm (set out in the code above) as the RTC can only have one alarm set. From some testing, I believe that this is the case but would be grateful if someone could please confirm?
If so, does anyone have any suggestions as to how to overcome this limitation? One idea which came to me was to develop some type of scheduling class so that an object holds multiple alarms (expressed as times from the epoch) and then works out the next alarm time and calls rtc.setAlarmTime(). Once that alarm is called then the scheduling library removes that alarm and calls etc.setAlarmTime() for the next one.
Thanks
P.S. The actual code will be using rtc.standbyMode() but I have omitted from the above for simplicity