const int BUTTON_PIN = 12; // set the button pin number
const int BUZZER_PIN = 13; // set the buzzer pin number
bool buzzerOn = false; // create a variable to store the buzzer state
void setup() {
Serial.begin(9600); // initialize the serial communication
pinMode(BUTTON_PIN, INPUT_PULLUP); // set the button pin as input with pull-up resistor
pinMode(BUZZER_PIN, OUTPUT); // set the buzzer pin as output
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) { // if the button is still pressed after debounce
toggleBuzzer(); // toggle the buzzer on/off
}
}
}
void toggleBuzzer() {
buzzerOn = !buzzerOn; // toggle the buzzer state
digitalWrite(BUZZER_PIN, buzzerOn ? HIGH : LOW); // turn the buzzer on or off based on the state
Serial.println(buzzerOn ? "Buzzer is on." : "Buzzer is off."); // display a message indicating the current state of the buzzer
}