diff --git a/include/commonFunctions.h b/include/commonFunctions.h index dd7b579..19cb8f7 100644 --- a/include/commonFunctions.h +++ b/include/commonFunctions.h @@ -1,3 +1,13 @@ #pragma once -bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState); \ No newline at end of file +class Reader +{ +private: + unsigned long lastDebounceTime = 0; + bool lastButtonState = LOW; + bool buttonState = HIGH; + int lastPin; + +public: + bool debounceRead(int buttonPin); +}; \ No newline at end of file diff --git a/include/constants.h b/include/constants.h index 8bb947c..1bc5be7 100644 --- a/include/constants.h +++ b/include/constants.h @@ -1,6 +1,6 @@ #pragma once -const int DEBOUNCE_DELAY = 50; +const int DEBOUNCE_DELAY = 200; const int USER_STEPS = 2; const int STAFF_STEPS = 16; diff --git a/src/commonFunctions.cpp b/src/commonFunctions.cpp index c7b3102..a0718be 100644 --- a/src/commonFunctions.cpp +++ b/src/commonFunctions.cpp @@ -2,33 +2,35 @@ #include #include -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; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 31fc94c..b93d804 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,9 +10,7 @@ int maxVolume = 64; int userVolume = 64; int oldUserVolume = userVolume; -int buttonState; -int lastButtonState = LOW; -unsigned long lastDebounceTime = 0; +Reader reader; void setup() { @@ -44,39 +42,39 @@ void loop() { updateLeds(maxVolume); - // SW1 - Staff Volume Up - if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState)) - { - staffVolumeUp(&maxVolume); - } + // // SW1 - Staff Volume Up + // if (!reader.debounceRead(BTN_STAFF_UP)) + // { + // staffVolumeUp(&maxVolume); + // } - // SW2 - Staff Volume Down - if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState)) - { - staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); - } + // // SW2 - Staff Volume Down + // if (!reader.debounceRead(BTN_STAFF_DWN)) + // { + // staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); + // } // Remote D - Mute - if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState)) + if (reader.debounceRead(REMOTE_D)) { mute(&userVolume, &oldUserVolume, maxVolume); } // Remote A - Significantly Lower Volume - if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState)) + if (reader.debounceRead(REMOTE_A)) { lowerVolume(&userVolume, &oldUserVolume, maxVolume); } // Remote B - Volume Up // 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); } // Remote C - Volume Down - if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) + if (reader.debounceRead(REMOTE_C) && !(oldUserVolume > userVolume)) { volumeDown(&userVolume, &oldUserVolume); }