I am using the code below to set the RTC on my Teensy 3.5 and also using a 3V cell to maintain time. All well. But I just wanted to clarify two points :
1. It receives the T variable from the PC. How does that happen as I never installed Processing in it. Or is it native to Win10 PC ?
2. Having synchronized once and the time loaded to the RTC, I am blocking further Sync attempts by setting a Boolean in setup ... please see the "enableTimeSetting" boolean in the code below. Is this OK to use like this ? ( Otherwise the RTC will be set for every loop ? )
1. It receives the T variable from the PC. How does that happen as I never installed Processing in it. Or is it native to Win10 PC ?
2. Having synchronized once and the time loaded to the RTC, I am blocking further Sync attempts by setting a Boolean in setup ... please see the "enableTimeSetting" boolean in the code below. Is this OK to use like this ? ( Otherwise the RTC will be set for every loop ? )
Code:
#include <TimeLib.h>
bool enableTimeSetting ; // To flag if we need to Synch time...
//*********************************************
void setup()
{
setSyncProvider(getTeensy3Time); // Set the Time library to use Teensy 3.5's RTC to keep time
Serial.begin(9600);
while (!Serial); // Wait for Arduino Serial Monitor to open
delay(100);
if (timeStatus() != timeSet)
{
Serial.println("Unable to sync with the RTC");
enableTimeSetting = true ;
}
else
{
Serial.println("RTC has set the system time");
enableTimeSetting = false ;
}
}
//*********************************************
void loop()
{
if (Serial.available()&& enableTimeSetting )
{
time_t t = processSyncMessage(); // Try to synch with PC clock..
if (t != 0)
{
Teensy3Clock.set(t); // Set the RTC
setTime(t);
}
enableTimeSetting = false; // Stop the PC time synch too often..
}
digitalClockDisplay();
delay(1000);
}
//************* FUNCTIONS **********************
void digitalClockDisplay()
{
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;
}
//...............................................
void printDigits(int digits) // Utility clock display: prints preceding colon and leading 0
{
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}