#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Adjust the address if needed
const int buzzerPin = 8;
unsigned long previousMillis = 0;
const long interval = 10000; // 10 seconds
bool showDate = true;
void setup() {
Wire.begin();
rtc.begin();
lcd.begin(16, 2); // Initialize the LCD with 16 columns and 2 rows
lcd.backlight();
pinMode(buzzerPin, OUTPUT);
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
Serial.begin(9600);
}
void loop() {
DateTime now = rtc.now();
int hour = now.hour();
String period = "AM";
if (hour >= 12) {
period = "PM";
if (hour > 12) {
hour -= 12;
}
} else if (hour == 0) {
hour = 12;
}
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(hour);
lcd.print(':');
lcd.print(now.minute());
lcd.print(':');
lcd.print(now.second());
lcd.setCursor(14, 0); // Set cursor to the 15th position
lcd.print(period);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
showDate = !showDate;
lcd.setCursor(0, 1);
lcd.print(" "); // Clear the second row
if (showDate) {
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day());
lcd.print('/');
lcd.print(now.month());
lcd.print('/');
lcd.print(now.year());
} else {
lcd.setCursor(0, 1);
lcd.print("DAY: ");
lcd.print(dayOfWeek(now.dayOfTheWeek()));
}
}
if (now.hour() == 5 && now.minute() == 0 && now.second() == 0) {
if (now.dayOfTheWeek() >= 1 && now.dayOfTheWeek() <= 5) {
triggerAlarm();
}
}
delay(1000);
}
void triggerAlarm() {
Serial.println("Alarm! It's 5 AM!");
for (int i = 0; i < 10; i++) { // Buzzer sounds for 10 seconds
digitalWrite(buzzerPin, HIGH);
delay(10000);
digitalWrite(buzzerPin, LOW);
delay(10000);
}
}
String dayOfWeek(int day) {
switch (day) {
case 0: return "LINGGO";
case 1: return "LUNES";
case 2: return "MARTES";
case 3: return "MIYERKULES";
case 4: return "HUWEBES";
case 5: return "BIYERNES";
case 6: return "SABADO";
default: return "";
}
}