#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
// Inisialisasi RTC dan LCD
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4); // Alamat I2C LCD
// Definisi nada
#define C4 262
#define CS4 277
#define D4 294
#define E4 330
#define F4 349
#define G4 392
#define A4 440
#define AS4 466
#define B4 494
#define C5 523 // Nada bel (C5) yang tinggi
#define D5 587
#define E5 659
#define F5 698
#define G5 784
#define A5 880
#define B5 988 // Nada yang lebih tinggi
// Melodi Indonesia Raya
int indoRayaMelody[] = {
C4, D4, E4, C5, 0, C5, AS4, AS4, G4, E4, 0, E4, E4, F4, E4, D4,
C4, B4, 0, B4, C4, D4, AS4, 0, AS4, G4, G4, F4, E4, 0, E4, E4,
G4, F4, E4, D4, C4, 0, C4, D4, E4, C5, 0, C5, AS4, AS4, G4, E4,
0, E4, E4, F4, E4, G4, AS4, G4, F4, E4, E4, E4, C5, C5, B4, AS4,
G4, E4, 0, E4, E4, F4, E4, D4, 0, C4, D4, E4, C5, 0, C5,
AS4, AS4, G4, E4, 0, E4, E4, F4, E4, D4, 0, C4, C4, D4, C4, 0
};
int indoRayaDurations[] = {
468, 156, 624, 624, 468, 156, 468, 156, 624, 624, 624, 468, 156, 624, 624,
624, 624, 1248, 624, 468, 156, 624, 624, 468, 156, 468, 156, 624, 624,
624, 468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 468,
156, 468, 156, 624, 624, 624, 468, 156, 624, 624, 624, 624, 1248, 624,
468, 156, 624, 624, 624, 624, 1248, 624, 468, 156, 624, 624, 624, 624,
1248, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 624,
468, 156, 624, 468, 156, 1248, 624, 468, 156, 624, 468, 156, 624,
468, 156, 624, 468, 156, 624, 468, 156, 624, 468, 156, 1248, 1248,
0
};
// Melodi Tanah Airku
int tanahAirkuMelody[] = {
0, G4, E4, F4, G4, C5, E5, D5, C5, B4, A4, G4,
0, G4, C5, E5, D5, C5, B4, B4, A4, B4, C5,
0, C5, C5, B4, A4, A4, C5, B4, A4, G4,
0, E4, F4, G4, B4, A4, A4, D4, E4, F4, E4,
0, G4, C5, B4, A4, A4, D5, E5, F5,
A4, G4, C5, B4, D5, C5
};
int tanahAirkuDurations[] = {
500, 250, 250, 250, 500,
1000, 250, 250, 250, 250,
500, 0, 500, 250, 250,
500, 0, 500, 250, 250,
250, 250, 500, 0,
500, 250, 250, 250,
250, 500, 0,
500, 250, 250,
0
};
unsigned long previousMillis = 0; // Untuk penghitungan waktu
bool isPlayingBell = false;
bool isPlayingMelody = false;
int currentNote = 0;
int currentMelody = 0;
void setup() {
pinMode(11, OUTPUT); // Pin untuk speaker
rtc.begin(); // Memulai RTC
lcd.begin(20, 4); // Memulai LCD
lcd.backlight(); // Menyalakan backlight LCD
// Jika RTC kehilangan daya, atur waktu pada saat kompilasi
if (rtc.lostPower()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // Mengatur waktu RTC
}
// Tampilkan nama di LCD
lcd.setCursor(0, 0);
lcd.print("Nama: Vika Septia");
}
void loop() {
DateTime now = rtc.now();
// Tampilkan jam di LCD
lcd.setCursor(0, 1);
lcd.print("Jam: ");
lcd.print(now.hour());
lcd.print(":");
if (now.minute() < 10) lcd.print("0"); // Tambahkan nol di depan jika perlu
lcd.print(now.minute());
// Jam 06:30 - Bel Masuk Sekolah
if (now.hour() == 6 && now.minute() == 30 && !isPlayingBell) {
isPlayingBell = true;
lcd.setCursor(0, 2);
lcd.print("Bel Masuk Sekolah");
playBell();
}
// Jam 07:00 - Lagu: Indonesia Raya
if (now.hour() == 7 && now.minute() == 0 && !isPlayingMelody) {
currentMelody = 1; // Menandakan Indonesia Raya
isPlayingMelody = true;
currentNote = 0; // Reset nada
previousMillis = millis(); // Atur waktu awal
lcd.setCursor(0, 3);
lcd.print("Main: Indonesia Raya");
if (indoRayaMelody[currentNote] > 0) {
tone(11, indoRayaMelody[currentNote]); // Mainkan nada pertama
}
}
// Jam 14:50 - Bel Pulang Sekolah
if (now.hour() == 14 && now.minute() == 50 && !isPlayingBell) {
isPlayingBell = true;
lcd.setCursor(0, 2);
lcd.print("Bel Pulang Sekolah");
playBell();
}
// Jam 14:53 - Lagu: Tanah Airku
if (now.hour() == 14 && now.minute() == 53 && !isPlayingMelody) {
currentMelody = 2; // Menandakan Tanah Airku
isPlayingMelody = true;
currentNote = 0; // Reset nada
previousMillis = millis(); // Atur waktu awal
lcd.setCursor(0, 3);
lcd.print("Main: Tanah Airku");
if (tanahAirkuMelody[currentNote] > 0) {
tone(11, tanahAirkuMelody[currentNote]); // Mainkan nada pertama
}
}
// Memainkan nada secara non-blocking
unsigned long currentMillis = millis();
if (isPlayingMelody) {
int duration = (currentMelody == 1) ? indoRayaDurations[currentNote] : tanahAirkuDurations[currentNote];
if (currentMillis - previousMillis >= duration) {
previousMillis = currentMillis;
// Hentikan nada sebelumnya
noTone(11);
// Pindah ke nada berikutnya
currentNote++;
if (currentMelody == 1 && currentNote >= sizeof(indoRayaMelody) / sizeof(indoRayaMelody[0])) {
isPlayingMelody = false; // Selesai memainkan melodi
currentNote = 0; // Reset untuk pemutaran berikutnya
} else if (currentMelody == 2 && currentNote >= sizeof(tanahAirkuMelody) / sizeof(tanahAirkuMelody[0])) {
isPlayingMelody = false; // Selesai memainkan melodi
currentNote = 0; // Reset untuk pemutaran berikutnya
} else {
// Mainkan nada baru
if ((currentMelody == 1 && indoRayaMelody[currentNote] > 0) || (currentMelody == 2 && tanahAirkuMelody[currentNote] > 0)) {
tone(11, (currentMelody == 1 ? indoRayaMelody[currentNote] : tanahAirkuMelody[currentNote]));
}
}
}
}
// Memainkan bel secara non-blocking
if (isPlayingBell) {
// Durasi bel selama 1000 ms
if (currentMillis - previousMillis >= 1000) {
noTone(11);
isPlayingBell = false; // Selesai membunyikan bel
}
}
}
void playBell() {
tone(11, C5, 1000); // Nada bel (C5) selama 1000 ms
delay(100); // Tambahkan delay kecil untuk memberikan efek
tone(11, B5, 1000); // Nada tambahan (B5) selama 1000 ms
}