interrupting from sleep.

Hello.

I'm stuck on something here: I need the Arduino to be in sleep, be woken up by an interrupt, do something simple (like switch on/off an LED) and then go back to sleep. A code I found on Playground was pretty confusing. I'm doing something like this:

void wake_up()
{
  detachInterrupt(0); 
  //do something
}

void sleepnow()
{
  //set_sleep_mode.. power down
  //sleep enable
  attachInterrupt(0, wake_up, LOW);
  sleep_mode();
  sleepnow();  //recursive, sleep again..
}

What I don't understand is, will the recursion work? Does the execution return to sleepnow() after the ISR, or does it go back to loop() ? The controller stops responding after a few tests, as if it goes to sleep without enabling interrupts.

hello :slight_smile:

a good link is better than a long talk..... have a look here :

everything you need to know is clearly explained and detailed :slight_smile:

and for your code, find the "Waking from sleep with a signal" in the link above, it does exactly what you want to do, and it is easy to understand :wink:

Thank you very much. It was indeed very helpful and quite comprehensive.

I'm still unclear on one thing (which the page didn't mention): what is the difference between sleep_cpu() and sleep_mode() ?
All except one example on the page use sleep_cpu().

in the Nick Gammon's page, I don't see a sleepMode() function, only a set_sleep_mode(mode) one.
There are several sleep modes for the controller :
SLEEP_MODE_IDLE
.......
.......
SLEEP_MODE_PWR_DOWN
each providing different behavior, and .... current consumption

before you call sleep_cpu() , you tell in which mode it will sleep with a set_sleep_mode(mode) call

He uses the function sleep_mode() just once in the last example.

I've seen the sleep_mode() function being used here in this Playground:
http://playground.arduino.cc/Learning/arduinoSleepCode

And also, thank you again for the link. But for a few doubts, it worked! :slight_smile: