@@ -103,20 +103,25 @@ void setup()
103
103
104
104
// convert the system (UNIX) time to a local date and time in a configurable format
105
105
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 );
107
107
108
108
// register a callback (execute whenever an NTP update has occurred)
109
109
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)
110
114
}
111
115
112
116
int last_hour;
113
117
int last_min;
118
+ int last_sec;
114
119
115
120
// callback routine - arrive here whenever a successful NTP update has occurred
116
121
void timeUpdated () {
117
122
time_t last = time (nullptr ); // get UNIX timestamp
118
123
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 );
120
125
121
126
char UPDATE_TIME[50 ]; // buffer for use by strftime()
122
127
strftime (UPDATE_TIME, sizeof (UPDATE_TIME), " %T" , last_tm); // extract just the 'time' portion
@@ -127,9 +132,10 @@ void timeUpdated() {
127
132
}
128
133
129
134
// 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 ) {
131
136
last_hour = hour;
132
137
last_min = minute;
138
+ last_sec = sec;
133
139
if (hour > 12 ) {
134
140
hour -= 12 ;
135
141
}
@@ -163,12 +169,36 @@ void loop() {
163
169
time_t now_t = time (nullptr );
164
170
// convert the system (UNIX) time to a local date and time in a configurable format
165
171
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);
171
199
}
200
+ last_sec = sec;
201
+
172
202
if (now->tm_hour != last_hour || now->tm_min != last_min) {
173
203
// Hour or minute is different; update the whole clock.
174
204
Serial.print (" last_tm: " );
@@ -183,20 +213,7 @@ void loop() {
183
213
Serial.print (" :" );
184
214
Serial.println (now->tm_sec );
185
215
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 );
200
218
}
201
- delay (250 );
202
219
}
0 commit comments