Hey everybody,
I got trouble trying to wake up my Arduino nano every minutes. I'm using a DS3231, with the Korneliusz Jarzebski library (GitHub - jarzebski/Arduino-DS3231: DS3231 Real-Time-Clock)
#include <Wire.h>
#include <DS3231.h>
#include <avr/sleep.h>
DS3231 clock;
RTCDateTime dt;
const byte wakePin = 2; // pin used for waking up (interupt 0)
void setup()
{
Serial.begin(9600);
// Initialize DS3231
clock.begin();
clock.armAlarm1(false);
clock.armAlarm2(false);
clock.clearAlarm1();
clock.clearAlarm2();
pinMode(wakePin, INPUT);
byte temp_buffer = 0b11110111; // to disable SQW pin of DS3231, not logical yes, but without that the Arduino doesn't go to sleep mode even the first time
writeControlByte(temp_buffer, 0);
clock.setDateTime(2014, 4, 25, 0, 0, 55);
clock.setAlarm1(0, 0, 0, 0, DS3231_MATCH_S);
attachInterrupt(0, wakeUpNow, FALLING);
delay(100);
checkAlarms();
}
void wakeUpNow()
{
sleep_disable();
detachInterrupt(0);
delay(100);
}
void sleepNow()
{
sleep_enable();
attachInterrupt(0, wakeUpNow, LOW);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
cli();
sleep_bod_disable();
sei();
sleep_cpu();
/* wake up here */
sleep_disable();
}
void loop()
{
char print_date[16];
Serial.println("go to sleep...");
delay(100);
sleepNow();
dt = clock.getDateTime();
sprintf(print_date, "%02d/%02d/%d %02d:%02d %02d", dt.day, dt.month, dt.year, dt.hour, dt.minute, dt.second);
Serial.println(print_date);
}
void writeControlByte(byte control, bool which) { // Set DS3121 RTC control bytes
// Write the selected control byte.
// which=false -> 0x0e, true->0x0f.
Wire.beginTransmission(0x68);
if (which) {
Wire.write(0x0f);
} else {
Wire.write(0x0e);
}
Wire.write(control);
Wire.endTransmission();
}
void checkAlarms()
{
RTCAlarmTime a1;
RTCAlarmTime a2;
if (clock.isArmed1())
{
a1 = clock.getAlarm1();
Serial.print("Alarm1 is triggered ");
switch (clock.getAlarmType1())
{
case DS3231_EVERY_SECOND:
Serial.println("every second");
break;
case DS3231_MATCH_S:
Serial.print("when seconds match: ");
Serial.println(clock.dateFormat("__ __:__:s", a1));
break;
case DS3231_MATCH_M_S:
Serial.print("when minutes and sencods match: ");
Serial.println(clock.dateFormat("__ __:i:s", a1));
break;
case DS3231_MATCH_H_M_S:
Serial.print("when hours, minutes and seconds match: ");
Serial.println(clock.dateFormat("__ H:i:s", a1));
break;
case DS3231_MATCH_DT_H_M_S:
Serial.print("when date, hours, minutes and seconds match: ");
Serial.println(clock.dateFormat("d H:i:s", a1));
break;
case DS3231_MATCH_DY_H_M_S:
Serial.print("when day of week, hours, minutes and seconds match: ");
Serial.println(clock.dateFormat("l H:i:s", a1));
break;
default:
Serial.println("UNKNOWN RULE");
break;
}
} else
{
Serial.println("Alarm1 is disarmed.");
}
if (clock.isArmed2())
{
a2 = clock.getAlarm2();
Serial.print("Alarm2 is triggered ");
switch (clock.getAlarmType2())
{
case DS3231_EVERY_MINUTE:
Serial.println("every minute");
break;
case DS3231_MATCH_M:
Serial.print("when minutes match: ");
Serial.println(clock.dateFormat("__ __:i:s", a2));
break;
case DS3231_MATCH_H_M:
Serial.print("when hours and minutes match:");
Serial.println(clock.dateFormat("__ H:i:s", a2));
break;
case DS3231_MATCH_DT_H_M:
Serial.print("when date, hours and minutes match: ");
Serial.println(clock.dateFormat("d H:i:s", a2));
break;
case DS3231_MATCH_DY_H_M:
Serial.println("when day of week, hours and minutes match: ");
Serial.print(clock.dateFormat("l H:i:s", a2));
break;
default:
Serial.println("UNKNOWN RULE");
break;
}
} else
{
Serial.println("Alarm2 is disarmed.");
}
}
Already 2 days I'm trying different things and looking on forums for similar examples, but I didn't found anything working...
What I expect from this example is to have the date displayed once every minute, and the Arduino sleeping meantime. What I obtain with this code is that the Arduino is going to sleep directly, wakeup after 5 seconds (it's ok as I set the clock at 00:00:55, and the alarm is every minute). But after the Arduino do not come back to sleeping mode.
I also don't understand why without disactivating first the SQW, the Arduino even don't go in the first sleeping mode.
I'm out of ideas now. So any clue or advice are welcome!