diff --git a/include/commonFunctions.h b/include/commonFunctions.h index 19cb8f7..8e0d36e 100644 --- a/include/commonFunctions.h +++ b/include/commonFunctions.h @@ -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); }; \ No newline at end of file diff --git a/src/commonFunctions.cpp b/src/commonFunctions.cpp index a0718be..83b5438 100644 --- a/src/commonFunctions.cpp +++ b/src/commonFunctions.cpp @@ -2,7 +2,7 @@ #include #include -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; } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index b93d804..4fe31bb 100644 --- a/src/main.cpp +++ b/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); }