I'm working with a very simple RTC Time/Date sketch that works perfect after loaded, and when powered continuously after the load. I can plug in a power supply before unplugging the USB cable, and everything continues to operate. But if the power is removed, it restarts to a blank back lit LCD display, with the heartbeat LED blinking.
I'm feeling there is an LCD issue.
The odd part is, I must load a different RTC date/time sketch, then after that, reload of the below date-time sketch for it to operate. Reloading the same sketch will not restore operation.
See below non-restarting sketch.
Any advice greatly appreciated.
#include <Wire.h>
//#include "RTClib.h"
#include <TimeLib.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
#define LEDBLINK_PIN 13
#define LEDBLINK_MS 1000
void setup() {
lcd.begin();
lcd.backlight();
lcd.setCursor(0, 0);
pinMode(LEDBLINK_PIN, OUTPUT);
}
void loop() {
tmElements_t tm;
ledBlink();
if (RTC.read(tm)) {
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("TIME: ");
lcd.setCursor(6,0); //was commented
print2digits(tm.Hour);
lcd.print(':');
print2digits(tm.Minute);
lcd.print(':');
print2digits(tm.Second);
lcd.setCursor(0,1); //Start at character 0 on line 1
lcd.print("DATE: ");
lcd.print(tm.Month);
lcd.print('/');
lcd.print(tm.Day);
lcd.print('/');
lcd.print(tmYearToCalendar(tm.Year));
}
delay(990);
}
void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.print('0');
}
lcd.print(number);
}
void ledBlink() // Heartbeat LED
{
static unsigned int ledStatus = LOW; // Last set LED mode.
static unsigned long ledBlinkTime = 0; // LED blink time.
if ( (long)(millis()-ledBlinkTime) >= 0 )
{
// Toggle LED.
ledStatus = (ledStatus==HIGH ? LOW : HIGH);
// Set LED pin status.
digitalWrite(LEDBLINK_PIN, ledStatus);
// Reset "next time to toggle" time.
ledBlinkTime = millis()+LEDBLINK_MS;
}
}