Hello forum,
first post here, so lets get started.
After much searching I never found code to perform this function.
I needed a momentary switch to place an Arduino in sleep and subsequently interrupt for bring it to awake mode.
below is the code I have that works currently.
It uses the benefit of defining pins to establish what the code is looking for.
Hope this is useful for someone here
/* This code is for the on/off sleep cycles of the arduino Uno/Nano
* Since the switch is momentary a delay after detaching interrupt keeps the system so
* a long press of the switch will power off the device and a single press will turn on
* device from sleep mode.
*
* Pins are currently set as 3 for the interrupt and 3 for the watched pin. Power button will send the pin low
* if the device is on it functions as a digital pin to be read after brought low.
* if the device is off, the pin functions as an interrupt to the µP to wake the device
*/
#include <avr/sleep.h>
void setup(){
Serial.begin(115200);
pinMode(A1, OUTPUT);
}
void loop(){
interruptFunction();
sleepFunction();
Serial.println("AWAKE!");
}
void sleepFunction() {
pinMode(2,INPUT_PULLUP);
if (digitalRead(2) == LOW){
sleep_enable(); // set the sleep enable bit
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
// Add any pins you want to send low for the sleep state in this function
delay(250);
attachInterrupt(0, interruptFunction, FALLING);
sleep_cpu(); // put the arduino into sleep mode
// code after wake up
}
}
void interruptFunction() {
// code to be run upon boot up
sleep_disable(); // clear the sleep enable bit
detachInterrupt(0); // detach the interrupt
delay(750);
digitalWrite(A1, HIGH);
}