/* modified from orignal noted below code now */
# include <avr/sleep.h>
const byte interruptPin = 2;
const byte LEDPin = 6;
void flashLed(int count, int t) {
for (byte i = 0; i < count; i++) {
digitalWrite(LEDPin, HIGH);
delay(t);
digitalWrite(LEDPin, LOW);
delay(t);
}
}
void setup() {
Serial.begin(57600);
Serial.println("hello sleep world!\n");
pinMode(LEDPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
}
void loop() {
flashLed(2, 500);
Serial.println("AWAKE");
flashLed(10, 100);
Serial.println("Going to sleep in 3 sec");
flashLed(3, 500);
Going_To_Sleep();
Serial.print("woke up and fully ");
}
byte didAnything = 0; // did we even use the ISR?
void Going_To_Sleep() {
sleep_enable();//Enabling sleep mode
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW );
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
digitalWrite(LEDPin, LOW); //turning LED off
sleep_cpu();//activating sleep mode
Serial.print("just woke up! ");//next line of code executed after the interrupt
Serial.println(didAnything ? " did ISR" : " didn't ISR");//next line of code executed after the interrupt
didAnything = 0;
digitalWrite(LEDPin, HIGH); //turning LED on
delay(500);
}
void wakeUp() {
didAnything = 1;
Serial.println("\nInterrupt Fired...");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(digitalPinToInterrupt(interruptPin)); //Removes the interrupt from interruptPin;
}
/**
Author:Ab Kurk version: 1.0 date: 24/01/2018
Description: This sketch is part of the beginners guide to putting your Arduino to sleep
tutorial. It is to demonstrate how to put your arduino into deep sleep and how to wake it up.
Link To Tutorial http://www.thearduinomakerman.info/blog/2018/1/24/guide-to-arduino-sleep-mode
INFO
Micro, Leonardo, other 32u4-based boards support interrupts on pins 0, 1, 2, 3, 7
Sleep modes For the Atmega328P (Nick Gammon http://www.gammon.com.au/power
SLEEP_MODE_IDLE: 15 mA //does not stay asleep
SLEEP_MODE_ADC: 6.5 mA //does not stay asleep
SLEEP_MODE_PWR_SAVE: 1.62 mA //wont wake up
SLEEP_MODE_EXT_STANDBY: 1.62 mA //wont wake up
SLEEP_MODE_STANDBY : 0.84 mA //wont wake up
SLEEP_MODE_PWR_DOWN : 0.36 mA //wont wake up
*/