Evening,
I've spent the past week trying to figure this out, and have read virtually every post here and on StackOverflow on the topic - no luck. Not a single post seems to adequately address using the 2 alarms of the DS3231 to wake an Arduino at two discrete times of the day.
I have a DS3231 wired into a Pro Mini 3.3v 8MHz. I want to use the two alarms on the DS3231 to wake up the Arduino from sleep twice a day at two specified hour:minute times. The RTC is properly wired in, and it's interrupt pin is connected to D2. This is a model that uses a "supercapacitor" rather than a battery. I'll swap it out later for a battery, but for now the capacitor is working fine.
I started with an example from the RTClib library, but have not been able to get it to function for two consecutive alarms. The first alarm will run flawlessly, operate my code, and then go back to sleep. However, the RTC never wakes up the Pro Mini when the second alarm comes around. The RTC is keeping accurate time, so that is not the problem. Also, I can't simply have the Arduino ping the RTC for the time as it has to be in a low power sleep mode due to power consumption/battery limitations. I've tried finding a way to clear the alarm register, but it appears the RTClib doesn't have a specific function for this? They just bundle it into checkAlarm(), which is already being called, so the register's flag should be getting reset.
Pasting my code below. If you could please help me out, I'd appreciate it. Again, the goal is to get the second alarm to wake up the arduino as well. I'd like the unit to wake up at 10:00 and 14:00 every day (once during 10:00-11:00 and once during 14:00-15:00).
Thanks so much for the help!
EDIT: I ran a test where Alarm1 had already passed (so it does not try an interrupt) and where Alarm2 was set to go off a few minutes after compiling. It did not. It appears that the problem is related to either A) the checkIfAlarm(2) function not working properly, or B) Alarm2 in the unit is not triggering. The RTC does say that Alarm 2 is enabled. Thoughts?
Code below...
// Date, Time and Alarm functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <SPI.h> // not used here, but needed to prevent a RTClib compile error
#include <avr/sleep.h>
#include <RTClib.h>
RTC_DS3231 rtc;
int INTERRUPT_PIN = 2;
volatile int state = LOW;
volatile boolean flag = false;
void setup () {
pinMode(INTERRUPT_PIN, INPUT);
//pull up the interrupt pin
digitalWrite(INTERRUPT_PIN, HIGH);
Serial.begin(57600);
// Wire.begin();
rtc.begin(); // Includes a call to Wire.begin()
rtc.adjust(DateTime(__DATE__, __TIME__));
DateTime now = rtc.now();
rtc.setAlarm1Simple(10, 00);
rtc.setAlarm2Simple(14, 00);
rtc.turnOnAlarm(1);
rtc.turnOnAlarm(2);
if (rtc.checkAlarmEnabled(1)) {
Serial.println("Alarm 1 Enabled");
}
if (rtc.checkAlarmEnabled(2)) {
Serial.println("Alarm 2 Enabled");
}
attachInterrupt(0, logData, LOW);
}
void loop () {
DateTime now = rtc.now();
if (rtc.checkIfAlarm(1)) {
Serial.println("Alarm 1 Triggered");
awakeNow();
}
if (rtc.checkIfAlarm(2)) {
Serial.println("Alarm 2 Triggered");
awakeNow();
}
Serial.print("Time is: ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
Serial.println("Going to sleep now.");
delay(600);
sleepNow();
Serial.println("AWAKE");
}
void sleepNow() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
attachInterrupt(0,logData, FALLING);
sleep_mode();
//HERE AFTER WAKING UP
sleep_disable();
detachInterrupt(0);
}
void awakeNow() {
if (flag) {
Serial.println("I am awake now. Doing stuff.");
flag = false;
}
}
void logData() {
//do something quick, flip a flag, and handle in loop();
flag = true;
}