Same pushbutton to sleep and wake up ATMega328P SOLVED

Hi!

I want a pushbutton to put my 328P to sleep and also to wake it up.

If the button is used for sleep, it won't wake up with that same button. However if I put it to sleep in main loop, the button works for wake up.

Power consumption active/sleep: 9mA / 0,330mA

Wake up does not work:

#include <Bounce2.h>
#include <avr/sleep.h>

Bounce debouncerPower = Bounce(); 


void setup() {

  pinMode(2, INPUT_PULLUP);

  debouncerPower.attach(2, INPUT_PULLUP);
  debouncerPower.interval(20);

}

void loop() {
  debouncerPower.update();

  if (debouncerPower.read() == LOW) {
    sleepNow();
  }

}


void wakeUpNow()
{

}

void sleepNow() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  attachInterrupt(0, wakeUpNow, LOW);
    
  sleep_mode();
  
  sleep_disable();
  detachInterrupt(0);
}

Wake up works:

#include <avr/sleep.h>

void setup() {
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  delay(3000);
  sleepNow();
}


void wakeUpNow()
{
  
}

void sleepNow() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  attachInterrupt(0, wakeUpNow, LOW);
    
  sleep_mode();
  
  sleep_disable();
  detachInterrupt(0);
}

Hoping that someone can point me in the right direction :slight_smile:

EDIT: Ok, I had too much blood in my caffeinestream, added a timer that doesn't allow it to sleep within 1000ms of wake up and it finally work as expected.

If you have another solution, feel free to contribute!