changeset 2:13a554ba5332

Add lights and fix debouncing
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 25 Aug 2015 13:49:49 +0200
parents 1928b20a7094
children 19ea19d8381a
files voter.ino
diffstat 1 files changed, 20 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/voter.ino	Mon Aug 24 23:33:02 2015 +0200
+++ b/voter.ino	Tue Aug 25 13:49:49 2015 +0200
@@ -18,9 +18,11 @@
     0b01011000,
 };
 const int BUTTON_PINS[] = {2, 3, A2};
+const int LED_PINS[] = {A3, A4, A5};
 
 
 int counts[] = {0, 0, 0};
+long int last = 0;
 
 
 void show7seg(int number, int symbol) {
@@ -50,18 +52,25 @@
 
 
 void handle_buttons() {
-    static long int last = 0;
+    static bool led = false;
     long int now = millis();
 
-    if (now - last < 300) {
+    if (now - last < 400) {
         // Debouncing.
         return;
     }
-    last = now;
+    if (led) {
+        for (int l = 0; l < 3; ++l) {
+            digitalWrite(LED_PINS[l], LOW);
+        }
+    }
 
     for (int b = 0; b < 3; ++b) {
         if (!digitalRead(BUTTON_PINS[b])) {
             counts[b] += 1;
+            led = true;
+            digitalWrite(LED_PINS[b], HIGH);
+            last = now;
         }
     }
 }
@@ -77,11 +86,17 @@
     for (int b = 0; b < 3; ++b) {
         pinMode(BUTTON_PINS[b], INPUT_PULLUP);
     }
+    for (int l = 0; l < 3; ++l) {
+        pinMode(LED_PINS[l], OUTPUT);
+    }
 }
 
 
 void loop() {
-    int counter = (millis() / 2000) % 3;
-    show7seg(counts[counter], counter);
+    long int now = millis();
+    int counter = (now / 2000) % 3;
     handle_buttons();
+    if (now - last < 30000) {
+        show7seg(counts[counter], counter);
+    }
 }