#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address (0x27) accordingly
const int buttonHour = 2; // Pin for setting the hour
const int buttonMinute = 3; // Pin for setting the minute
int alarmHour = 15; // Set your desired alarm time
int alarmMinute = 29;
void setup() {
Serial.begin(9600);
Wire.begin(); // Initialize I2C communication
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// Uncomment the following line if you want to set the time on startup
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
lcd.begin(16, 2);
lcd.print("Alarm Clock");
delay(2000);
lcd.clear();
pinMode(buttonHour, INPUT_PULLUP);
pinMode(buttonMinute, INPUT_PULLUP);
}
void loop() {
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0, 1);
lcd.print("Alarm: ");
lcd.print(alarmHour, DEC);
lcd.print(':');
lcd.print(alarmMinute, DEC);
if (now.hour() == alarmHour && now.minute() == alarmMinute) {
lcd.clear();
lcd.print("Good Morning!");
delay(5000); // Display the message for 5 seconds
lcd.clear();
}
// Check if buttons are pressed to set alarm time
if (digitalRead(buttonHour) == LOW) {
delay(200); // Debounce
alarmHour = (alarmHour + 1) % 24;
}
if (digitalRead(buttonMinute) == LOW) {
delay(200); // Debounce
alarmMinute = (alarmMinute + 1) % 60;
}
delay(1000); // Update every second
}