My Teensy3.5 seems to love the time 21.05.00 ... I first ran the code around that time yesterday and then disconnected. No battery back up is there for the Teensy3.5. But today I ran the same code around noon to update the time and it seems to be stuck on the time around 21.05 hours. I have two queries :
1. Why is it not updating as expected ? ( My PC Time is perfect )
2. In case i need to use the on board RTC in other code (after i provide a battery back up), which part of this code below should i port to the new code ? Meaning just read time without trying to set it.
1. Why is it not updating as expected ? ( My PC Time is perfect )
2. In case i need to use the on board RTC in other code (after i provide a battery back up), which part of this code below should i port to the new code ? Meaning just read time without trying to set it.
Code:
#include <TimeLib.h>
void setup()
{
setSyncProvider(getTeensy3Time); // Set the Time library to use Teensy 3.0's RTC to keep time
Serial.begin(115200);
while (!Serial); // Wait for Arduino Serial Monitor to open
delay(100);
if (timeStatus() != timeSet)
{
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void loop()
{
if (Serial.available())
{
time_t t = processSyncMessage();
if (t != 0)
{
Teensy3Clock.set(t); // Set the RTC
setTime(t);
}
}
digitalClockDisplay();
delay(1000);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void digitalClockDisplay() // Digital clock display of the time
{
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
//*************************************
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
//*************************************
// Code to process time sync messages from the serial port
#define TIME_HEADER "T" // Header tag for serial time sync message
unsigned long processSyncMessage()
{
unsigned long pctime = 0L;
const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
if (Serial.find(TIME_HEADER))
{
pctime = Serial.parseInt();
return pctime;
if ( pctime < DEFAULT_TIME) // Check the value is a valid time (greater than Jan 1 2013)
{
pctime = 0L; // Return 0 to indicate that the time is not valid
}
}
return pctime;
}
//*************************************
//Utility function for clock display: prints preceding colon and leading 0
void printDigits(int digits)
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}