Your setup looks good. It sets the alarm timer and attaches the callback function that is called on timer expiration. So far so good.
Your loop is fine, we’ll come back to that.
When your program runs on the dash, setup() is run, then loop() is run repeatedly. Each time it completes, it starts again. When the alarm expires, it triggers an interrupt. That stops the processor where ever it is in your code, and alrm() is run. When alrm() is finished, the program goes back to where it was stopped and resumes.
The alarm will expire somewhere in the loop function, and then jump to alrm(). While in alrm, other interrupts are blocked from running (with some exceptions, but that’s not important for now). Once such interrupt that is blocked is the timer that pulses the LED that is set by pulseLED. So while snoozing in alrm, the LED is not going to pulse. You restart the alarm, which is good. But you shouldn’t ever call loop because loop will continue to run on its own. The general rule for interrupt handlers like alrm is to have it do its work quickly and return so nothing else is blocked for an extended period. You don’t need to re-attach the alarm function as that is already set. It doesn’t expired like the alarm itself.
Try this instead:
void setup() {
Serial.begin (9600);
Clock.setAlarmSecondsFromNow(5);
Clock.attachAlarmInterrupt(alrm);
Dash.pulseLED(100, 100);
}
void loop() {
Serial.println("loop begin");
Dash.snooze(2000);
}
void alrm() {
Serial.println("alarm expired 5s");
Clock.setAlarmSecondsFromNow(5);
}
While this code will work most of the time, you could have a situation where the alarm expires while “loop begin” is being printed. So every once in a while you might see something like:
loop begin
loop bealarm expired 5s
in
loop begin
The workaround for this is to have your alrm set a flag, and then print from your main loop. That way the loop and alrm aren’t both trying to use Serial at the same time
bool expired;
void setup() {
Serial.begin (9600);
expired = false;
Clock.attachAlarmInterrupt(alrm);
Clock.setAlarmSecondsFromNow(5);
Dash.pulseLED(100, 100);
}
void loop() {
Serial.print(".");
if(expired) {
expired = false;
Serial.println();
Serial.println("alarm expired 5s");
}
}
void alrm() {
expired = true;
Clock.setAlarmSecondsFromNow(5);
}
This will print a . every time through loop. But when the alarm expires, the flag is set, and the message is printed the next time through loop. Experiment and see what you can do.