Made debounceRead function work
This commit is contained in:
@@ -2,33 +2,35 @@
|
||||
#include <commonFunctions.h>
|
||||
#include <constants.h>
|
||||
|
||||
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState)
|
||||
bool Reader::debounceRead(int buttonPin)
|
||||
{
|
||||
int reading = digitalRead(buttonPin);
|
||||
bool reading = digitalRead(buttonPin);
|
||||
|
||||
// If the switch changed, due to noise or pressing:
|
||||
if (reading != *lastButtonState)
|
||||
if (reading)
|
||||
{
|
||||
// reset the debouncing timer
|
||||
*lastDebounceTime = millis();
|
||||
lastPin = buttonPin;
|
||||
}
|
||||
|
||||
if ((millis() - *lastDebounceTime) > DEBOUNCE_DELAY)
|
||||
{
|
||||
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
|
||||
bool buttonPressed = false;
|
||||
|
||||
// if the button state has changed:
|
||||
if (reading != *buttonState)
|
||||
if (reading != lastButtonState && lastPin == buttonPin)
|
||||
{
|
||||
lastDebounceTime = millis(); // reset debounce timer
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
||||
{
|
||||
// If button state is stable and just changed to pressed
|
||||
if (reading == HIGH && buttonState == LOW)
|
||||
{
|
||||
*buttonState = reading;
|
||||
|
||||
if (*buttonState == HIGH)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
buttonPressed = true;
|
||||
}
|
||||
buttonState = reading;
|
||||
}
|
||||
|
||||
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
||||
*lastButtonState = reading;
|
||||
if (lastPin == buttonPin)
|
||||
{
|
||||
lastButtonState = reading;
|
||||
}
|
||||
return buttonPressed;
|
||||
}
|
||||
Reference in New Issue
Block a user