#include <Wire.h>
#include <RtcDS3231.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
RtcDS3231<TwoWire> Rtc(Wire);
uint8_t month, day, hour, minute, second;
uint16_t year;
void setup()
{
Serial.begin(57600);
lcd.begin(16, 2);
lcd.backlight();
lcd.setCursor(0, 0);
/*********************** RTC Setting *********************/
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
RtcDateTime now = Rtc.GetDateTime();
// Set the RTC to the current time if it is behind the compile time
if (now < compiled) Rtc.SetDateTime(compiled);
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone);
/*********************************************************/
}
void loop()
{
/**********************************/
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
delay(1000); // Update the time every second
/**********************************/
}
void printDateTime(const RtcDateTime& dt)
{
year = dt.Year();
month = dt.Month();
day = dt.Day();
hour = dt.Hour();
minute = dt.Minute();
second = dt.Second();
// Create a formatted string with the date and time
String str = String(day) +
"/" + String(month) +
"/" + String(year) +
" " + String(hour) +
":" + String(minute) +
":" + String(second);
// Print to Serial Monitor
Serial.println(str);
// Display the date and time on the LCD
lcd.clear(); // Clear the previous display
lcd.setCursor(0, 0); // Set cursor to the first line
lcd.print(day);
lcd.print('/');
lcd.print(month);
lcd.print('/');
lcd.print(year);
lcd.setCursor(0, 1); // Set cursor to the second line
lcd.print(hour);
lcd.print(':');
lcd.print(minute);
lcd.print(':');
lcd.print(second);
}