#define BUZZER_PIN 2
#define BUTTON_PIN 4
const int toneDuration = 1000; // Duration in milliseconds
bool buttonPressed = false;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button connected with pull-up resistor
}
void loop() {
buttonPressed = !digitalRead(BUTTON_PIN); // Check if button is pressed
if (buttonPressed) {
tone(BUZZER_PIN, 5000); // Play tone
delay(toneDuration);
noTone(BUZZER_PIN); // Stop tone
}
}