In my application, I need to keep track of the time the application has been running as well as use sleep modes. However, when using the sleep modes, Timer0 is disabled, and therefore millis stop counting.
Is there a way to measure/count how long the Arduino was in sleep mode for?
To give you some more background info, this is what I'm trying to achieve: I am using a pressure sensor which sends an interrupt to the Arduino every 80ms and wakes it up. The program does various routines to process the data which can take anywhere from 2ms to 60ms. It then goes into sleep mode until another external interrupt occurs, approx 20-78ms later. The application has many processes based on time, such as a stopwatch, timer and alarm, so I need to be able to keep track of this, preferably using millis(). The device is running on a single cell 250mah LiPo battery which at present only lasts about 4 hours. I'm using an ATMega1284.
In most of the sleep modes Timer0 is disabled. Which means that millis() stops counting. Is there a way, without using an RTC (Both Space and weight are at a premium), to keep millis counting whilst in sleep mode?
I notice that SLEEP_MODE_IDLE keeps Timer0 running, but that means that for every millisecond an interrupt wakes up the device. Please correct me if I'm wrong, but given that my loop can take about 60ms to execute, I don't see much advantage to sleeping for 1ms.
Possible solutions:
- Use Timer2 for the millis function?
- Use the Watchdog Timer to count how much time has passed whilst in sleep mode and add this to millis() when waking up?
- If there was a VERY small SMD RTC which didn't need any external components such as oscilators etc then this might be feasible.
Power-save
sleep mode. I've done in on a clock project, but it's a bit of work figuring out how to set it up the code. After calibration it now accurate to within a minute per year.