#define BUZZER_PIN 8
#define BUTTON_PIN 1
unsigned long buttonPressTime = 0;
unsigned long lastBeepTime = 0;
bool isButtonPressed = false;
bool isBeepOn = false;
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Use internal pull-up resistor
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
unsigned long currentTime = millis();
// Check if button is pressed
if (buttonState == LOW && !isButtonPressed) {
isButtonPressed = true;
buttonPressTime = currentTime;
noTone(BUZZER_PIN); // Stop the tone immediately
}
// Handle button press timing
if (isButtonPressed) {
if (currentTime - buttonPressTime < 10000) {
noTone(BUZZER_PIN); // Keep the buzzer off for 10 seconds
} else {
isButtonPressed = false; // Reset the button press state after 10 seconds
}
} else {
// Handle beeping logic
if (currentTime - lastBeepTime >= 500) { // Change beep interval here (500ms on/off)
if (isBeepOn) {
noTone(BUZZER_PIN); // Turn off the buzzer
} else {
tone(BUZZER_PIN, 1000); // 1000 Hz frequency
}
isBeepOn = !isBeepOn; // Toggle beep state
lastBeepTime = currentTime; // Update last beep time
}
}
delay(10); // Short delay for button debouncing
}