#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "pitches.h"
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Pin
const int pinR = 9;
const int pinG = 10;
const int pinB = 11;
const int pinBuzzer = 7;
const int pinButton = 2;
bool lastButtonState = HIGH;
bool isPlaying = false;
// Pesan panjang
String message = "I'm Sorry Zea. Please Forgive Me. Hari ku hampa tanpa mu. ";
int scrollIndex = 0;
unsigned long lastScrollTime = 0;
const int scrollDelay = 300; // kecepatan scroll
// Lagu "Die With A Smile"
int melody[] = {
NOTE_FS4, NOTE_FS4, NOTE_A4,
NOTE_FS4, NOTE_A4,
NOTE_FS4, NOTE_GS4, NOTE_GS4, NOTE_GS4, NOTE_A4, NOTE_B4,
NOTE_E4, NOTE_GS4,
NOTE_CS5, NOTE_GS4, NOTE_FS4,
NOTE_FS4, NOTE_FS4, NOTE_A4,
NOTE_FS4, NOTE_FS4, NOTE_A4,
NOTE_FS4, NOTE_E4, NOTE_GS4, NOTE_GS4, NOTE_GS4, NOTE_A4, NOTE_B4,
NOTE_E4, NOTE_GS4,
NOTE_CS5, NOTE_GS4, NOTE_FS4,
};
int durations[] = {
4, 4, 3,
4, 3,
4, 3, 8, 8, 4, 3,
4, 1,
4, 4, 1,
4, 4, 3,
4, 4, 3,
4, 3, 3, 8, 8, 4, 3,
4, 1,
4, 4, 1,
};
int noteIndex = 0;
unsigned long noteStartTime = 0;
int noteDuration = 0;
void setup() {
pinMode(pinR, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinBuzzer, OUTPUT);
pinMode(pinButton, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Tekan tombol ");
lcd.setCursor(0, 1);
lcd.print(" untuk mulai ");
}
void startSong() {
isPlaying = true;
scrollIndex = 0;
noteIndex = 0;
noteStartTime = millis();
noteDuration = (1000 / durations[noteIndex]) * 1.30;
}
void updateSong() {
unsigned long now = millis();
if (noteIndex < (sizeof(durations) / sizeof(int))) {
if (now - noteStartTime >= (unsigned long)noteDuration) {
// Nada berikutnya
noteIndex++;
if (noteIndex < (sizeof(durations) / sizeof(int))) {
int duration = 1000 / durations[noteIndex];
tone(pinBuzzer, melody[noteIndex], duration);
// Efek lampu RGB
if (noteIndex % 3 == 0) {
digitalWrite(pinR, HIGH);
digitalWrite(pinG, LOW);
digitalWrite(pinB, LOW);
} else if (noteIndex % 3 == 1) {
digitalWrite(pinR, LOW);
digitalWrite(pinG, HIGH);
digitalWrite(pinB, LOW);
} else {
digitalWrite(pinR, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinB, HIGH);
}
noteDuration = duration * 1.30;
noteStartTime = now;
} else {
// Lagu selesai
noTone(pinBuzzer);
digitalWrite(pinR, LOW);
digitalWrite(pinG, LOW);
digitalWrite(pinB, LOW);
isPlaying = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Hai Zea");
lcd.setCursor(0, 1);
lcd.print("I'm Sorry");
}
}
}
}
void updateScroll() {
unsigned long now = millis();
if (now - lastScrollTime >= scrollDelay) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message.substring(scrollIndex, scrollIndex + 16));
lastScrollTime = now;
scrollIndex++;
if (scrollIndex > message.length()) {
scrollIndex = 0;
}
}
}
void loop() {
bool currentState = digitalRead(pinButton);
if (lastButtonState == HIGH && currentState == LOW) {
startSong(); // mulai lagu + teks
}
if (isPlaying) {
updateSong();
updateScroll();
}
lastButtonState = currentState;
}