/*#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize the LCD with the I2C address (usually 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the RTC
RTC_DS1307 rtc;
void setup() {
// Start the serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.init();//begin ไม่ถูก
lcd.backlight();
// Check if the RTC is connected properly
if (!rtc.begin()) {
lcd.clear();
lcd.print("Couldn't find RTC");
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC is running, if not, set the time
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Uncomment the next line to set the RTC to the current time
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// Get the current date and time
DateTime now = rtc.now();
// Clear the LCD before displaying new information
lcd.clear();
// Display the date in format: YYYY-MM-DD
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
// Display the time in format: HH:MM:SS
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
// Wait for 1 second before refreshing the display
delay(1000);
}
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <RTClib.h>
// Initialize the LCD with the I2C address (usually 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Initialize the RTC
RTC_DS1307 rtc;
// Function to get the name of the day of the week
String getDayOfWeek(uint8_t dayOfWeek) {
switch (dayOfWeek) {
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 "";
}
}
void setup() {
// Start the serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.init();//ต้องแก้จากbeginเป็นinit
lcd.backlight();
// Check if the RTC is connected properly
if (!rtc.begin()) {
lcd.clear();
lcd.print("Couldn't find RTC");
Serial.println("Couldn't find RTC");
while (1);
}
// Check if the RTC is running, if not, set the time
if (!rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// Uncomment the next line to set the RTC to the current time
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// Get the current date and time
DateTime now = rtc.now();
// Clear the LCD before displaying new information
lcd.clear();
// Display the date in format: YYYY-MM-DD
lcd.setCursor(0, 0);
lcd.print(now.year(), DEC);
lcd.print('/');
lcd.print(now.month(), DEC);
lcd.print('/');
lcd.print(now.day(), DEC);
// Display the day of the week
lcd.setCursor(10, 0); // Adjusted position to fit in the first row
lcd.print(getDayOfWeek(now.dayOfTheWeek()));
// Display the time in format: HH:MM:SS
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
// Wait for 1 second before refreshing the display
delay(1000);
}