int buttonPin = 2; // Pin for push
int buzzerPin = 4; // Pin for buzzer
bool buzzerOn = false; // Track buzzer state
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Input with internal pull-up
pinMode(buzzerPin, OUTPUT); // Set pin for buzzer
}
void loop() {
int lastButtonState = HIGH; // Store the last button state
int buttonState = digitalRead(buttonPin); // Read current state
// Check for button press (LOW state)
if (buttonState == LOW && lastButtonState == HIGH) {
buzzerOn = !buzzerOn; // Toggle buzzer state
if (buzzerOn) {
tone(buzzerPin, 600); // Turn on buzzer
} else {
noTone(buzzerPin); // Turn off buzzer
}
delay(250); // Debounce delay
}
lastButtonState = buttonState; // Update last button state
}