#define LED_Pin 33
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
void setup() {
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
while (1) delay(10);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running, let's set the time!");
// When time needs to be set on a new device, or after a power loss, the
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
lcd.init();
lcd.clear();
lcd.backlight();
lcd.setCursor(5,0);
lcd.print("NUTTAPOL");
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(LED_Pin, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
int YEAR,MONTH,DAY,HOUR,MIN,SEC;
YEAR = now.year();
MONTH = now.month();
DAY = now.day();
HOUR = now.hour();
MIN = now.minute();
SEC = now.second();
lcd.setCursor(1,2);
lcd.print(DAY); lcd.print("/");
lcd.print(MONTH); lcd.print("/");
lcd.print(YEAR); lcd.print(" ");
lcd.print(HOUR); lcd.print(":");
lcd.print(MIN); lcd.print(":");
lcd.print(SEC);
Serial.print(YEAR, DEC);
Serial.print('/');
Serial.print(MONTH, DEC);
Serial.print('/');
Serial.print(DAY, DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(HOUR, DEC);
Serial.print(':');
Serial.print(MIN, DEC);
Serial.print(':');
Serial.print(SEC, DEC);
Serial.println();
digitalWrite(LED_Pin,!digitalRead(LED_Pin));
delay(1000);
}