#include <TimeLib.
h>
void setup() {
// Start serial communication at 9600 baud
[Link](9600);
// Wait for the serial monitor to open
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// Set the time to the time this sketch was compiled
setTime(compileTime());
[Link]("Arduino Watch Initialized!");
[Link]("-------------------------");
[Link]("The current time is now being displayed below.");
[Link]();
}
void loop() {
// Print the current time to the serial monitor
printTime();
// Wait for one second before printing the time again
delay(1000);
}
// Custom function to print the time in a nice format
void printTime() {
char timeBuffer[20];
// Format the time into HH:MM:SS
sprintf(timeBuffer, "%02d:%02d:%02d", hour(), minute(), second());
[Link](timeBuffer);
}
// This function is called by setTime() to get the compile time
time_t compileTime() {
const time_t FUDGE(10); // fudge factor to allow for upload time
const char *compDate = __DATE__;
const char *compTime = __TIME__;
char s_month[5];
int year;
int month, day, hour, minute, second;
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
sscanf(compDate, "%s %d %d", s_month, &day, &year);
sscanf(compTime, "%d:%d:%d", &hour, &minute, &second);
month = (strstr(month_names, s_month) - month_names) / 3 + 1;
tmElements_t tm;
[Link] = CalendarYrToTm(year);
[Link] = month;
[Link] = day;
[Link] = hour;
[Link] = minute;
[Link] = second;
return makeTime(tm) + FUDGE;
}