#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
// Inisialisasi LCD I2C
LiquidCrystal_I2C lcd(0x27, 16, 2); // Alamat I2C untuk LCD, 16 kolom dan 2 baris
const int buzzerPin = 27; // Pin buzzer
// Konfigurasi keypad
const byte ROWS = 4; // Empat baris
const byte COLS = 4; // Empat kolom
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {19, 18, 5, 17}; // Hubungkan ke pin baris keypad
byte colPins[COLS] = {16, 4, 0, 2}; // Hubungkan ke pin kolom keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputCode = "";
String correctCode = "290707"; // Kode yang benar untuk mengaktifkan ucapan ulang tahun
// Nada untuk lagu Happy Birthday
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
};
int noteDurations[] = {
4, 8, 4, 4, 4, 2,
4, 8, 4, 4, 4, 2,
4, 8, 4, 4, 4, 4, 4,
4, 8, 4, 4, 4, 2
};
void setup() {
Serial.begin(115200); // Memulai komunikasi serial
lcd.init(); // Inisialisasi LCD
lcd.backlight(); // Mengaktifkan backlight LCD
pinMode(buzzerPin, OUTPUT); // Set pin buzzer sebagai output
lcd.setCursor(0, 0); // Set posisi kursor LCD
lcd.print("Halo Cantikkk"); // Tampilkan pesan untuk memasukkan kode
lcd.setCursor(0, 1);
lcd.print("Tulis tgl lahir kamu");
}
void loop() {
char key = keypad.getKey(); // Baca input dari keypad
if (key) {
inputCode += key; // Tambahkan input ke string kode
lcd.setCursor(0, 2); // Set posisi kursor ke baris kedua
lcd.print(inputCode); // Tampilkan input yang dimasukkan
// Jika panjang input sudah 6 karakter
if (inputCode.length() == 6) {
if (inputCode == correctCode) {
lcd.clear(); // Bersihkan layar LCD
lcd.setCursor(0, 0); // Set posisi kursor LCD
lcd.print("Happy Birthday Abin!"); // Tampilkan ucapan ulang tahun
lcd.setCursor(0, 1);
lcd.print("Panjang Umur yaa:>");
playHappyBirthday(); // Mainkan lagu Happy Birthday
} else {
lcd.clear(); // Bersihkan layar LCD
lcd.setCursor(0, 0); // Set posisi kursor LCD
lcd.print("Salah Woi!"); // Tampilkan pesan kode salah
}
inputCode = ""; // Reset input kode
delay(2000); // Tunggu selama 2 detik
lcd.clear(); // Bersihkan layar LCD
lcd.setCursor(0, 0); // Set posisi kursor LCD
lcd.print("Lagi?"); // Tampilkan pesan untuk memasukkan kode
lcd.setCursor(0, 1);
lcd.print("Tulis tgl lahir kamu");
}
}
}
void playHappyBirthday() {
for (int thisNote = 0; thisNote < 26; thisNote++) {
int noteDuration = 1000 / noteDurations[thisNote];
tone(buzzerPin, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(buzzerPin);
}
}