Sleep Mode - Problem with Pin Change Interrupt

I am testing my Sleep Mode sketch using a button that triggers a Pin change interrupt for the sleep mode.
If the button is pressed until the microcontroller goes to sleep, everything works fine -->the microcontroller wakes up after the button is released.

BUT, if the button is released before the microcontroller goes to sleep, the microcontroller won't wake up if the button is pressed again.

Can someone explain me this "behaviour" of the microcontroller?

I am using an Arduino Uno.

//Sleep-Modus
  #include <avr/sleep.h>//this AVR library contains the methods that controls the sleep modes

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 8;    // the number of the pushbutton pin

// Variables will change:
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

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

    // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

void loop() {
  delay(300);
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer
    // than the debounce delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;
    }
        Serial.println("going to sleep");
        delay(100);
        sleepSetup();   //now we see ~27 mA until pin 2 is connected to GND
  }

  lastButtonState = reading;
}

void sleepSetup()
{
    sleep_enable();
    attachInterrupt(0, pinInterrupt, CHANGE);//Set pin 2 as interrupt and attach Interrupt Service Routine (ISR)
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);//define power down sleep mode
    sleep_cpu();//Set sleep enable (SE) bit, this puts the ATmega to sleep
    sleep_disable();//this is important. It is possible that the interrupt is called between executing "attachInterrupt(...)" and sleep_CPU() in the main loop
                  //if that happens without the sleep_disable() in the ISR, the ISR would be called, the interrupt detached and the device put to sleep.
                  //since the interrupt would be disabled at that point, there would be no way to wake the device up anymore.
                  //by putting sleep_disable() in the ISR, sleep_cpu() would not be effective during that loop, i.e. the main loop would run one more time
                  //and then properly attach the interrupt before hitting the sleep_cpu() a second time. At that point the device would go to sleep, but 
                  //the interrupt would now be activated, i.e. wake-up can be induced.
    Serial.println("just woke up!");//When it wakes up due to the interrupt the program continues with the instruction following sleep_cpu() 

}

void pinInterrupt()//ISR
{
  detachInterrupt(0);//disable the interrupt. This effectively debounces the interrupt mechanism to prevent multiple interrupt calls.

}

Thanks for the reply.

Unfortunately I do not understand why there is a difference if I hold the button until the mc goes to sleep or release it before the mc goes to sleep and trigger a new interrupt (what is not working).
In my code the interrupt is attached everytime before the mc goes to sleep.
What would be the correct procedure?

Thanks again for your support!

I think I still have some general misunderstandings:
I thought that after an interrupt is fired, the interrupt gets detached and therefore no further interrupt is possible until it gets attached again.
In the code the interrupt always gets attached before going to sleep, therefore you should always have an interrupt attached after going to sleep?!
I don't see the difference in releasing / pushing the button (should be also possible multiple times) before the code gets the next time to the sleep code-line.

sleep_enable();
attachInterrupt(0, pinInterrupt, CHANGE);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_cpu();
sleep_disable()

Regarding the other options:
CHANGE to FALLING: I need the CHANGE because it should work on high and on low (trigger every push and release --> at the end I will use a switch)

Don't try to detach: That works of course, because the interrupt always fires. But my goal is to have no further interrupt unless the code is processed one entire time, then "activating" the interrupt again...

Thanks for your patience :wink:

So here once more the code including the ISR:

void sleepSetup()
{
A) sleep_enable();
B) attachInterrupt(0, pinInterrupt, CHANGE);
C) set_sleep_mode(SLEEP_MODE_PWR_DOWN);
D) sleep_cpu();
E) sleep_disable();
F) Serial.println("just woke up!"); the instruction following sleep_cpu()

}

void pinInterrupt()//ISR
{
G) detachInterrupt(0);

}

So I thought that if a interrupt is fired:

  1. the pinInterrupt function is called and the interrupt detached (line G)
  2. sleep is disabled and Serial printed (part of the sleepSetup function) (line E&F)
    Now there is no interrupt possible until I call the sleepSetup function again
  3. If I call the sleepSetup function again only lines A to D are processed and the interrupt is attached again (line B)

If I described the process correct, it is not clear for me why it makes a difference if the button is released between step 2. and 3. (if done so, theinterrupt is not working when microcontroller goes to sleep mode next time) but the button has to be pressed until the microcontroller goes to sleep (then it is working) [or vice versa, I am just describing the possibility button pressed].

I thought that it doesn't matter what happens with the button after the interrupt is fired because the interrupt is detached and that it is only relevant that there is a change of the button after the interrupt is attached again.
This happens only when I am calling the sleepSetup function the next time.