#include <Wire.h>
#include <LiquidCrystal.h>
#include <RTClib.h>
// Initialize the RTC object
RTC_DS1307 rtc;
// Initialize the LCD object
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
// Initialize the push button pin
const int buttonPin = 2;
// Initialize a flag to check if the button was pressed
bool buttonPressed = false;
void setup() {
// Initialize the serial communication for debugging purposes
Serial.begin(9600);
// Initialize the I2C communication for the RTC module
Wire.begin();
// Set the time of the RTC module
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// Initialize the LCD module
lcd.begin(16, 2);
// Attach the interrupt function to the push button pin
attachInterrupt(digitalPinToInterrupt(buttonPin), clearLCD, RISING);
}
void loop() {
// Get the current time from the RTC module
DateTime now = rtc.now();
// Print the time to the LCD module
lcd.setCursor(0, 0);
if (now.hour() < 10) {
lcd.print("0");
}
lcd.print(now.hour(), DEC);
lcd.print(":");
if (now.minute() < 10) {
lcd.print("0");
}
lcd.print(now.minute(), DEC);
lcd.print(":");
if (now.second() < 10) {
lcd.print("0");
}
lcd.print(now.second(), DEC);
// Print a separator line
lcd.setCursor(0, 1);
lcd.print("----------------");
// Delay for 1 second to update the clock display
delay(1000);
// If the button was pressed, clear the LCD display
if (buttonPressed) {
lcd.clear();
buttonPressed = false;
}
}
void clearLCD() {
// Set the flag to indicate that the button was pressed
buttonPressed = true;
}