Seperated debounce read function for buttons which are normally high and normally low

This commit is contained in:
2025-10-31 14:39:00 +01:00
parent 194e608f9e
commit 2991f58c7d
3 changed files with 58 additions and 22 deletions

View File

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

View File

@@ -42,39 +42,39 @@ void loop()
{
updateLeds(maxVolume);
// // SW1 - Staff Volume Up
// if (!reader.debounceRead(BTN_STAFF_UP))
// {
// staffVolumeUp(&maxVolume);
// }
// SW1 - Staff Volume Up
if (reader.debounceReadNC(BTN_STAFF_UP))
{
staffVolumeUp(&maxVolume);
}
// // SW2 - Staff Volume Down
// if (!reader.debounceRead(BTN_STAFF_DWN))
// {
// staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
// }
// SW2 - Staff Volume Down
if (reader.debounceReadNC(BTN_STAFF_DWN))
{
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
}
// Remote D - Mute
if (reader.debounceRead(REMOTE_D))
if (reader.debounceReadNO(REMOTE_D))
{
mute(&userVolume, &oldUserVolume, maxVolume);
}
// Remote A - Significantly Lower Volume
if (reader.debounceRead(REMOTE_A))
if (reader.debounceReadNO(REMOTE_A))
{
lowerVolume(&userVolume, &oldUserVolume, maxVolume);
}
// Remote B - Volume Up
// Make sure we are not is a state of mute or lowered
if (reader.debounceRead(REMOTE_B) && !(oldUserVolume > userVolume))
if (reader.debounceReadNO(REMOTE_B) && !(oldUserVolume > userVolume))
{
volumeUp(&userVolume, &oldUserVolume, maxVolume);
}
// Remote C - Volume Down
if (reader.debounceRead(REMOTE_C) && !(oldUserVolume > userVolume))
if (reader.debounceReadNO(REMOTE_C) && !(oldUserVolume > userVolume))
{
volumeDown(&userVolume, &oldUserVolume);
}