/**********************
* REAL TIME CLOCK MODULE
* you can see the current date and timestamp displayed on the I2C LCD Module
***********************/
#include <Wire.h>
#include <DS1302.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <string.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
uint8_t RST_PIN = 5;
uint8_t SDA_PIN = 6;
uint8_t SCL_PIN = 7;
char buf[50];
char day[10];
String comdata = "";
int numdata[7] = { 0 }, j = 0, mark = 0;
DS1302 rtc(RST_PIN, SDA_PIN, SCL_PIN);
void print_time() {
Time t = rtc.time();
memset(day, 0, sizeof(day));
switch (t.day) {
case 1:
strcpy(day, "Sun");
break;
case 2:
strcpy(day, "Mon");
break;
case 3:
strcpy(day, "Tue");
break;
case 4:
strcpy(day, "Wed");
break;
case 5:
strcpy(day, "Thu");
break;
case 6:
strcpy(day, "Fri");
break;
case 7:
strcpy(day, "Sat");
break;
}
// Format the time and date and insert into the temporary buffer
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d", day, t.yr, t.mon, t.date, t.hr, t.min, t.sec);
Serial.println(buf);
lcd.setCursor(2, 0);
lcd.print(t.yr);
lcd.print("-");
lcd.print(t.mon / 10);
lcd.print(t.mon % 10);
lcd.print("-");
lcd.print(t.date / 10);
lcd.print(t.date % 10);
lcd.print(" ");
lcd.print(day);
lcd.setCursor(4, 1);
lcd.print(t.hr);
lcd.print(":");
lcd.print(t.min / 10);
lcd.print(t.min % 10);
lcd.print(":");
lcd.print(t.sec / 10);
lcd.print(t.sec % 10);
}
void setup() {
Serial.begin(9600);
rtc.write_protect(false);
rtc.halt(false);
lcd.init();
lcd.backlight();
Time t(2023, 2, 19, 20, 34, 50, 1);
// rtc.time(t);
}
void loop() {
while (Serial.available() > 0) {
comdata += char(Serial.read());
delay(2);
mark = 1;
}
if (mark == 1) {
Serial.print("You inputed: ");
Serial.println(comdata);
for (int i = 0; i < comdata.length(); i++) {
if (comdata[i] == ',' || comdata[i] == 0x10 || comdata[i] == 0x13) {
j++;
} else {
numdata[j] = numdata[j] * 10 + (comdata[i] - '0');
}
}
Time t(numdata[0], numdata[1], numdata[2], numdata[3], numdata[4], numdata[5], numdata[6]);
rtc.time(t);
mark = 0;
j = 0;
comdata = String("");
for (int i = 0; i < 7; i++) numdata[i] = 0;
}
print_time();
delay(1000);
}