adafruit / RTClib

A fork of Jeelab's fantastic RTC Arduino library
MIT License
798 stars 709 forks source link

Problems with the sample program DS3231_alarm.ino #260

Closed Jasea closed 2 years ago

Jasea commented 2 years ago

Thank you for opening an issue on an Adafruit Arduino library repository. To improve the speed of resolution please review the following guidelines and common troubleshooting steps below before creating the issue:

If you're sure this issue is a defect in the code and checked the steps above please fill in the following fields to provide enough troubleshooting information. You may delete the guideline and text above to just leave the following details:

load 0x4010f000, len 3460, room 16 tail 4 chksum 0xcc load 0x3fff20b8, len 40, room 4 tail 4 chksum 0xc9 csum 0xc9 v00043cc0 ~ld

This problem was resolved by adding this predefinition for the ISR void ICACHE_RAM_ATTR onAlarm();

  1. It looks like the code at the bottom of the loop() is not working as expected if(rtc.alarmFired(1)) { rtc.clearAlarm(1); Serial.println("\nAlarm cleared"); } I think that the call to rtc.clearAlarm(1) should remove the alarm completley fron the DS3231, but that is not happening see the following Serial Monitor output.

Alarm will happen in 10 seconds! Alarm is set

16:08:25 SQW: 1 Alarm1: 0 16:08:27 SQW: 1 Alarm1: 0 16:08:29 SQW: 1 Alarm1: 0 16:08:31 SQW: 1 Alarm1: 0 16:08:33 SQW: 1 Alarm1: 0 Alarm occurred!

16:08:35 SQW: 0 Alarm1: 1 Alarm cleared Alarm should have been cleared

16:08:37 SQW: 1 Alarm1: 0 16:08:39 SQW: 1 Alarm1: 0 16:08:41 SQW: 1 Alarm1: 0 16:08:43 SQW: 1 Alarm1: 0 16:08:45 SQW: 1 Alarm1: 0 16:08:47 SQW: 1 Alarm1: 0 16:08:49 SQW: 1 Alarm1: 0 16:08:51 SQW: 1 Alarm1: 0 16:08:53 SQW: 1 Alarm1: 0 16:08:55 SQW: 1 Alarm1: 0 16:08:57 SQW: 1 Alarm1: 0 16:08:59 SQW: 1 Alarm1: 0 16:09:01 SQW: 1 Alarm1: 0 16:09:03 SQW: 1 Alarm1: 0 16:09:05 SQW: 1 Alarm1: 0 16:09:07 SQW: 1 Alarm1: 0 16:09:09 SQW: 1 Alarm1: 0 16:09:11 SQW: 1 Alarm1: 0 16:09:13 SQW: 1 Alarm1: 0 16:09:15 SQW: 1 Alarm1: 0 16:09:17 SQW: 1 Alarm1: 0 16:09:19 SQW: 1 Alarm1: 0 16:09:21 SQW: 1 Alarm1: 0 16:09:23 SQW: 1 Alarm1: 0 16:09:25 SQW: 1 Alarm1: 0 16:09:27 SQW: 1 Alarm1: 0 16:09:29 SQW: 1 Alarm1: 0 16:09:31 SQW: 1 Alarm1: 0 16:09:33 SQW: 1 Alarm1: 0 Alarm occured! BUT Alarm occurres again and at every 33 seconds past every minute.

16:09:35 SQW: 0 Alarm1: 1 Alarm cleared

16:09:37 SQW: 1 Alarm1: 0 16:09:39 SQW: 1 Alarm1: 0 I have found that if I set another alarm after the first has been cleared then it is the time of the last alarm that triggers the unwanted alarms.

I look forward to hearing your thoughts

DS3231_alarm.txt

caternuson commented 2 years ago

I think that the call to rtc.clearAlarm(1) should remove the alarm completley fron the DS3231, but that is not happening see the following Serial Monitor output.

clearAlarm() only clears the alarm status: https://adafruit.github.io/RTClib/html/class_r_t_c___d_s3231.html#aa8050b94c8333d72b31fd223eeb50911 it does not remove (disable) the alarm itself. To do that, use disableAlaram(): https://adafruit.github.io/RTClib/html/class_r_t_c___d_s3231.html#a9f5191fed61fd8a4716572d43f300a5a

The example sets up the alarm to fire when seconds match:

    // schedule an alarm 10 seconds in the future
    if(!rtc.setAlarm1(
            rtc.now() + TimeSpan(10),
            DS3231_A1_Second // this mode triggers the alarm when the seconds match. See Doxygen for other options
    ))

So if the status is cleared, but the alarm is not disable, then it will fire again later when the seconds match again.