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

@@ -1,3 +1,13 @@
#pragma once #pragma once
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState); class Reader
{
private:
unsigned long lastDebounceTime = 0;
bool lastButtonState = LOW;
bool buttonState = HIGH;
int lastPin;
public:
bool debounceRead(int buttonPin);
};

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
const int DEBOUNCE_DELAY = 50; const int DEBOUNCE_DELAY = 200;
const int USER_STEPS = 2; const int USER_STEPS = 2;
const int STAFF_STEPS = 16; const int STAFF_STEPS = 16;

View File

@@ -2,33 +2,35 @@
#include <commonFunctions.h> #include <commonFunctions.h>
#include <constants.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)
if (reading != *lastButtonState)
{ {
// reset the debouncing timer lastPin = buttonPin;
*lastDebounceTime = millis();
} }
if ((millis() - *lastDebounceTime) > DEBOUNCE_DELAY) bool buttonPressed = false;
if (reading != lastButtonState && lastPin == buttonPin)
{ {
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state: lastDebounceTime = millis(); // reset debounce timer
}
// if the button state has changed: if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
if (reading != *buttonState)
{ {
*buttonState = reading; // If button state is stable and just changed to pressed
if (reading == HIGH && buttonState == LOW)
if (*buttonState == HIGH) {
return true; buttonPressed = true;
else
return false;
} }
buttonState = reading;
} }
// save the reading. Next time through the loop, it'll be the lastButtonState: if (lastPin == buttonPin)
*lastButtonState = reading; {
lastButtonState = reading;
}
return buttonPressed;
} }

View File

@@ -10,9 +10,7 @@ int maxVolume = 64;
int userVolume = 64; int userVolume = 64;
int oldUserVolume = userVolume; int oldUserVolume = userVolume;
int buttonState; Reader reader;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
void setup() void setup()
{ {
@@ -44,39 +42,39 @@ void loop()
{ {
updateLeds(maxVolume); updateLeds(maxVolume);
// SW1 - Staff Volume Up // // SW1 - Staff Volume Up
if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState)) // if (!reader.debounceRead(BTN_STAFF_UP))
{ // {
staffVolumeUp(&maxVolume); // staffVolumeUp(&maxVolume);
} // }
// SW2 - Staff Volume Down // // SW2 - Staff Volume Down
if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState)) // if (!reader.debounceRead(BTN_STAFF_DWN))
{ // {
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); // staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
} // }
// Remote D - Mute // Remote D - Mute
if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceRead(REMOTE_D))
{ {
mute(&userVolume, &oldUserVolume, maxVolume); mute(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote A - Significantly Lower Volume // Remote A - Significantly Lower Volume
if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState)) if (reader.debounceRead(REMOTE_A))
{ {
lowerVolume(&userVolume, &oldUserVolume, maxVolume); lowerVolume(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote B - Volume Up // Remote B - Volume Up
// Make sure we are not is a state of mute or lowered // Make sure we are not is a state of mute or lowered
if (debounceRead(REMOTE_B, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) if (reader.debounceRead(REMOTE_B) && !(oldUserVolume > userVolume))
{ {
volumeUp(&userVolume, &oldUserVolume, maxVolume); volumeUp(&userVolume, &oldUserVolume, maxVolume);
} }
// Remote C - Volume Down // Remote C - Volume Down
if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) if (reader.debounceRead(REMOTE_C) && !(oldUserVolume > userVolume))
{ {
volumeDown(&userVolume, &oldUserVolume); volumeDown(&userVolume, &oldUserVolume);
} }