-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmillis_clock.ino
99 lines (90 loc) · 2.46 KB
/
millis_clock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// ---------------------------------------------
// millis_clock.ino
// ---------------------------------------------
// License: The Unlicense, Public Domain.
// Author: Koepel
// 2019 march 20
// ---------------------------------------------
//
// Run a clock with hours, minutes and seconds.
// The serial monitor is used for the output and
// for setting the time.
//
// The format is hh:mm:ss with 24 hours.
//
unsigned long previousMillis;
const unsigned long interval = 1000; // 1 second
int hours, minutes, seconds;
void setup()
{
Serial.begin( 9600);
Serial.setTimeout( 100); // 100ms timeout for serial input
Serial.println( "Set the new time in hh:mm:ss format");
}
void loop()
{
unsigned long currentMillis = millis();
if( Serial.available() > 0)
{
delay( 100); // wait 100ms to get whole line
int h, a, m, b, s;
h = Serial.parseInt(); // get integer with timeout
a = Serial.read(); // get character, no timeout
m = Serial.parseInt(); // get integer with timeout
b = Serial.read(); // get character, no timeout
s = Serial.parseInt(); // get integer with timeout
if( a == ':' && b == ':') // check for right format
{
hours = constrain( h, 0, 23);
minutes = constrain( m, 0, 59);
seconds = constrain( s, 0, 59);
}
else
{
Serial.println( "New time not accepted");
}
// Clear remaining characters from the serial input buffer.
while( Serial.available() > 0)
{
Serial.read();
}
}
if( currentMillis - previousMillis >= interval)
{
// When previousMillis would be set to currentMillis,
// then a delay in the code would delay the clock.
// When previousMillis is incremented with 1 second,
// then it stays synchronized.
previousMillis += interval; // increment with 1 second
seconds++;
if( seconds >= 60)
{
seconds = 0;
minutes++;
if( minutes >= 60)
{
minutes = 0;
hours++;
if( hours >= 24)
{
hours = 0;
}
}
}
// Update the time to the serial monitor.
// The format is hh:mm:ss.
// For example 09:30:55
if( hours < 10)
Serial.print( "0");
Serial.print( hours);
Serial.print( ":");
if( minutes < 10)
Serial.print( "0");
Serial.print( minutes);
Serial.print( ":");
if( seconds < 10)
Serial.print( "0");
Serial.print( seconds);
Serial.println();
}
}