#define BUZZER_PIN D7 // Buzzer pin
#define BUTTON_PIN D3 // Button pin
#define LED_PIN D4 // LED pin
void setup() {
Serial.begin(115200); // Initialize Serial communication
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set button pin as input with pull-up resistor
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
digitalWrite(BUZZER_PIN, LOW); // Ensure buzzer is off initially
digitalWrite(LED_PIN, LOW); // Ensure LED is off initially
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN); // Read button state
// Check if button is pressed (LOW state)
if (buttonState == LOW) {
digitalWrite(LED_PIN, HIGH); // Turn on LED
Serial.println("Buzzer Ringing!"); // Print message to Serial Monitor
// Accuracy: Play tone at 262 Hz with maximum volume
tone(BUZZER_PIN, 262, 500); // Play 262 Hz tone for 500 milliseconds
delay(500); // Wait for the tone duration
noTone(BUZZER_PIN); // Stop the tone
// Note: Volume is set to maximum (1.0) as `tone()` does not support volume control directly
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
}
}