#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
const int segA = 2;
const int segB = 3;
const int segC = 4;
const int segD = 5;
const int segE = 6;
const int segF = 7;
const int segG = 8;
const int buzzerPin = 9;
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, 2,
4, 8, 4, 4, 4, 2
};
// Tabel tampilan 7-segment untuk angka 3, 2, 1, 0
const byte digit[4] = {
0b0111111, // 0
0b0000110, // 1
0b1011011, // 2
0b1001111 // 3
};
unsigned long previousMillisBuzzer = 0;
unsigned long previousMillisScroll = 0;
unsigned long intervalBuzzer = 500;
unsigned long intervalScroll = 200;
int currentNote = 0;
bool buzzerPlaying = false;
void displayDigit(int number) {
byte segments = digit[number];
digitalWrite(segA, segments & 0b00000001);
digitalWrite(segB, segments & 0b00000010);
digitalWrite(segC, segments & 0b00000100);
digitalWrite(segD, segments & 0b00001000);
digitalWrite(segE, segments & 0b00010000);
digitalWrite(segF, segments & 0b00100000);
digitalWrite(segG, segments & 0b01000000);
}
void setup() {
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.init();
lcd.backlight();
for (int i = 3; i >= 0; i--) {
displayDigit(i);
delay(1000);
}
buzzerPlaying = true;
}
void loop() {
unsigned long currentMillis = millis();
if (buzzerPlaying) {
if (currentMillis - previousMillisBuzzer >= intervalBuzzer) {
previousMillisBuzzer = currentMillis;
int noteDuration = 1000 / noteDurations[currentNote];
tone(buzzerPin, melody[currentNote], noteDuration);
delay(noteDuration);
currentNote++;
if (currentNote >= sizeof(melody) / sizeof(melody[0])) {
buzzerPlaying = false;
noTone(buzzerPin);
}
}
}
if (currentMillis - previousMillisScroll >= intervalScroll) {
previousMillisScroll = currentMillis;
lcd.setCursor(15, 0);
//012345678912345
lcd.print("Yaumul Milad ");
lcd.setCursor(15, 1);
lcd.print("Barakallahu Fii Umrik");
lcd.scrollDisplayLeft();
}
if (!buzzerPlaying && currentMillis - previousMillisScroll >= intervalScroll) {
previousMillisScroll = currentMillis;
lcd.scrollDisplayLeft();
}
}