#include <Wire.h>
#include <RTClib.h>
#include <LiquidCrystal_I2C.h>
// Initialize the RTC and LCD. Adjust the LCD address as needed (commonly 0x27 or 0x3F)
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27,16,2);
const int buzzerPin = 8;
// Set your desired alarm time here
const int alarmHour = 7;
const int alarmMinute = 30;
RTC_DS3231 rtc;
void setup() {
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
// This line sets the RTC with an explicit date & time, for example, January 1, 2024, at 00:00:00
rtc.adjust(DateTime(2024, 1, 1, 0, 0, 0));
}
void setup() {
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// The following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Time: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.setCursor(0,1);
lcd.print("Alarm: ");
lcd.print(alarmHour);
lcd.print(':');
lcd.print(alarmMinute);
// Check if current time matches alarm time
if (now.hour() == alarmHour && now.minute() == alarmMinute) {
// Sound the alarm for 60 seconds or until a condition to stop it is met
for(int i = 0; i < 60; i++) {
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
delay(500);
// Here, you can check for a button press to stop the alarm
// Example: if (digitalRead(stopButtonPin) == HIGH) break;
}
}
// Avoid constantly triggering the alarm when the time is right.
delay(60000); // Wait a minute before checking again
}