Experimental code to clean up the signal by averaging 1000 samples
[the_perfect_clock.git] / the_perfect_clock.ino
1 // "The Perfect Clock" 
2
3 // Copyright (C) 2019-2023 by Art Cancro <ajc@citadel.org>
4
5 // My perfect clock has no buttons and cannot be set manually.  This version uses a WWVB receiver module
6 // attached to pin D9 of the Arduino, and sets the clock any time it receives a complete frame.  The clock
7 // is kept without an RTC, simply using the millis() timer.  When time is set, it is displayed on
8 // a 7-segment array connected using an HT16K33 decoder/driver (yes, an Adafruit backpack).  Our display
9 // can also display at 15 different brightness levels, so we dim it when the room is dark to avoid
10 // blasticating a dark room with super-bright LED display.
11
12 // The clock is hard coded to use US Eastern time with DST in effect whenever WWVB is announcing it.
13
14 // This software is made available to you conditionally upon you accepting the following terms and conditions:
15 // 1. You agree that it is called "open source", not "free software".
16 // 2. You agree that the Linux operating system is not called "GNU/Linux".
17 // 3. You agree that Corey Ehmke is a scumbag, as are all social justice warriors.
18 // 4. You promise never to vote democrat in any election.
19 // 5. Under no circumstances may you use this program and also maintain a Facebook account.
20 // Aside from these conditions, the program is made available to you under the terms of the GNU General Public License.
21
22 // On my clock, there is a green LED on 2, a yellow LED on 3, and a red LED on 4.
23
24 const uint8_t wwvb = 9;         // pin on which WWVB signal will be received
25 const uint8_t last24led = 2;    // An LED attached to this pin will illuminate if the time has been set within the last 24 hours
26 const uint8_t cleantimecodeled = LED_BUILTIN;   // An LED attached to this pin will illuminate if we are currently receiving a clean frame
27 const uint8_t timecodeled = 3;
28 const uint8_t photocell = A0;   // Attach a photocell with a 10K voltage divider to this pin
29 const uint8_t addr = 0x70;      // I2C address of HT16K33 (using Adafruit backpack with digits on 0,1,3,4; dots on 2)
30
31 long millis_per_minute = 60000; // Nominally 60000; adjust if your board runs fast or slow
32
33 // This is a simple BCD-to-7-segment font.  It includes 0x0A through 0x0F even though they're not needed for a time clock.
34 const uint8_t sevensegfont[] = { 63, 6, 91, 79, 102, 109, 125, 7, 127, 111, 119, 124, 57, 94, 121, 113 };
35 const uint8_t firstcolfont[] = { 0, 6, 91 };    // this version of the font is for the first position
36
37 #include <Wire.h>               // I2C library to drive the HT16K33 display
38
39 int hour = 0;
40 int minute = 0;
41 unsigned long millisecond = 0;
42 unsigned long previous_millis = 0;
43 unsigned long last_sync = -86398000;
44 uint16_t displayBuffer[8];      // Digit buffer for HT16K33
45 int previous_minute = 61;       // What the minute was previously; we use this to detect whether an update is needed
46 int this_pulse = 0;             // Value of the current pulse received
47 int previous_pulse = 0;         // Value of the previous pulse received (two "mark" bits == new frame)
48 int start_of_pulse = 0;         // The value of the millis() timer when the current pulse began
49 uint8_t framebuf[60];           // We store the entire 60-bit frame here
50 uint8_t framesync = 0;          // Nonzero if we've received all good pulses since the start of the frame
51 int position_in_frame = 0;      // Where we are in the frame (1 bit per second)
52 int previous_signal = 0;        // "high" or "low" received on the previous cycle (so we can do edge detection)
53 int time_is_set = 0;            // nonzero when time has been set at least once
54
55 void setup() {
56         int i;
57
58         pinMode(timecodeled, OUTPUT);   // The built-in LED will display the raw WWVB signal pulses
59         pinMode(last24led, OUTPUT);     // This LED will illuminate if the time has been set within the last 24 hours
60         pinMode(cleantimecodeled, OUTPUT);      // This LED will illuminate if we are currently receiving a clean frame
61         pinMode(wwvb, INPUT);   // Input pin for WWVB receiver signal
62         pinMode(photocell, INPUT);      // Input pin for photocell
63
64         Wire.begin();           // Initialize I2C
65
66         Wire.beginTransmission(addr);
67         Wire.write(0x21);       // turn on oscillator
68         Wire.endTransmission();
69
70         Wire.beginTransmission(addr);
71         Wire.write(0xE1);       // brightness (max is 15)
72         Wire.endTransmission();
73
74         Wire.beginTransmission(addr);
75         Wire.write(0x81);       // no blinking or blanking
76         Wire.endTransmission();
77
78         displayBuffer[0] = 0;
79         displayBuffer[1] = 0;
80         displayBuffer[2] = 16;
81         displayBuffer[3] = 0;
82         displayBuffer[4] = 0;
83         show();
84 }
85
86
87 // Note: only write to the display when the readout needs to be updated.
88 // Speaking I2C on every loop iteration jams the WWVB receiver.
89 void loop() {
90   int signal;
91   static int total_samples = 0;
92   static int high_samples = 0;
93
94   if (total_samples >= 1000) {
95     if (high_samples > 500) {
96       signal = HIGH;
97     }
98     else {
99       signal = LOW;
100     }
101     total_samples = 0;
102     high_samples = 0;
103   }
104   else {
105     total_samples += 1;
106     if (digitalRead(wwvb) == HIGH) {
107       ++high_samples;
108     }
109   }
110
111         unsigned long m = millis();
112         if (m != previous_millis) {
113                 millisecond += (m - previous_millis);
114                 if (millisecond >= millis_per_minute) {
115                         millisecond -= millis_per_minute;
116                         ++minute;
117                         if (minute > 59) {
118                                 minute = 0;
119                                 ++hour;
120                                 if (hour > 23) {
121                                         hour = 0;
122                                 }
123                         }
124                 }
125   }
126         previous_millis = m;
127
128         int pulse_length;
129         //int signal = digitalRead(wwvb);                               // is the input high or low right now?
130
131   if (signal) {
132     analogWrite(timecodeled, 10);       // it's too bright on my board so we dim it; change to digitalWrite() if not needed
133   }
134   else {
135     digitalWrite(timecodeled, LOW);
136   }
137
138         if (signal && (!previous_signal)) {                     // leading edge of pulse detected
139                 start_of_pulse = millis();
140         }
141         else if ((!signal) && (previous_signal)) {              // trailing edge of pulse detected
142                 pulse_length = millis() - start_of_pulse;
143
144                 if (pulse_length > 175 && pulse_length < 225) { // "0" bit ~= 200 ms (represented as "0")
145                         this_pulse = 0;
146                 }
147                 else if (pulse_length > 475 && pulse_length < 525) {    // "1" bit ~= 500 ms (represented as "1")
148                         this_pulse = 1;
149                 }
150                 else if (pulse_length > 775 && pulse_length < 825) {    // marker bit ~= 800 ms (represented as "2")
151                         this_pulse = 2;
152                 }
153                 else {
154                         this_pulse = 15;        // bad pulse (represented as "15")
155                         framesync = 0;  // throw the whole frame away
156                 }
157
158                 // BEGIN -- THINGS TO DO AT THE END OF A PULSE
159
160                 if ((this_pulse == 2) && (previous_pulse == 2)) {       // start of a new frame!
161
162                         if (framesync == 1) {
163                                 set_the_time(); // We have a whole good frame.  Set the clock!
164                         }
165                         else if ((!framesync) && (time_is_set)) {
166                                 snap_to_zero(); // We don't have a whole frame, but we know it's :00 seconds now.
167                         }
168
169                         framesync = 1;
170                         position_in_frame = 0;
171                         calibrate();    // calibrate the software timer
172                 }
173
174                 if (framesync) {        // yellow LED = we currently have frame sync
175                         digitalWrite(cleantimecodeled, HIGH);   // (we run it at a low intensity)
176                 }
177                 else {
178                         digitalWrite(cleantimecodeled, LOW);
179                 }
180
181                 if ((framesync) && (position_in_frame < 60)) {
182                         framebuf[position_in_frame++] = this_pulse;
183                 }
184
185                 previous_pulse = this_pulse;
186
187                 // END -- THINGS TO DO AT THE END OF A PULSE
188         }
189
190         previous_signal = signal;
191
192         // Update the display only if it's a new minute.
193
194         if (time_is_set && (minute != previous_minute)) {
195                 previous_minute = minute;
196                 int h12 = (hour % 12);
197                 if (h12 == 0)
198                         h12 = 12;
199                 displayBuffer[0] = firstcolfont[h12 / 10];
200                 displayBuffer[1] = sevensegfont[h12 % 10];
201                 displayBuffer[2] = (hour < 12) ? 0x06 : 0x0a;   // AM or PM dot , colon always on
202                 displayBuffer[3] = sevensegfont[minute / 10];
203                 displayBuffer[4] = sevensegfont[minute % 10];
204                 show();
205         }
206
207         if ((m - last_sync) < 86400000) {       // green LED = got a good sync in the last 24 hours
208                 digitalWrite(last24led, HIGH);
209         }
210         else {
211                 digitalWrite(last24led, LOW);
212         }
213 }
214
215
216 // Write the display buffer to the display
217 void show() {
218         // display the time
219         Wire.beginTransmission(addr);
220         Wire.write(0x00);       // start at address 0x0
221         for (int i = 0; i < 5; i++) {
222                 Wire.write(displayBuffer[i] & 0xFF);
223                 Wire.write(displayBuffer[i] >> 8);
224         }
225         Wire.endTransmission();
226
227         // set the brightness
228         int light_level = analogRead(photocell) / 64;
229         if (light_level < 1) {
230                 light_level = 1;
231         }
232         if (light_level > 15) {
233                 light_level = 15;
234         }
235         Wire.beginTransmission(addr);
236         Wire.write(0xE0 + light_level); // set the display brightness
237         Wire.endTransmission();
238 }
239
240
241 // Set the software clock to the WWVB time currently in the buffer
242 void set_the_time() {
243         int i, newhour, newminute, dst;
244
245         // These six positions MUST contain marker bits.
246         // If any of them do not, we are looking at a corrupt frame.
247         int markers[] = { 0, 9, 19, 39, 49, 59 };
248         for (i = 0; i < 6; ++i) {
249                 if (framebuf[markers[i]] != 2) {
250                         return;
251                 }
252         }
253
254         newhour = (framebuf[12] ? 20 : 0);
255         newhour += (framebuf[13] ? 10 : 0);
256         newhour += (framebuf[15] ? 8 : 0);
257         newhour += (framebuf[16] ? 4 : 0);
258         newhour += (framebuf[17] ? 2 : 0);
259         newhour += (framebuf[18] ? 1 : 0);
260         if ((newhour < 0) || (newhour > 23)) {
261                 return;         // reject impossible hours
262         }
263
264         newminute = (framebuf[1] ? 40 : 0);
265         newminute += (framebuf[2] ? 20 : 0);
266         newminute += (framebuf[3] ? 10 : 0);
267         newminute += (framebuf[5] ? 8 : 0);
268         newminute += (framebuf[6] ? 4 : 0);
269         newminute += (framebuf[7] ? 2 : 0);
270         newminute += (framebuf[8] ? 1 : 0);
271         if ((newminute < 0) || (newminute > 59)) {
272                 return;         // reject impossible minutes
273         }
274
275         // advance 1 minute because WWVB gives the *previous* minute
276         newminute += 1;
277         if (newminute >= 60) {
278                 newminute = newminute % 60;
279                 newhour += 1;
280         }
281
282         // US Eastern time (yes it is hard coded)
283         newhour -= 5;
284
285         // DST (FIXME make this adjustable)
286         dst = (framebuf[57] ? 2 : 0);
287         dst += (framebuf[58] ? 1 : 0);
288         switch (dst) {
289         case 0:         // dst not in effect (make no adjustments)
290                 break;
291         case 2:         // dst begins today (adjust if local hour > 2)
292                 if (newhour >= 2) {
293                         ++newhour;
294                 }
295                 break;
296         case 3:         // dst is in effect (always adjust)
297                 ++newhour;
298                 break;
299         case 1:         // dst ends today (adjust if local hour < 2)
300                 if (newhour < 2) {
301                         ++newhour;
302                 }
303                 break;
304         }
305
306         // If we went back to the previous day, adjust so that hour > 0
307         if (newhour < 0) {
308                 newhour += 24;
309         }
310
311         // Set the software clock:
312         // * We have decoded the hour and minute from the signal
313         // * This function always gets called *after* the first pulse at :00, so we set the millisecond to 800
314         hour = newhour;
315         minute = newminute;
316         millisecond = 800;
317         time_is_set = 1;
318
319         // Let's remember the last time we synced the clock
320         last_sync = millis();
321 }
322
323
324 // Adjust the time to :00.8 seconds at the nearest minute.
325 void snap_to_zero() {
326         if ((millisecond > 0) && (millisecond < 15000)) {       // If the second is from :00.0 to :15.0
327                 millisecond = 800;      // snap back to :00.8
328         }
329         else if (millisecond > 45000) { // If the second is :45.0 or above
330                 millisecond = millis_per_minute + 800;  // snap forward to :00.8 (minute will advance automatically)
331         }
332 }
333
334
335 // By determining how many timer ticks elapsed between two minute markers, we can calibrate our software clock.
336 // Nominally it is 60000 milliseconds, but the software clock tends to drift.
337 // So we start with an array of all 60000 ms, and we keep ten calibrations and average them.
338 void calibrate() {
339
340         static unsigned long mpm_array[10] = { 60000, 60000, 60000, 60000, 60000, 60000, 60000, 60000, 60000, 60000 };
341         static int mpm = 0;                             // next one to update
342
343         static unsigned long last_calib = -86398000;
344         unsigned long m = millis();
345         unsigned long mm = m - last_calib;
346         if ((mm > 50000) && (mm < 70000)) {
347                 mpm_array[mpm++] = mm;
348                 if (mpm >= 10) {
349                         mpm = 0;
350                 }
351                 millis_per_minute = (mpm_array[0] + mpm_array[1] + mpm_array[2] + mpm_array[3]
352                                 + mpm_array[4] + mpm_array[5] + mpm_array[6] + mpm_array[7]
353                                 + mpm_array[8] + mpm_array[9]) / 10;
354         }
355         last_calib = m;
356 }