Your sketch is quite close. If you use a LOW on Pin 2 (Interrupt 0) to wake up the processor, you have to make Pin 2 your button pin. Once you do that, you can have the processor sleep until the button pin goes HIGH or LOW. There is no need to keep track of the LastState because the processor only wakes up when there is a change to the new state.
Also, no need to set both pin 'buz' (13) and pin LED_BUILTIN (13).
I tested this code on an Arduino UNO and the LED lights up for 250 milliseconds each time the button on Pin 2 is pressed or released:
#include <avr/sleep.h>
#include <avr/power.h>
// this constant won't change:
const byte ButtonPin = 2; // the pin that the pushbutton is attached to
// the button must be wired from pin to ground, it's pinmode is input_pullup
const byte BuzzerPin = LED_BUILTIN; // Use LED to simulate buzzer
// The Interrupt Service Routine (ISR) does nothing, but we need an ISR
void WakeISR() {}
// Sleep until the button pin goes HIGH or LOW
void SleepUntil(int state)
{
attachInterrupt(digitalPinToInterrupt(ButtonPin), WakeISR, state);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Only sleep if the button is not yet in the desired state
if (digitalRead(ButtonPin) != state)
sleep_mode(); // Sleep here until selected state causes and interrupt
sleep_disable();
detachInterrupt(digitalPinToInterrupt(ButtonPin));
}
// https://forum.arduino.cc/index.php?topic=631378
// beep on press and another beep on release
// this one uses delay()
// ************* ONLY TESTED WITH AN LED *************
void setup()
{
// initialize serial communication:
Serial.begin(9600);
Serial.println(".... beeps, uses DELAY ....");
Serial.print("Compiler: ");
Serial.print(__VERSION__);
Serial.print(", Arduino IDE: ");
Serial.println(ARDUINO);
Serial.print("Created: ");
Serial.print(__TIME__);
Serial.print(", ");
Serial.println(__DATE__);
Serial.println(__FILE__);
// initialize the button pin as a input with pullup so active low
// make sure the button is from pin to ground
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(BuzzerPin, OUTPUT);
digitalWrite(BuzzerPin, LOW);
Serial.println();
Serial.println(" *** ONLY TESTED WITH AN LED ***");
Serial.println("Setup done... press the button to beep, release to beep again");
Serial.println();
}
void loop()
{
// Wake up when Pin 2 goes LOW (pressed)
SleepUntil(LOW);
// if the current state is LOW then the button was pressed
Serial.print("Beep on press... ");
digitalWrite(BuzzerPin, HIGH);
delay(250);
digitalWrite(BuzzerPin, LOW);
// Wake up when Pin 2 goes HIGH (released)
SleepUntil(HIGH);
// if the current state is HIGH then the button was released
Serial.println("Beep on Release");
digitalWrite(BuzzerPin, HIGH);
delay(250);
digitalWrite(BuzzerPin, LOW);
} //loop
You connect your awakening interrupt with pin 2 but it seems that your button pin is 12. So I guess your hardware is not matching your code but that's a wild guess as you failed to provide any detailed information about your hardware setup (wiring diagram)!
Don't expect us to do your job. I give you another hint: if your button is fixed to pin 12 you must change your code completely, you cannot use the external interrupt feature but must use the pin change interrupt.