Made debounceRead function work

This commit is contained in:
2025-10-31 14:23:42 +01:00
parent a8ddf41be3
commit 194e608f9e
4 changed files with 48 additions and 38 deletions

View File

@@ -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;
}