Seperated debounce read function for buttons which are normally high and normally low
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
#include <commonFunctions.h>
|
||||
#include <constants.h>
|
||||
|
||||
bool Reader::debounceRead(int buttonPin)
|
||||
bool Reader::debounceReadNO(int buttonPin)
|
||||
{
|
||||
bool reading = digitalRead(buttonPin);
|
||||
|
||||
@@ -13,7 +13,7 @@ bool Reader::debounceRead(int buttonPin)
|
||||
|
||||
bool buttonPressed = false;
|
||||
|
||||
if (reading != lastButtonState && lastPin == buttonPin)
|
||||
if (reading != lastButtonStateNO && lastPin == buttonPin)
|
||||
{
|
||||
lastDebounceTime = millis(); // reset debounce timer
|
||||
}
|
||||
@@ -21,16 +21,49 @@ bool Reader::debounceRead(int buttonPin)
|
||||
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
||||
{
|
||||
// If button state is stable and just changed to pressed
|
||||
if (reading == HIGH && buttonState == LOW)
|
||||
if (reading == HIGH && buttonStateNO == LOW)
|
||||
{
|
||||
buttonPressed = true;
|
||||
}
|
||||
buttonState = reading;
|
||||
buttonStateNO = reading;
|
||||
}
|
||||
|
||||
if (lastPin == buttonPin)
|
||||
{
|
||||
lastButtonState = reading;
|
||||
lastButtonStateNO = reading;
|
||||
}
|
||||
return buttonPressed;
|
||||
}
|
||||
|
||||
bool Reader::debounceReadNC(int buttonPin)
|
||||
{
|
||||
bool reading = digitalRead(buttonPin);
|
||||
|
||||
if (reading == LOW)
|
||||
{
|
||||
lastPin = buttonPin;
|
||||
}
|
||||
|
||||
bool buttonPressed = false;
|
||||
|
||||
if (reading != lastButtonStateNC && lastPin == buttonPin)
|
||||
{
|
||||
lastDebounceTime = millis(); // reset debounce timer
|
||||
}
|
||||
|
||||
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
||||
{
|
||||
// If button state is stable and just changed to pressed
|
||||
if (reading == LOW && buttonStateNC == HIGH)
|
||||
{
|
||||
buttonPressed = true;
|
||||
}
|
||||
buttonStateNC = reading;
|
||||
}
|
||||
|
||||
if (lastPin == buttonPin)
|
||||
{
|
||||
lastButtonStateNC = reading;
|
||||
}
|
||||
return buttonPressed;
|
||||
}
|
||||
Reference in New Issue
Block a user