#include <LiquidCrystal_I2C.h> // Include the I2C LCD library
// Initialize the LCD (change 0x27 to your I2C address, 16x2 for columns and rows)
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int buzzerPin = 8; // Buzzer connected to pin 8
// Amazing Grace melody notes (in Hz) and durations (in ms)
int melody[] = {392, 440, 392, 349, 392, 294, 349, 392, 440, 392, 294, 392,
349, 294, 262, 294, 349, 392, 440, 392, 294, 262};
int noteDurations[] = {500, 500, 500, 500, 750, 500, 750, 500, 500, 500, 500, 500,
750, 500, 500, 500, 500, 500, 750, 500, 500, 1000};
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.print("Amazing Grace"); // Display initial message
delay(2000); // Wait 2 seconds
lcd.clear();
// Initialize the buzzer pin
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Play each note in the melody
for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i++) {
int noteDuration = noteDurations[i];
// Play the current note on the buzzer
tone(buzzerPin, melody[i], noteDuration);
// Display the note frequency on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Playing Note:");
lcd.setCursor(0, 1);
lcd.print(melody[i]); // Print the current frequency in Hz
// Wait for the note to finish
delay(noteDuration * 1.3);
// Stop the tone
noTone(buzzerPin);
}
// Wait 2 seconds before replaying the melody
lcd.clear();
lcd.print("Restarting...");
delay(2000);
}