#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with the I2C address (usually 0x27)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buttonPin = 2; // Pushbutton pin
const int buzzerPin = 9; // Buzzer pin
int buttonState = 0; // Variable to store the button state
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Initialize the LCD
lcd.begin(16, 2); // Provide the number of columns and rows
lcd.backlight();
// Print initial message to the LCD
lcd.setCursor(0, 0);
lcd.print("BUZZER OFF!");
}
void loop() {
// Read the state of the pushbutton
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// Debounce delay
delay(50);
if (digitalRead(buttonPin) == HIGH) {
// If the button is pressed
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BUZZER ON!");
// Play a simple tune on the buzzer
tone(buzzerPin, 440); // Play note A4
delay(500);
tone(buzzerPin, 494); // Play note B4
delay(500);
tone(buzzerPin, 523); // Play note C5
delay(500);
noTone(buzzerPin); // Stop the tone
// Wait for button release
while (digitalRead(buttonPin) == HIGH) {}
// Clear LCD after playing the song and update the message
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("BUZZER OFF!");
}
}
}