#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Initialize the LCD object with the correct address and number of columns/rows
const int buttonPin = 2;
const int buzzerPin = A0;
const int speakerPin = A4;
const int tempo = 200;
// "Happy Birthday" Song
const char* melody = "GGABCDEFGDCBAGC";
// Corresponding lyrics for each note
const char* lyrics[] = {
"Happy ", "birthday ", "to ", "you ",
"Happy ", "birthday ", "to ", "you ",
"Happy ", "birthday ", "dear ", "friend ",
"Happy ", "birthday ", "to ", "you "
};
int noteDurations[] = {
4, 4, 4, 4,
4, 4, 4, 4,
4, 4, 4, 4,
4, 4, 4, 4
};
bool buttonState = false;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void setup() {
lcd.begin(16, 2); // Initialize the LCD with the specified dimensions (16 columns x 2 rows)
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
lcd.setCursor(0, 0);
lcd.print("Press Button!");
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Button Pressed!");
delay(1000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Playing Song...");
int songLength = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < songLength; i++) {
lcd.setCursor(0, 1);
lcd.print(lyrics[i]);
playTone(melody[i], tempo);
delay(tempo);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Happy Birthday!");
delay(2000); // Display "Happy Birthday!" for 2 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Press Button!");
}
}