Cant get board to wake following interrupt
I'm using LED flashes to indicate the state of the program; and a switch to start the "sleep" process so that following a reset the arduino will not go to sleep and prevent reprogramming.
In the code that follows I have this line commented,
//sleep_cpu(); //activating sleep mode
and I can demonstrate that the interrupt is working with a message on the serial monitor "Interrupt Fired" in wakeUp();
However if I remove the comment marks so it goes to sleep one of two things happens; (listed at start of code)
SLEEP_MODE_IDLE: or SLEEP_MODE_ADC: does not stay asleep
all other sleep modes - pressing the button for the interrupt has no effect, it wont wake up
I've been fighting with this for a while now, grateful for any suggestions.
/**
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
*/
#include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes
const byte interruptPin = 7; //Pin we are going to use to wake up the Arduino
const byte sleepPin = 9; //Pin we are going to use to sleep the Arduino - button to gnd
const byte LEDPin = 6; //connected to led & resisitor on pin 6
void flashLed(int count, int t) {
//shows its not asleep
for (int i = 0; i < count; i++) {
digitalWrite(LEDPin, HIGH); //turning LED on
delay(t); //happy to use delay() as it wont prevent an interrupt
digitalWrite(LEDPin, LOW); //turning LED off
delay(t); //total time per rep is 0.5 sec
}
}
void setup() {
Serial.begin(57600);//Start Serial Comunication
pinMode(LEDPin, OUTPUT); //indicate when Arduino is Asleep
pinMode(interruptPin, INPUT_PULLUP); //Set interruptPin as input
pinMode(sleepPin, INPUT_PULLUP); //Set sleepPin as input
flashLed(2, 1000); // show state and allow time for serial comms to start
Serial.println("Initialisation complete");
flashLed(6, 250);// allow 6 flashes = 3 sec to write above message or reprogram before it can sleep
Serial.println("Setup completed");
delay(1000);
}
void loop() {
flashLed(2, 500);
Serial.println("not ready to sleep");
delay(1000);
if (digitalRead(sleepPin)) flashLed(10, 100); //not going to sleep yet
else {
Serial.println("Going_To_Sleep in 5 sec");
flashLed(5, 500); //slow flash 5 seconds before going to sleep
Going_To_Sleep();
}
}
void Going_To_Sleep() {
sleep_enable();//Enabling sleep mode
//attachInterrupt(0, wakeUp, LOW);//attaching a interrupt to pin d2
//https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
//changed above line to use recommended syntax, and trigger on rising edge
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, RISING );
//set_sleep_mode(SLEEP_MODE_PWR_DOWN);//Setting the sleep mode, in our case full sleep
set_sleep_mode(SLEEP_MODE_IDLE);
digitalWrite(LEDPin, LOW); //turning LED off
delay(1000); //wait a second to allow the led to be turned off before going to sleep
//sleep_cpu();//activating sleep mode
Serial.println("just woke up!");//next line of code executed after the interrupt
digitalWrite(LEDPin, HIGH); //turning LED on
}
void wakeUp() {
Serial.println("Interrupt Fired");//Print message to serial monitor
sleep_disable();//Disable sleep mode
detachInterrupt(digitalPinToInterrupt(interruptPin)); //Removes the interrupt from interruptPin;
}