Hello,
I tried to find the solution to this problem but I haven't made any progress. So, if someone could give me a hand that would be great. Here, for energy-saving purposes (device on batteries) I have to put my assembly in deep_sleep. The wake-up is done with an RTC alert. Everything works great. The problem is that when the Arduino is working, I would like to use the watchdog in case there is a blockage somewhere. So, the watchdog also works very well when I don't use the deep_sleep function. I specify that I don't use the watchdog during deep_sleep (so I disable it) but just after waking up (the sleep function is disabled then). When the watchdog is triggered, it often (but not always) hangs the Arduino... I made this little program that shows it. I work with the MEGA 2560 pro. The RTC is connected via an I2C multiplexer.
#include <RTClib.h>
#include <Wire.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/power.h>
RTC_DS3231 rtc;
// the pin that is connected to SQW
#define CLOCK_INTERRUPT_PIN 2
void setup() {
wdt_disable();
sleep_disable();
delay(100);
Serial.begin(38400);
Serial1.begin(38400);
Serial2.begin(9600);
Serial3.begin(9600);
pinMode(13, OUTPUT);// led "L"
digitalWrite(13, LOW);
digitalWrite(31, HIGH);//Resert i2c
pinMode(31, OUTPUT);
Wire.begin();
//I21C multiplexer to RTC
Wire.beginTransmission(0x70);
Wire.write(1 << 0); // send byte to selected bus
Wire.endTransmission();
delay(100);
// initializing the rtc
if(!rtc.begin()) {
Serial.println("Couldn't find RTC!");
Serial.flush();
while (1) delay(10);
}
Serial.println("RTC OK");
rtc.disable32K();
rtc.clearAlarm(1);
rtc.clearAlarm(2);
rtc.writeSqwPinMode(DS3231_OFF);
rtc.disableAlarm(2);
pinMode(CLOCK_INTERRUPT_PIN, INPUT_PULLUP);
wdt_reset();
}
void loop() {
if(SetNextWakeup()){
delay(100);
wdt_disable();
deepSleep();
// after wakeup
WakeUp();
}
else {
Wire.beginTransmission(0x70);
Wire.write(1 << 0); // send byte to selected bus
Wire.endTransmission();
delay(1000);
}
wdt_reset();
}
void onAlarm() {
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, FALLING);
detachInterrupt(CLOCK_INTERRUPT_PIN);
Serial.begin(38400);
Serial1.begin(38400);
Serial2.begin(9600);
Serial3.begin(9600);
delay(1000);
Serial.println("Alarm occured!");
}
bool SetNextWakeup() {
DateTime now = rtc.now();
rtc.clearAlarm(1);
Serial.println("Alarm cleared");
if(!rtc.setAlarm1(now.unixtime() + 10,DS3231_A1_Date)) {
Serial.println("Error, alarm wasn't set!");
return false;
}
else {
Serial.println("Next Alarm will happen in 10 sec!");
attachInterrupt(digitalPinToInterrupt(CLOCK_INTERRUPT_PIN), onAlarm, LOW);
return true;
}
}
void deepSleep(){
char date[] = {"DD-MM-YYYY hh:mm:ss"};
rtc.now().toString(date);
Serial.println(date);
// the stored alarm value
DateTime alarm1 = rtc.getAlarm1();
Ds3231Alarm1Mode alarm1mode = rtc.getAlarm1Mode();
char alarm1Date[] = {"DD hh:mm:ss"};
alarm1.toString(alarm1Date);
Serial.print("Alarm1: ");
Serial.println(alarm1Date);
Serial.println("setting Arduino's params for the sleep mode");
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
delay(100);
Serial.println("next line will put Arduino in sleep mode");
digitalWrite(13, LOW);//LED "L"
Serial.end();
Serial1.end();
Serial2.end();
Serial3.end();
delay(100);
sleep_mode();
}
void WakeUp(){
sleep_disable();
detachInterrupt(CLOCK_INTERRUPT_PIN);
delay(100);
//power_all_enable();
wdt_reset();
MCUSR &= ~(1<<WDRF);
wdt_enable(WDTO_4S);
digitalWrite(31, LOW);//Reset multiplexer i2c
delay(500);
digitalWrite(31, HIGH);//Enadle multiplexer i2c
Wire.beginTransmission(0x70);
Wire.write(1 << 0); // send byte to selected bus
Wire.endTransmission();
delay(100);
digitalWrite(13, HIGH);//LED "L"
Serial.println("Wake Up");
Serial.println("sleep disabled for 5s");
delay(5000);
}
Serial output:
"RTC OK
Alarm cleared
Next Alarm will happen in 10 sec!
11-04-2025 14:53:14
Alarm1: 11 14:53:24
setting Arduino's params for the sleep mode
next line will put Arduino in sleep mode
Alarm occured!
Wake Up
sleep disabled for 5s" And the Arduino hangs not restarting....
Any suggestions?