Hi
I want to make Arduino Nano go to deep sleep using interrupt, Pin 2. And then wake up with same pin.
Schematic => A jumper wire connected to pin D2.
Here is the code:
#include <avr/sleep.h>
#define interruptPin 2
unsigned long wakeUpMillis = 0;
bool IntAttached = true; // Interrupt is attached
void setup()
{
Serial.begin(115200);//Start Serial Comunication
pinMode(LED_BUILTIN,OUTPUT);
pinMode(interruptPin,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), goToSleep, LOW);
}
void loop()
{
digitalWrite(LED_BUILTIN,HIGH);
delay(500);
digitalWrite(LED_BUILTIN,LOW);
delay(500);
if (!IntAttached && millis() - wakeUpMillis > 2000)
{
noInterrupts();
attachInterrupt(digitalPinToInterrupt(interruptPin), goToSleep, LOW);
interrupts();
IntAttached = true;
}
}
void goToSleep()
{
sleep_enable();//Enabling sleep mode
noInterrupts();
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
Serial.println("Going to sleep..... zzzZZZZ");
delay(2000);
interrupts();
sleep_cpu();//activating sleep mode
// Now the µController is asleep........zzzZZZZZZ
//
//
// will resume from here
//
//
Serial.println("just woke up!");
}
void wakeUp()
{
noInterrupts();
detachInterrupt(digitalPinToInterrupt(2));
interrupts();
IntAttached = false; // Interrupt is detached.
Serial.println("Interrupt Fired");
sleep_disable();
wakeUpMillis = millis();
}
The problem is that the Nano goes to sleep and wakes up immediately when D2 is LOWered.