I have an application who is run by batteries, I need to put the Arduino Micro to sleep and wake it up again with an interrupt. I have searched the net and found different approaches to put the Arduino to sleep, but I simply can't get it to work.
When I program the Arduino Micro with the code below, it drops the usb connection to the board and nothing happens, no LED is on and when I pulls pin 2 low nothing happens???
#include <avr/sleep.h>
int wakePin = 2; // pin used for waking up
int led = 13;
void setup()
{
pinMode(led, OUTPUT);
digitalWrite(led, LOW); //Lights on when not in sleep
pinMode(wakePin, INPUT);
digitalWrite(wakePin, HIGH); //Activate the internal pull up resistor
}
void loop()
{
delay(5000); //wait 5 sec. before sleep
SleepNow();
}
void SleepNow() // here we put the arduino to sleep
{
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(0, WakeUp, LOW); // use interrupt 0 (pin 2) and run function wakeUp when pin 2 gets LOW
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
digitalWrite(led, LOW);
//sleep_cpu();
sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
for(int i = 0; i < 5; i++) //when wake up blink with the onboard led 5 times
{
digitalWrite(led, HIGH);
delay(100);
digitalWrite(led, LOW);
}
}
void WakeUp() // here the interrupt is handled after wakeup
{
sleep_disable();
detachInterrupt(0);
}