Arduino Nano Every Sleep Mode

Although this topic has been discussed a couple of times here, they were all closed without a working solution, which is quite frustrating. Anyways...
I'm trying to wake the Atmega4809 (Nano Every) from a sleep mode, which much like Sleeping Beauty, is easy to put to sleep, but nearly impossible to wake-up.
I've got working code for sleep mode mode with timer wake-up, but I need the seemingly simple option of waking from a digital input interrupt. Although I've tried several methods, from simple to slightly more bitwise conscious, nothing seems to work.

Here is the code for what I believe to be the closest (though still no cigar):

/* 
This program will put the ATMEGA in a sleep mode 
and should !! wake it up via a hardware Interrupt, INT0
 ---> Pin2 Wake-Up NOT working...
*/
#include <avr/sleep.h>
#include <avr/interrupt.h>

#define interruptPin 2

unsigned long wakeUpMillis = 0;
bool IntAttached = true; // Interrupt is attached
int buttonState = 0;

void setup()
{
 Serial.begin(9600);//Start Serial Comunication
 pinMode(LED_BUILTIN,OUTPUT);
 pinMode(interruptPin,INPUT_PULLUP);
 //attachInterrupt(digitalPinToInterrupt(interruptPin), goToSleep, LOW);
}

void loop()
{
 Serial.print("Start Up | ");
 digitalWrite(LED_BUILTIN,HIGH);
 delay(500);
 digitalWrite(LED_BUILTIN,LOW);
 delay(500);

 buttonState = digitalRead(interruptPin);
 //if (!IntAttached && millis() - wakeUpMillis > 2000)
 if (buttonState == HIGH)
 {
   Serial.print("SLEEP");
   //goToSleep();
   noInterrupts();
   attachInterrupt(digitalPinToInterrupt(interruptPin), goToSleep, LOW);
   interrupts();
   IntAttached = true;
 }
}

void goToSleep()
{
 Serial.println("Going to sleep...");
 sleep_enable();                       //Enabling sleep mode
 noInterrupts();
 attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, RISING);
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 delay(2000);
 interrupts();
 sleep_cpu();//activating sleep mode
 // Now the µController is asleep...
 //
 // 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();
}

Any suggestions or fingers pointing in the right direction would e greatly appreciated.
Thanks!

You want to the interrupt to wake up the processor.
But: on interrupt you call the function goToSleep.

1 Like

Thank you @stitech, that definitely helped me look in a more specific direction.
I found this post that I managed to integrate into my main code quite easily, with a working interrupt on D2: LINK
All running quite nicely now.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.