Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created Nov 27, 2013
Embed
What would you like to do?
Simplified sleep Demo (Created since the others I found were too complex to really illustrate the point)
/* Simplified sleep Demo
* ---------------------
* Shows how to make the Arduino sleep and wake up again.
*
* Connect pin 2 and 12 to 5V via a pullup-resistor (10k)
* Connect pin 12 to ground to make the Arduino sleep
* Connect pin 2 to ground to make the Arduino wake up again
*
* Based on the code in http://www.gammon.com.au/forum/?id=11488
* by Nick Gammon. Example written by Jens Chr Brynildsen 2013
*/
#include <avr/sleep.h>
int sleepPin = 12; // Connect this pin to ground to sleep
int wakePin = 2; // Connect this pin to ground to wake up
void setup() {
pinMode(13, OUTPUT);
}
void loop() {
// toggle the LED
digitalWrite(13, !digitalRead(13));
delay(100);
// Check if we should sleep
byte shouldWeGoToSleep = digitalRead(sleepPin);
if (sleepStatus == LOW) { // put the device in sleep
sleepNow();
}
}
// interrupt service routine in sleep mode
void wake ()
{
sleep_disable (); // first thing after waking from sleep:
detachInterrupt (0); // stop LOW interrupt
} // end of wake
void sleepNow ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // make sure we don't get interrupted before we sleep
sleep_enable (); // enables the sleep bit in the mcucr register
attachInterrupt (0, wake, LOW); // wake up on low level
interrupts (); // interrupts allowed now, next instruction WILL be executed
sleep_cpu (); // here the device is put to sleep
} // end of sleepNow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment