How do I prevent an alarm, which checks if the current time matches an alarm time in hours and minutes, from going more than once within one minute?
I am using the DS3231 RTC module and RTClib from adafruit. I want to check if the current time equals to the alarm time, then set the alarmFlag variable. I want to clear alarmFlag when a button on pin D5 is pressed.
The code below works fine as long as I don't press the button within about a minute from the start of the alarm. If I press the button within about a minute from the start of the alarm, the alarmFlag will be 0 for one loop() cycle, but then (almost immediately, I don't have much in loop()) will be back to 1. I want for the button to stop the alarm completely, so it fires again only on the next day.
uint8_t alarm[2] = {19, 30}; // hour (24), minute
// other code ...
void loop() {
currTime = rtc.now();
// check if current time matches alarm time
// if it matches, it matches for a whole minute, which is the problem here
if (!alarmFlag && alarm[0] == currTime.hour() && alarm[1] == currTime.minute()) {
alarmFlag = true;
}
// clear alarmFlag on button press (this is a shortened version, excuse the lack of debouncing/other stuff)
if (digitalRead(5) == HIGH) {
alarmFlag = false;
}
}
Notes:
- I can not use the RTC's built-in alarms because I need them elsewhere.
- I don't want to just add
currTime.seconds() == 0to the firstifstatement, because that would skip the alarm if theloop()cycle takes more than a second. - I know that the way I detect a button press in the example code is suboptimal, I wrote a better way to deal with this in my main code, but I don't want to clutter up this question.

ifstatement? Something like this:alarm[2] == currTime.seconds(). Oh, and maybe this too:uint8_t alarm[3] = {19, 30, 0};. – VE7JRO Jan 10 '22 at 03:28alarmFlaghave three states, 0 for false, 1 for alarming, 2 for muted. – Dave X Jan 10 '22 at 14:24