#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize the LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); // Set the I2C address and the LCD dimensions
// Initialize the RTC module
RTC_DS1307 rtc;
// Define push button pins
const int hourButtonPin = 6;
const int minuteButtonPin = 7;
int setHour = 0; // Variable to hold the hour value
int setMinute = 0; // Variable to hold the minute value
const int buzzerPin = 8; // Digital pin connected to the buzzer
void setup() {
lcd.begin(16, 2); // Initialize the LCD dimensions (16x2)
lcd.backlight(); // Turn on the LCD backlight
pinMode(hourButtonPin, INPUT_PULLUP); // Set hour button pin as input with internal pull-up resistor
pinMode(minuteButtonPin, INPUT_PULLUP); // Set minute button pin as input with internal pull-up resistor
pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
Wire.begin();
rtc.begin();
// Uncomment the line below if you want to set the time initially
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
void loop() {
// Read the current time from the RTC module
DateTime currentTime = rtc.now();
// Check if the hour button is pressed
if (digitalRead(hourButtonPin) == LOW) {
// Increment the hour when the button is pressed
setHour = (setHour + 1) % 24;
// Wait a short delay to avoid multiple increments with a single press
delay(200);
}
// Check if the minute button is pressed
if (digitalRead(minuteButtonPin) == LOW) {
// Increment the minute when the button is pressed
setMinute = (setMinute + 1) % 60;
// Wait a short delay to avoid multiple increments with a single press
delay(200);
}
// Display the set time on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Time: ");
lcd.print(setHour);
lcd.print(":");
if (setMinute < 10) {
lcd.print("0");
}
lcd.print(setMinute);
// Compare the set time with the real-time on the RTC module
if (currentTime.hour() == setHour && currentTime.minute() == setMinute) {
lcd.setCursor(0, 1);
lcd.print("Time Match!");
// Activate the buzzer
digitalWrite(buzzerPin, HIGH);
} else {
lcd.setCursor(0, 1);
lcd.print("Not Matched");
// Deactivate the buzzer
digitalWrite(buzzerPin, LOW);
}
}