I'm using the DS3231 library by NorthernWidget, but I'm having a problem being that the alarm flag is not really cleared after the alarm is fired. Here's the code that I'm testing:
My goal is to turn off the led after the alarm is fired, clear the alarm flag and turn off the alarm, but that doesn't happen here. So I tried monitoring both the INT pin(pin1) and alarm flag. My question is how to properly turn off the alarm?
Did you have a look at the AlarmPolling example that came with the library?
This is the contents of the loop() function:
void loop() {
// static variable to keep track of LED on/off state
static byte state = false;
// if alarm went of, do alarm stuff
// first call to checkIFAlarm does not clear alarm flag
if (myRTC.checkIfAlarm(1, false)) {
state = ~state;
digitalWrite(LED_BUILTIN, state);
// Clear alarm state
myRTC.checkIfAlarm(1, true);
}
// Loop delay to emulate other running code
delay(10);
}
Seems they use the checkIfAlarm() function to disable the alarm rather than turnOffAlarm().
@markd833 I uploaded the example, but what I want to do is to fire the alarm only once then turn it off completely when it is triggered, that's the reason I did this:
But doing this still triggers the alarm as if it was never turned off.
@blh64 I used this because I want to fire the alarm once per minute(or however I want to later in the code):
To properly turn off the alarm and clear the alarm flag in the DS3231 library, you can follow these steps:
Check if the alarm has fired by monitoring the alarm flag. You're already doing this with myRTC.checkIfAlarm(1).
If the alarm has fired, you should perform the following actions:
Turn off the alarm using myRTC.turnOffAlarm(1) to disable Alarm 1.
Clear the alarm flag by writing a 0 to the alarm flag bit. You can do this using the clearAlarm1() function in the DS3231 library.
Here's how you can modify your loop() function to achieve this:
void loop() {
static byte state = false;
Serial.print(digitalRead(pin1));
Serial.print(" ");
Serial.print(myRTC.checkIfAlarm(1, false));
Serial.println();
if (myRTC.checkIfAlarm(1)) {
state = ~state;
digitalWrite(LED_BUILTIN, state);
// Turn off the alarm
myRTC.turnOffAlarm(1);
// Clear the alarm flag
myRTC.clearAlarm1();
}
delay(1000);
}
By adding the myRTC.clearAlarm1() function, you will clear the alarm flag, and the alarm should work as expected. Make sure you've included the DS3231 library correctly in your Arduino IDE for this code to work.
@codingapacalyspe if you are leveraging chtaGPT or similar AI in creating this and your other recent responses, it would be polite to make a proper attribution/disclaimer/admission of having done.
So the alarms is never really turned off, and the one way to work around is to just ignore the next alarm or just use it that way. Or I could poll the the INT pin, disable the interrupt on alarm.