I'd like to delay/sleep the execution of my code for about 9 hours. I've tried incrementing an integer in the loop() and resuming the execution once it has hit a threshold, but this only works for about an hour and then jumps back into the code (some sort of overflow?)...
I've also tried the MsTimer2-library, but if I increase the time-to-sleep past 40 seconds (40000ms), it seemingly "sleeps" forever and never wakes up (the callback never fires).
The relevant part of my code:
void loop() {
switch (mode) {
case NORMAL:
analogWrite(ULNOutputPin, 255);
break;
case SLEEP:
analogWrite(ULNOutputPin,0);
#ifdef DEBUG
Serial.print("\nSleeping...");
#endif
break;
}
// Button "Sleep" pressed?
if (sleepButton.pressed()) {
if(mode == NORMAL) {
mode = SLEEP;
MsTimer2::set(32400000, wakeUp); // 9 hours in ms
MsTimer2::start();
} else {
mode = NORMAL;
MsTimer2::stop();
}
#ifdef DEBUG
Serial.print("\nSleep-Button pressed, mode is now ");
Serial.print(mode);
#endif
}
delay(200);
}
void wakeUp() {
mode = NORMAL;
MsTimer2::stop();
#ifdef DEBUG
Serial.print("\nSleep time ended, time to get up!");
#endif
}
Is what I'm trying to achieve possible without connecting an external quartz? I've searched the forums but haven't found a suitable answer (likely due to stupid search-terms :-)!)...
That works, but I'd like to retain responsiveness if possible, i.e. pressing the "Sleep" button again should return the mode back to NORMAL (and if I use delay(), the Arduino will not register button-presses any more).
In that case, you need to record millis() at the start of your sleep, then have a loop that checks the sleep button, then checks whether millis() has increased by 32,400,000 since the start.