sleep_mode PWR_SAVE

Hi

I’m using MsTimer2 http://www.arduino.cc/playground/Main/MsTimer2 to launch periodically a function.

It works perfectly. As it uses Timer2, I’d like to use sleep_mode to put the arduino in PWR_SAVE in order to save energy

so i do this code as a test:

// Toggle LED on pin 13 each second
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <MsTimer2.h>

void flash() {
static boolean output = HIGH;

interrupts();
digitalWrite(13, output);
output = !output;
Serial.println(1, BYTE);
Serial.println(2, BYTE);
}

void setup() {
pinMode(13, OUTPUT);

Serial.begin(9600);
MsTimer2::set(10000, flash);
MsTimer2::start();

sei();
}

void sleepNow() // here we put the arduino to sleep
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet

  • http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
  • there is a list of sleep modes which explains which clocks and
  • wake up sources are available in which sleep modus.
  • In the avr/sleep.h file, the call names of these sleep modus are to be found:
  • The 5 different modes are:
  • SLEEP_MODE_IDLE -the least power savings
  • SLEEP_MODE_ADC
  • SLEEP_MODE_PWR_SAVE
  • SLEEP_MODE_STANDBY
  • SLEEP_MODE_PWR_DOWN -the most power savings
  • For now, we want as much power savings as possible, so we
  • choose the according
  • sleep modus: SLEEP_MODE_PWR_DOWN
  • Timer 2 overflow interrupt is only able to wake up the ATmega in PWR_SAVE

*/
set_sleep_mode(SLEEP_MODE_PWR_SAVE); // sleep mode is set here

sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin

sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

sleep_disable(); // first thing after waking from sleep:
// disable sleep…
}

void loop() {
sleepNow();
}

and it gives nonsense as output, it sends 10 chars \x0 every 20 sec…

This sketch works perfectly in SLEEP_MODE_IDLE, but not in SLEEP_MODE_PWR_SAVE

however timer2 works in SLEEP_PWR_SAVE, so it should work

Any idea is welcome !!

PS: i’ve been debugging this piece of code for hours with several guys on irc, i’ve tried a LOT of example
If you have an idea, please test it, because I may have already tried

Thanks for your help
I’m gonna crazy with this…

Hi again

After hours (and hours and hours...) of debug with chroos on IRC. This man has in the end found the solution !

Just add a delay(5) after last print...

in order to let the buffer empty itself before running in sleep mode

Thanks chroos !!

I have tried the code and the timer is working perfectly. The problem is the arduino is not going to sleep - I can tell since I connected another LED to pin 12 and toggle it every second by adding this code in the loop():

void loop() {
sleepNow();
digitalWrite(12, output12);
output12 = !output12;
delay(1000);
}

Any idea why my chip is not sleeping? I was expecting the new LED on 12 to only toggle every 10 seconds, but it toggles every second…

Hope you can help!

Maybe that’s the reason :):

void loop() {
sleepNow();
digitalWrite(12, output12);
output12 = !output12;
delay(1_000);
}

mettapera - my code with a delay of 1000 is simply to flash my LED on pin 12. BUT - I was not expecting this code to run, since the sleepNow statement at the top of the loop is supposed to sleep the chip for 10 seconds!

I am guessing that the mstimer library (since it is using an 8 bit timer) is not able to actually sleep the chip for the full 10 seconds, and that it overflows much more often but is put back to sleep.

What I wanted is a way to REALLY sleep the chip for a minute or more to save power....

Haha, seems like commenting at midnight isn't a good idea...I understand your point now but I'm not into interrupts so can't really help :(