#include <TimeLib.h> // https://www.pjrc.com/teensy/td_libs_Time.html
elapsedMillis sincePrint;
int ledState = LOW; // ledState used to set the LED
// int Led = LED_BUILTIN; //13; // Teensy 3.x
int Hr;
int Min;
int Sec;
int Dy;
int Mnth;
int Yr;
// **********************************************************************************
// * setup ()
// **********************************************************************************
void setup() {
// put your setup code here, to run once:
pinMode(LED_BUILTIN, OUTPUT);
setSyncInterval(59); // 1 minute / Configure how often the getTimeFunction is called in seconds.
Serial.begin(115200); // Init the serial port only if needed
// NOTE: On Teensy Serial.begin(baud) is completely ignored.
// Unlike a standard Arduino (FTDI IC), the Teensy 3.x Serial object always communicates at 12 Mbit/sec USB speed.
// Unlike a standard Arduino (FTDI IC), the Teensy 4.x Serial object always communicates at 480 Mbit/sec USB 2.0 Hi-Speed.
while (!Serial && millis() < 2000) {} // wait for Serial Monitor with timeout 2 sec.
if (timeStatus() != timeSet) {
Serial.println("Unable to sync with the RTC");
Serial.println("Seting Arduino Time library now");
Dy = 1;
Mnth = 5;
Yr = 2021;
Hr = 21;
Min = 20;
Sec = 0;
[COLOR="#FF0000"]setTime( Hr, Min, Sec, Dy, Mnth, Yr);[/COLOR] // Set Arduino Time library.
Serial.println("Seting the Teensy RTC Clock from Arduino Time library");
// 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 :)
[COLOR="#FF0000"]Teensy3Clock.set(now());[/COLOR]
Serial.println();
} else {
Serial.println("RTC has set the system time");
Serial.println();
}
}
// **********************************************************************************
// * loop ()
// **********************************************************************************
void loop() {
// put your main code here, to run repeatedly:
DisplayTime();
}
// **********************************************************************************
// * DisplayTime ()
// **********************************************************************************
void DisplayTime() {
if (sincePrint >= 1000) { // "sincePrint" auto-increases
sincePrint = 0;
//digitalToggle(13); // Teensyduino 1.53 AND UP
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
Serial.print("Teensy Unix time (also known as Epoch time) UTC: ");
Serial.println(Teensy3Clock.get());
digitalClockDisplay(); // Arduino Time library
Serial.println();
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWriteFast(LED_BUILTIN, ledState);
}
}
// **********************************************************************************
// * Display Digital Clock
// **********************************************************************************
void digitalClockDisplay() {
// digital clock display of the time
Serial.print("12h: ");
Serial.print(hourFormat12()); // the hour now in 12 hour format
printDigits(minute());
printDigits(second());
if (isAM() == true) {
Serial.print(".AM");
}
else {
Serial.print(".PM");
}
Serial.println();
if (hour() < 10) {
Serial.print('0');
}
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();
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}