Skip to content

Commit b498cfe

Browse files
committed
Show sectonds better. Blink the light
1 parent 95e214a commit b498cfe

File tree

1 file changed

+40
-23
lines changed

1 file changed

+40
-23
lines changed

4x7seg-flip-disc-clock/4x7seg-flip-disc-clock.ino

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,20 +103,25 @@ void setup()
103103

104104
// convert the system (UNIX) time to a local date and time in a configurable format
105105
struct tm* last_tm = localtime(&last); // break down the timestamp
106-
showTime(last_tm->tm_hour, last_tm->tm_min);
106+
showTime(last_tm->tm_hour, last_tm->tm_min, last_tm->tm_sec);
107107

108108
// register a callback (execute whenever an NTP update has occurred)
109109
settimeofday_cb(timeUpdated);
110+
111+
// We're going to blink the LED also, so we can tell that it's alive.
112+
pinMode(LED_BUILTIN, OUTPUT);
113+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
110114
}
111115

112116
int last_hour;
113117
int last_min;
118+
int last_sec;
114119

115120
// callback routine - arrive here whenever a successful NTP update has occurred
116121
void timeUpdated() {
117122
time_t last = time(nullptr); // get UNIX timestamp
118123
struct tm *last_tm = localtime(&last); // convert to local time and break down
119-
showTime(last_tm->tm_hour, last_tm->tm_min);
124+
showTime(last_tm->tm_hour, last_tm->tm_min, last_tm->tm_sec);
120125

121126
char UPDATE_TIME[50]; // buffer for use by strftime()
122127
strftime(UPDATE_TIME, sizeof(UPDATE_TIME), "%T", last_tm); // extract just the 'time' portion
@@ -127,9 +132,10 @@ void timeUpdated() {
127132
}
128133

129134
// Show the time; update the globals with the "last time shown"
130-
void showTime(int hour, int minute) {
135+
void showTime(int hour, int minute, int sec) {
131136
last_hour = hour;
132137
last_min = minute;
138+
last_sec = sec;
133139
if (hour > 12) {
134140
hour -= 12;
135141
}
@@ -163,12 +169,36 @@ void loop() {
163169
time_t now_t = time(nullptr);
164170
// convert the system (UNIX) time to a local date and time in a configurable format
165171
struct tm *now = localtime(&now_t);
166-
if (now->tm_hour != last_hour) {
167-
Serial.println("different hours");
168-
}
169-
if (now->tm_min != last_min) {
170-
Serial.println("different min");
172+
173+
/* An example of calling the function to set disc no.19 of the first 7-Segment display */
174+
/* 0 1 2 3 4
175+
19 5
176+
18 6
177+
17 20 21 22 7
178+
16 8
179+
15 9
180+
14 13 12 11 10 */
181+
int sec = now->tm_sec;
182+
if (sec != last_sec) {
183+
// Flip one disc in the leftmost column to indicate 10s of seconds. I don't love this.
184+
for (int i = 0; i < sec / 10; i++) {
185+
Flip.Disc_7Seg(1, 10 + i, 1); // last argument can be 0 to turn off
186+
}
187+
// for (int i = 0; i < 5; ++i) {
188+
// Flip.Disc_7Seg(1, 10 + i, (i + sec) % 2); // last argument can be 0 to turn off
189+
// }
190+
if ((sec % 2) == 0) {
191+
digitalWrite(LED_BUILTIN, HIGH);
192+
} else {
193+
digitalWrite(LED_BUILTIN, LOW);
194+
}
195+
Serial.print("updated sec. was: ");
196+
Serial.print(last_sec);
197+
Serial.print(", now: " );
198+
Serial.println(sec);
171199
}
200+
last_sec = sec;
201+
172202
if (now->tm_hour != last_hour || now->tm_min != last_min) {
173203
// Hour or minute is different; update the whole clock.
174204
Serial.print("last_tm: ");
@@ -183,20 +213,7 @@ void loop() {
183213
Serial.print(":");
184214
Serial.println(now->tm_sec);
185215

186-
showTime(now->tm_hour, now->tm_min);
187-
}
188-
189-
/* An example of calling the function to set disc no.19 of the first 7-Segment display */
190-
/* 0 1 2 3 4
191-
19 5
192-
18 6
193-
17 20 21 22 7
194-
16 8
195-
15 9
196-
14 13 12 11 10 */
197-
// Flip one disc in the leftmost column to indicate 10s of seconds. I don't love this.
198-
for (int i = 0; i <= now->tm_sec / 10; i++) {
199-
Flip.Disc_7Seg(1, 14 + i, 1); // last argument can be 0 to turn off
216+
// this sets last_hour, last_min and last_sec
217+
showTime(now->tm_hour, now->tm_min, now->tm_sec);
200218
}
201-
delay(250);
202219
}

0 commit comments

Comments
 (0)