Created
July 27, 2015 15:42
-
-
Save dakrawczyk/dd3b8d75c7409a247fbc 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
/*TO IMPLEMENT: you mast have the following variable names for your button pins: | |
hourPin | |
minutePin | |
setPin | |
TO USE: | |
if (debounceRead(hourPin)) | |
{ | |
hourButtonPressed(); | |
} | |
*/ | |
int buttonStates[4] = {LOW, LOW, LOW, LOW}; | |
int lastButtonStates[4]; | |
int buttonState = LOW; // the current reading from the input pin | |
int lastButtonState = LOW; // the previous reading from the input pin | |
long lastDebounceTime = 0; // the last time the output pin was toggled | |
long debounceDelay = 50; // the debounce time; increase if the output flickers | |
long lastChange = 0; | |
bool debounceRead(int pin) | |
{ | |
if ((millis() - lastChange) < 200) | |
{ | |
return false; | |
} | |
switch (pin) | |
{ | |
case hourPin: | |
{ | |
buttonState = buttonStates[0]; | |
lastButtonState = lastButtonStates[0]; | |
} | |
break; | |
case minutePin: | |
{ | |
buttonState = buttonStates[1]; | |
lastButtonState = lastButtonStates[1]; | |
} | |
break; | |
case setPin: | |
{ | |
buttonState = buttonStates[2]; | |
lastButtonState = lastButtonStates[2]; | |
} | |
break; | |
case snoozePin: | |
{ | |
buttonState = buttonStates[3]; | |
lastButtonState = lastButtonStates[3]; | |
} | |
break; | |
} | |
int reading = digitalRead(pin); | |
if (reading != lastButtonState) | |
{ | |
// Serial.println("reading is different than last button state"); | |
lastDebounceTime = millis(); | |
} | |
if ((millis() - lastDebounceTime) > debounceDelay) | |
{ | |
// whatever the reading is at, it's been there for longer | |
// than the debounce delay, so take it as the actual current state: | |
// if the button state has changed: | |
if (reading != buttonState) | |
{ | |
Serial.println("Button press successful"); | |
buttonState = reading; | |
lastChange = millis(); | |
return buttonState; | |
} | |
} | |
lastButtonState = reading; | |
switch (pin) | |
{ | |
case hourPin: | |
{ | |
buttonStates[0] = buttonState; | |
lastButtonStates[0] = lastButtonState; | |
} | |
break; | |
case minutePin: | |
{ | |
buttonStates[1] = buttonState; | |
lastButtonStates[1] = lastButtonState; | |
} | |
break; | |
case setPin: | |
{ | |
buttonStates[2] = buttonState; | |
lastButtonStates[2] = lastButtonState; | |
} | |
break; | |
case snoozePin: | |
{ | |
buttonStates[3] = buttonState; | |
lastButtonStates[3] = lastButtonState; | |
} | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment