/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/
#include <Time.h> //include Arduino Time library
// my variabls for time
int myHr, myMin, mySec, myDay, myMonth, myYr;
boolean oneTime = true; //
// ***********************************************************************************************************
// *
// * Power Up Init.
// * put your setup code here, to run once:
// *
// ***********************************************************************************************************
void setup() {
// Set the Arduino Time library to Sync from Teensy 3.x's RTC
setSyncProvider(getTeensy3Time);
setSyncInterval(60); // 1 minute / Configure how often the getTimeFunction is called in seconds.
setTime(getTeensy3Time()); // Set Arduino Time library with Teensy 3.x RTC time.
Serial.begin(115200);
// while (!Serial); // Wait for Arduino Serial Monitor to open
delay(2000); // Wait 2 sec
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
} else {
Serial.println("RTC has set the system time");
}
}
// ***********************************************************************************************************
// *
// * Main Loop
// * put your main code here, to run repeatedly:
// *
// ***********************************************************************************************************
void loop() {
//oneTime_set_option(); //One Time Set Option
// Check for a time message from your computer, This message is sent by a Processing-based program,
// found in Time's examples/Processing folder. https://processing.org/
USBtime_set_option(); //Check for a time message.
digitalClockDisplay(); //Display Clock
// put your main code here, to run repeatedly:
delay(1000); //<----- Remove delay l8r.
}
// ***********************************************************************************************************
// *
// * One Time Set Option
// *
// *
// ***********************************************************************************************************
void oneTime_set_option() {
if (oneTime == true) {
oneTime = false;
Serial.println("Setting new time");
myHr = 10;
myMin = 15;
mySec = 15;
myDay = 20;
myMonth = 5;
myYr = 2020; // year 2020
// This sets the system time (NOT the Teensy RTC Clock)
// set your seperated date/time variables out as normal and update system time FIRST
//setTime(hour(), minute(), second(), day(), month(), year())
setTime(myHr, myMin, mySec, myDay, myMonth, myYr);
// now lets set the RTC in teensy before exiting.
// now we can use the system time to update the Teensy's RTC bits
// This sets the Teensy RTC Clock from Arduino Time library(system time) - epoch stylee, just like it wants :)
Teensy3Clock.set(now());
}
}
// ***********************************************************************************************************
// *
// * Display Digital Clock
// *
// *
// ***********************************************************************************************************
void digitalClockDisplay() {
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
//isAM(); // Returns true if time now is AM
//isPM(); // Returns true if time now is PM
if (isAM() == true) {
Serial.print(".AM");
}
else {
Serial.print(".PM");
}
Serial.print(" "); // space
switch (weekday()) // Day of the week, Sunday is day 1
{
case 1:
Serial.print("Sunday");
break;
case 2:
Serial.print("Monday");
break;
case 3:
Serial.print("Tuesday");
break;
case 4:
Serial.print("Wednesday");
break;
case 5:
Serial.print("Thursday");
break;
case 6:
Serial.print("Friday");
break;
case 7:
Serial.print("Saturday");
break;
} //end of switch case
Serial.print(" "); // space
Serial.print(day());
Serial.print(" "); // space
Serial.print(month());
Serial.print(" "); // space
Serial.print(year());
Serial.println();
}
// ***********************************************************************************************************
// *
// * utility function for digital clock display: prints preceding colon and leading 0
// *
// *
// ***********************************************************************************************************
void printDigits(int digits) {
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
// ***********************************************************************************************************
// *
// * Read Teensy 3.x time from Teensy 3.x internal RTC
// *
// *
// ***********************************************************************************************************
time_t getTeensy3Time()
{
return Teensy3Clock.get();
}
// ***********************************************************************************************************
// * USB time set option using your computer's time.
// * TimeSerial listens for a message from your computer. This message is sent by a Processing-based program,
// * found in Time's examples/Processing folder.
// ***********************************************************************************************************
void USBtime_set_option() {
if (Serial.available()) {
time_t t = processSyncMessage();
if (t != 0) {
Teensy3Clock.set(t); // set the internal RTC in teensy
setTime(t); // set the Arduino Time library
}
}
}
// ***********************************************************************************************************
// *
// * code to process time sync messages from the serial port
// *
// *
// ***********************************************************************************************************
//#define TIME_HEADER "T" // Header tag for serial time sync message
char TIME_HEADER[] = "T"; // fix / warning: deprecated conversion from string constant to 'char*'
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;
}