#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // Sesuaikan alamat I2C jika diperlukan
const int buzzerPin = 9;
const int ledPins[] = {10, 11, 12};
const int buttonPin = 2; // Pin yang digunakan untuk tombol push button
const char *birthdayName = "Cheraku";
// Notes of the melody (Happy Birthday)
const int melody[] = {
262, 262, 294, 262, 349, 330,
262, 262, 294, 262, 392, 349,
262, 262, 523, 440, 349, 330, 294,
466, 466, 440, 349, 392, 349
};
// Note durations: 4 = quarter note, 8 = eighth note, etc.
const int noteDurations[] = {
8, 8, 4, 4, 4, 2,
8, 8, 4, 4, 4, 2,
8, 8, 4, 4, 4, 4, 2,
8, 8, 4, 4, 4, 2
};
// Lirik lagu Happy Birthday
const char* lyrics[] = {
"Happy", "Birthday", "to", "You",
"Happy", "Birthday", "to", "You",
"Happy", "Birthday", "dear", birthdayName,
"Happy", "Birthday", "to", "You"
};
int lyricIndex = 0; // Index untuk mengakses lirik
bool birthdaySongPlaying = true;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize LED pins
for (int i = 0; i < 3; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize button pin
pinMode(buttonPin, INPUT); // Mengaktifkan pull-up resistor internal
// Display welcome message
lcd.setCursor(0, 0);
lcd.print("Happy Birthday");
lcd.setCursor(0, 1);
lcd.print(birthdayName);
delay(5000); // Wait for 2 seconds
// Clear LCD
lcd.clear();
}
void loop() {
// Play the melody and control LEDs and LCD
for (int i = 0; i < 25 && birthdaySongPlaying; i++) {
int noteDuration = 1000 / noteDurations[i];
tone(buzzerPin, melody[i], noteDuration);
// Control LEDs according to the beat
digitalWrite(ledPins[i % 3], HIGH); // Turn on LED
// Display lyrics on LCD
if (i == 0 || (i > 0 && shouldDisplayNewLyric(i))) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(lyrics[lyricIndex]);
if (lyricIndex == 9) { // Split 'dear [name]' into two lines for better display
lcd.setCursor(0, 1);
lcd.print(birthdayName);
}
}
delay(noteDuration); // Note is played
digitalWrite(ledPins[i % 3], LOW); // Turn off LED
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
if (birthdaySongPlaying) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LoveU Babe");
delay(3000); // Wait for 3 seconds
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dari Zidni");
lcd.setCursor(0, 1);
lcd.print("Aku Sayang Kamu");
// Wait for button press
while (digitalRead(buttonPin) == LOW) {
// Tunggu sampai tombol ditekan
delay(10); // Debounce delay
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Langgeng");
lcd.setCursor(0, 1);
lcd.print("Ya Sayang!");
// Beep the buzzer briefly
tone(buzzerPin, 1000, 200); // Frequency 1000 Hz, duration 200 ms
delay(200); // Wait for the beep to finish
delay(2000); // Display for 2 seconds
// Turn off LCD, buzzer, and LEDs
lcd.clear();
noTone(buzzerPin);
for (int i = 0; i < 3; i++) {
digitalWrite(ledPins[i], LOW);
}
birthdaySongPlaying = false; // Stop playing the birthday song
}
// Delay before ending the program
delay(2000);
}
bool shouldDisplayNewLyric(int noteIndex) {
// Determine if the lyric should change based on note index
if (noteIndex == 23) { // After the second "Happy Birthday"
lyricIndex++;
return true;
} else if (strcmp(lyrics[lyricIndex], "to") == 0 || strcmp(lyrics[lyricIndex], "You") == 0) {
lyricIndex++; // Increment for single-note words
return true;
} else {
if (noteIndex % 2 == 0) { // Change lyric every two notes otherwise
lyricIndex++;
return true;
}
}
return false;
}