#include <RTClib.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
RTC_DS3231 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4); // Set the LCD I2C address to 0x27 for a 20 chars and 4 line display
char t[40]; // Increased the size to accommodate the additional characters for the day of the week
float temperature;
void setup()
{
Serial.begin(9600);
Wire.begin();
lcd.begin(20, 4); // Initialize the LCD display with 20 columns and 4 rows
lcd.backlight();
rtc.begin();
rtc.adjust(DateTime(2024, 4, 3, 17, 35, 56)); // Set the desired date and time
}
void loop()
{
DateTime now = rtc.now();
sprintf(t, "%02d:%02d:%02d %02d/%02d/%02d ", now.hour(), now.minute(), now.second(), now.day(), now.month(), now.year());
// Add the day of the week to the string
strcat(t, getDayOfWeek(now.dayOfTheWeek()));
lcd.setCursor(0, 0);
lcd.print("Date/Time: ");
lcd.setCursor(0, 1);
lcd.print(t);
temperature = rtc.getTemperature();
lcd.setCursor(0, 2);
lcd.print("Temperature: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 3);
delay(1000);
}
// Function to get the day of the week as a string
const char* getDayOfWeek(int d) {
switch(d) {
case 0: return "Sun";
case 1: return "Mon";
case 2: return "Tue";
case 3: return "Wed";
case 4: return "Thu";
case 5: return "Fri";
case 6: return "Sat";
default: return "Error";
}
}