Created
June 23, 2017 21:30
-
-
Save evanrinehart/4d68b28e9957bcff28d633d7f14d11cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/HEAD^^:../Bed_Puzzle.ino b/HEAD:./bed-puzzle.ino | |
index 62ab181..126dd77 100644 | |
--- a/HEAD^^:../Bed_Puzzle.ino | |
+++ b/HEAD:./bed-puzzle.ino | |
@@ -29,20 +29,48 @@ void setup() { | |
Serial.begin(9600); | |
} | |
+/* debounced switch HIGH detectors */ | |
+#define DEBOUNCE_TIME 100 | |
+enum debounceStateCode {WAITING, ENGAGED}; | |
+int debounceState[3] = {WAITING,WAITING,WAITING}; | |
+int debounceTimer[3] = {DEBOUNCE_TIME, DEBOUNCE_TIME, DEBOUNCE_TIME}; | |
+ | |
+int debouncedDetect(int button){ | |
+ int pin; | |
+ switch(button){ | |
+ case 1: pin = buttonPin1; break; | |
+ case 2: pin = buttonPin2; break; | |
+ case 3: pin = buttonPin3; break; | |
+ default: return 0; /* bug. button must be 1 2 or 3 */ | |
+ } | |
+ int level = digitalRead(pin); | |
+ int i = button - 1; | |
+ if(level==HIGH){ | |
+ if(debounceState[i] == WAITING){ // do we care about this HIGH read, yes | |
+ debounceTimer[i]--; | |
+ if(debounceTimer[i]==0){ // been HIGH for a while, it's a hit. | |
+ debounceState[i] = ENGAGED; // we no longer care (until we see a LOW) | |
+ return 1; | |
+ } | |
+ } | |
+ } | |
+ else if(level==LOW){ // its bouncing or they let go, reset the detector | |
+ debounceTimer[i] = DEBOUNCE_TIME; | |
+ debounceState[i] = WAITING; | |
+ return 0; | |
+ } | |
+} | |
+ | |
+ | |
// the main loop will constantly check to see if the button has been pressed | |
// if it has, a counter is incremented, and then some action can be taken | |
void loop() { | |
// read the state of the button | |
static int buttonPushCounter = 0; | |
- bS1 = digitalRead(buttonPin1); | |
- bS2 = digitalRead(buttonPin2); | |
- bS3 = digitalRead(buttonPin3); | |
// check to see if it different than the last time we checked | |
- if (bS1 != lBS1) { | |
- // either the button was just pressed or just released | |
- if (bS1 == HIGH) { | |
+ if(debouncedDetect(1)) { | |
// it was just pressed | |
buttonPushCounter = buttonPushCounter + 1; | |
Serial.println(buttonPushCounter); | |
@@ -57,14 +85,9 @@ void loop() { | |
{ | |
val = 5; | |
} | |
- } | |
} | |
- lBS1 = bS1; | |
- | |
-if (bS2 != lBS2) { | |
- // either the button was just pressed or just released | |
- if (bS2 == HIGH) { | |
+ if(debouncedDetect(2)) { | |
// it was just pressed | |
buttonPushCounter = buttonPushCounter + 1; | |
Serial.println(buttonPushCounter); | |
@@ -80,14 +103,9 @@ if (bS2 != lBS2) { | |
{ | |
val = 4; | |
} | |
- } | |
} | |
- lBS2 = bS2; | |
- | |
-if (bS3 != lBS3) { | |
- // either the button was just pressed or just released | |
- if (bS3 == HIGH) { | |
+ if(debouncedDetect(3)) { | |
// it was just pressed | |
buttonPushCounter = buttonPushCounter + 1; | |
Serial.println(buttonPushCounter); | |
@@ -103,10 +121,7 @@ if (bS3 != lBS3) { | |
{ | |
val = 6 ; | |
} | |
- } | |
} | |
- lBS3 = bS3; | |
- | |
if(val == 6 && buttonPushCounter == 6) | |
{ | |
@@ -128,4 +143,4 @@ else if(val != 6 && buttonPushCounter == 6) | |
} | |
-} | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment