]> code.citadel.org Git - the_perfect_clock.git/blobdiff - the_perfect_clock.ino
Changed the hysteresis algorithm to make 'on' be 20 out of 100 samples
[the_perfect_clock.git] / the_perfect_clock.ino
index 318d641e1cbcaf392fa3527a34085d6862b81cbb..d437e392bfbd2ec2182770119718611d84251a2f 100644 (file)
@@ -87,6 +87,29 @@ void setup() {
 // Note: only write to the display when the readout needs to be updated.
 // Speaking I2C on every loop iteration jams the WWVB receiver.
 void loop() {
+  int signal;
+  static int total_samples = 0;
+  static int high_samples = 0;
+
+  // read from the WWVB receiver, with some hysteresis
+  if (total_samples >= 100) {
+    if (high_samples > 20) {
+      signal = HIGH;
+    }
+    else {
+      signal = LOW;
+    }
+    total_samples = 0;
+    high_samples = 0;
+  }
+  else {
+    total_samples += 1;
+    if (digitalRead(wwvb) == HIGH) {
+      ++high_samples;
+    }
+  }
+
+  // has the timer ticked?
        unsigned long m = millis();
        if (m != previous_millis) {
                millisecond += (m - previous_millis);
@@ -101,14 +124,13 @@ void loop() {
                                }
                        }
                }
-       }
+  }
        previous_millis = m;
 
        int pulse_length;
-       int signal = digitalRead(wwvb);                         // is the input high or low right now?
 
   if (signal) {
-    analogWrite(timecodeled, 10);       // it's too bright on my board so we dim it; change to digitalWrite() if not needed
+    analogWrite(timecodeled, 5);       // it's too bright on my board so we dim it; change to digitalWrite() if not needed
   }
   else {
     digitalWrite(timecodeled, LOW);