0% found this document useful (0 votes)
31 views2 pages

Clock

This Arduino sketch initializes a watch that displays the current time on the serial monitor. It sets the time to the moment the sketch was compiled and updates the time every second. The compile time is extracted and formatted for display using a custom function.

Uploaded by

claussimon1418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Clock

This Arduino sketch initializes a watch that displays the current time on the serial monitor. It sets the time to the moment the sketch was compiled and updates the time every second. The compile time is extracted and formatted for display using a custom function.

Uploaded by

claussimon1418
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

​#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;


}

You might also like