Seperated debounce read function for buttons which are normally high and normally low
This commit is contained in:
@@ -4,10 +4,13 @@ class Reader
|
||||
{
|
||||
private:
|
||||
unsigned long lastDebounceTime = 0;
|
||||
bool lastButtonState = LOW;
|
||||
bool buttonState = HIGH;
|
||||
bool lastButtonStateNO = LOW;
|
||||
bool buttonStateNO = HIGH;
|
||||
bool lastButtonStateNC = HIGH;
|
||||
bool buttonStateNC = LOW;
|
||||
int lastPin;
|
||||
|
||||
public:
|
||||
bool debounceRead(int buttonPin);
|
||||
bool debounceReadNO(int buttonPin);
|
||||
bool debounceReadNC(int buttonPin);
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
28
src/main.cpp
28
src/main.cpp
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user