#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Wire.begin();
rtc.begin();
lcd.init();
lcd.backlight();
// Uncomment the following line to set the time only once during setup.
// rtc.adjust(DateTime(__DATE__, __TIME__));
Serial.begin(9600);
Serial.println("RTC Initialized.");
}
void loop() {
DateTime now = rtc.now();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(formatTime(now.hour(), false)); // Display in 12-hour format with AM/PM
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.print(" ");
lcd.print(getAMPM(now.hour())); // Display AM or PM
lcd.setCursor(0, 1);
lcd.print("Date: ");
lcd.print(now.day(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.year(), DEC);
//Uncomment These Lines If you want it to display the time in the "Console"
//Serial.print("Current Date/Time: ");
//Serial.print(now.year(), DEC);
//Serial.print('/');
//Serial.print(now.month(), DEC);
//Serial.print('/');
//Serial.print(now.day(), DEC);
//Serial.print(' ');
//Serial.print(formatTime(now.hour(), false)); // Display in 12-hour format with AM/PM
//Serial.print(':');
//Serial.print(now.minute(), DEC);
//Serial.print(':');
//Serial.print(now.second(), DEC);
//Serial.print(" ");
//Serial.print(getAMPM(now.hour())); // Display AM or PM
//Serial.println();
delay(690); // Update Every 0.69 Seconds.
}
String formatTime(int hours, bool leadingZero) {
if (hours == 0) {
return (leadingZero ? "12" : " 12");
} else if (hours > 0 && hours <= 9) {
return (leadingZero ? "0" : " ") + String(hours);
} else if (hours >= 10 && hours <= 12) {
return String(hours);
} else {
return (leadingZero ? "0" : " ") + String(hours - 12);
}
}
String getAMPM(int hours) {
return (hours >= 12) ? "PM" : "AM";
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5
rtc1:GND
rtc1:5V
rtc1:SDA
rtc1:SCL
rtc1:SQW
lcd1:GND
lcd1:VCC
lcd1:SDA
lcd1:SCL