When not busy, I want to put the Arduino asleep. It will wake up one of three ways:
-
power cycle (i.e. reset)
-
hardware interrupt - For example, the user presses a button which is tied to pin 2 which is monitored by attachInterrupt(). Fairly straightforward and well documented.
-
sleep(time) - When a certain amount of time has elapsed (10 seconds, 4 hours, whatever), the Arduino should wake itself up. The delay() function would work but I want the power savings of sleep.
The sleep functions (</avr/sleep.h>) don't use any kind of time delay which means a hardware interrupt is the only way to wake up. I want a timer interrupt.
Looking at various AVR code it looks like timer2 should be able trigger an interrupt (ISR). Unfortunately I don't know how to do this and I can't find any good examples.
Has anybody written a Sleep(time) function? It seems to me that sleep(time) should even be part of the core library. Is it really that difficult or am I missing something obvious? If someone can point me at documentation for a potential solution, I'd appreciate it.