Components:
2004 LCD
1302 RTC
Pro Micro
The date and time have been working for a while now but suddenly it changed and only displays 17:30, 29/04/2043, which is nowhere close to the actual time and date. What happened to make it changed? The only thing I've done recently is change the usb cable used to provide power to the pro micro.
Here is the code:
#include <ThreeWire.h>
#include <RtcDS1302.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
ThreeWire myWire(A2, A0, A1); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27
byte smile[8] =
{
B00000,
B00000,
B01010,
B00000,
B10001,
B01110,
B00000,
B00000
};
void setup(){
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid()) {
// Common Causes: 1) first time you ran and the device wasn't running yet 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected()) {
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning()) {
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled) {
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
Serial.println("RTC is newer than compile time. (this is expected)");
else
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
lcd.init(); //initialize the lcd
lcd.backlight();
lcd.createChar(1, smile);
lcd.setCursor ( 0, 2 );
lcd.write(1);
lcd.setCursor (2, 2);
lcd.print("You've got this!");
lcd.setCursor (19, 2);
lcd.write(1);
lcd.setCursor ( 1, 3 );
lcd.print("Fruit/Veg? Vit D?");
}
/*********************************************************/
void loop() {
RtcDateTime now = Rtc.GetDateTime();
now -= 60;
printDateTime(now);
if (!now.IsValid()) {
// Common Causes: 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay(1000);
}
#define countof(a) (sizeof(a) / sizeof(a[0]))
void printDateTime(const RtcDateTime& dt) {
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u"),
dt.Day(), dt.Month(), dt.Year());
char timestring[20];
snprintf_P(timestring,
countof(timestring),
PSTR("%02u:%02u"),
dt.Hour(), dt.Minute());
int dow = dt.DayOfWeek();
switch(dow){
case 1:
lcd.setCursor(17,0);
lcd.print("Mon");
break;
case 2:
lcd.setCursor(17,0);
lcd.print("Tue");
break;
case 3:
lcd.setCursor(17,0);
lcd.print("Wed");
break;
case 4:
lcd.setCursor(17,0);
lcd.print("Thu");
break;
case 5:
lcd.setCursor(17,0);
lcd.print("Fri");
break;
case 6:
lcd.setCursor(17,0);
lcd.print("Sat");
break;
case 0:
lcd.setCursor(17,0);
lcd.print("Sun");
break;}
lcd.setCursor ( 7, 0 );
lcd.print(timestring);
lcd.setCursor (5, 1);
lcd.print(datestring);
if (int(dt.Hour()) <= 07){
lcd.noBacklight();
}
}