From 194e608f9e3a5ba39142914914e0cde59accc7b6 Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 14:23:42 +0100 Subject: [PATCH 1/6] Made debounceRead function work --- include/commonFunctions.h | 12 +++++++++++- include/constants.h | 2 +- src/commonFunctions.cpp | 40 ++++++++++++++++++++------------------- src/main.cpp | 32 +++++++++++++++---------------- 4 files changed, 48 insertions(+), 38 deletions(-) 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); } -- 2.49.1 From 2991f58c7d6099776a0f1f877239007f1948ea4b Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 14:39:00 +0100 Subject: [PATCH 2/6] Seperated debounce read function for buttons which are normally high and normally low --- include/commonFunctions.h | 9 +++++--- src/commonFunctions.cpp | 43 ++++++++++++++++++++++++++++++++++----- src/main.cpp | 28 ++++++++++++------------- 3 files changed, 58 insertions(+), 22 deletions(-) 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); } -- 2.49.1 From 5c1f331b0ec6517018063de9630cb14379067f9b Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 14:42:16 +0100 Subject: [PATCH 3/6] Made staffBolumeDown work --- include/staffFunctions.h | 4 ++-- src/main.cpp | 2 +- src/staffFunctions.cpp | 18 +++++++++--------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/include/staffFunctions.h b/include/staffFunctions.h index 6bdf2a2..0fce6ea 100644 --- a/include/staffFunctions.h +++ b/include/staffFunctions.h @@ -10,8 +10,8 @@ void staffVolumeUp(int *maxVolume); /** * @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered. * - * @param maxVolume Variable used to keep track of the max volume + * @param maxVolume Pointer to the variable used to keep track of the max volume * @param userVolume Pointer to the variable used to keep track of the user volume * @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered */ -void staffVolumeDown(int maxVolume, int *userVolume, int *oldUserVolume); \ No newline at end of file +void staffVolumeDown(int *maxVolume, int *userVolume, int *oldUserVolume); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4fe31bb..38ba34b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,7 +51,7 @@ void loop() // SW2 - Staff Volume Down if (reader.debounceReadNC(BTN_STAFF_DWN)) { - staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); + staffVolumeDown(&maxVolume, &userVolume, &oldUserVolume); } // Remote D - Mute diff --git a/src/staffFunctions.cpp b/src/staffFunctions.cpp index ad28b9b..7982f77 100644 --- a/src/staffFunctions.cpp +++ b/src/staffFunctions.cpp @@ -29,30 +29,30 @@ void staffVolumeUp(int *maxVolume) /** * @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered. * - * @param maxVolume Variable used to keep track of the max volume + * @param maxVolume Pointer to the variable used to keep track of the max volume * @param userVolume Pointer to the variable used to keep track of the user volume * @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered */ -void staffVolumeDown(int maxVolume, int *userVolume, int *oldUserVolume) +void staffVolumeDown(int *maxVolume, int *userVolume, int *oldUserVolume) { - if (maxVolume > STAFF_STEPS) + if (*maxVolume > STAFF_STEPS) { - maxVolume -= STAFF_STEPS; + *maxVolume -= STAFF_STEPS; - if (*userVolume > maxVolume) + if (*userVolume > *maxVolume) { - for (int i = 0; i < (*userVolume - maxVolume); i++) + for (int i = 0; i < (*userVolume - *maxVolume); i++) { potDecrement(); } - *userVolume = maxVolume; + *userVolume = *maxVolume; *oldUserVolume = *userVolume; } - if (*oldUserVolume > maxVolume) + if (*oldUserVolume > *maxVolume) { - *oldUserVolume = maxVolume; + *oldUserVolume = *maxVolume; } } } \ No newline at end of file -- 2.49.1 From 40469c80c5411d535e481e9fb48ffc8e959792bc Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 14:58:54 +0100 Subject: [PATCH 4/6] Added comments to potControlls --- include/potControlls.h | 9 +++++++++ src/main.cpp | 3 +++ src/potControlls.cpp | 23 +++++++++++++++++++++++ src/staffFunctions.cpp | 2 +- src/userFunctions.cpp | 2 +- 5 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/potControlls.h b/include/potControlls.h index 096b64b..a9f3c85 100644 --- a/include/potControlls.h +++ b/include/potControlls.h @@ -1,4 +1,13 @@ #pragma once +/** + * @brief This function moves the potentiometer wiper one step to pin A + * + */ void potIncrement(); + +/** + * @brief This function moves the potentiometer wiper one step to pin B + * + */ void potDecrement(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 38ba34b..4ec4343 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,11 +27,13 @@ void setup() pinMode(REMOTE_C, INPUT); // Remote Button C pinMode(REMOTE_D, INPUT); // Remote Button D + // Potentiometer pinMode(POT_CS, OUTPUT); // D10 - CS pinMode(POT_UD, OUTPUT); // D9 - U/D digitalWrite(POT_CS, HIGH); digitalWrite(POT_UD, LOW); + // Reset the potentiometer for (int i = 0; i < 64; i++) { potIncrement(); @@ -74,6 +76,7 @@ void loop() } // Remote C - Volume Down + // Make sure we are not is a state of mute or lowered if (reader.debounceReadNO(REMOTE_C) && !(oldUserVolume > userVolume)) { volumeDown(&userVolume, &oldUserVolume); diff --git a/src/potControlls.cpp b/src/potControlls.cpp index 434bea1..77bebed 100644 --- a/src/potControlls.cpp +++ b/src/potControlls.cpp @@ -1,7 +1,26 @@ +/** + * @file potControlls.cpp + * @author Lyubomir Penev (me@lpenev.com) + * @brief Here are all the functions responsible for incrementing and decrementing the potentiometer + * @version 0.1 + * @date 2025-10-31 + * + * @copyright Copyright (c) 2025 + * + * Here are all the functions responsible for incrementing and decrementing the MCP4011 digital potentiometer. For more information on the protocol used to control the potentiometer you can look on pages 29 and 31 of the datasheet, and for timing information you can checkout pages 6 and 7. + * + * Datasheet: https://ww1.microchip.com/downloads/en/devicedoc/21978a.pdf + * + */ + #include #include #include +/** + * @brief This function moves the potentiometer wiper one step to pin A + * + */ void potIncrement() { digitalWrite(POT_UD, HIGH); @@ -18,6 +37,10 @@ void potIncrement() digitalWrite(POT_CS, HIGH); } +/** + * @brief This function moves the potentiometer wiper one step to pin B + * + */ void potDecrement() { digitalWrite(POT_UD, LOW); diff --git a/src/staffFunctions.cpp b/src/staffFunctions.cpp index 7982f77..ae035d8 100644 --- a/src/staffFunctions.cpp +++ b/src/staffFunctions.cpp @@ -1,6 +1,6 @@ /** * @file staffFunctions.cpp - * @author Lyubomir Penev + * @author Lyubomir Penev (me@lpenev.com) * @brief Here are all the implementations of the staff functions * @version 1.0 * @date 2025-10-23 diff --git a/src/userFunctions.cpp b/src/userFunctions.cpp index eabe136..558f580 100644 --- a/src/userFunctions.cpp +++ b/src/userFunctions.cpp @@ -1,6 +1,6 @@ /** * @file userFunctions.cpp - * @author Lyubomir Penev + * @author Lyubomir Penev (me@lpenev.com) * @brief Here are all the implementations for the user functions. * @version 1.0 * @date 2025-10-23 -- 2.49.1 From 65c4f3d0ba9745464421338be92e22144436373a Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 15:04:11 +0100 Subject: [PATCH 5/6] Added comments to ledControlls --- include/ledControlls.h | 10 ++++++++++ src/ledControlls.cpp | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/ledControlls.h b/include/ledControlls.h index d1ce3d2..0c1e795 100644 --- a/include/ledControlls.h +++ b/include/ledControlls.h @@ -1,4 +1,14 @@ #pragma once +/** + * @brief This function sets all the LEDs to a default(LOW) state + * + */ void updateLeds(int volume); + +/** + * @brief This function turns on the correct LED depending on the current max(staff) volume + * + * @param volume The current max(staff) volume + */ void resetLeds(); \ No newline at end of file diff --git a/src/ledControlls.cpp b/src/ledControlls.cpp index b9f45a8..87023ae 100644 --- a/src/ledControlls.cpp +++ b/src/ledControlls.cpp @@ -1,7 +1,22 @@ +/** + * @file ledControlls.cpp + * @author Lyubomir Penev (me@lpenev.com) + * @brief This file contains all the functions which control the LEDs used to indicate the current max volume + * @version 0.1 + * @date 2025-10-31 + * + * @copyright Copyright (c) 2025 + * + */ + #include #include #include +/** + * @brief This function sets all the LEDs to a default(LOW) state + * + */ void resetLeds() { digitalWrite(LED_HIGH, LOW); @@ -9,6 +24,11 @@ void resetLeds() digitalWrite(LED_LOW, LOW); } +/** + * @brief This function turns on the correct LED depending on the current max(staff) volume + * + * @param volume The current max(staff) volume + */ void updateLeds(int volume) { switch (volume) -- 2.49.1 From 1837e09bc15ceed235d9b57a0d0f68a8c5b921e2 Mon Sep 17 00:00:00 2001 From: Lyubomir Penev Date: Fri, 31 Oct 2025 15:06:12 +0100 Subject: [PATCH 6/6] Added fontys email --- src/ledControlls.cpp | 2 +- src/potControlls.cpp | 14 +++++++------- src/staffFunctions.cpp | 6 +++--- src/userFunctions.cpp | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ledControlls.cpp b/src/ledControlls.cpp index 87023ae..73b6383 100644 --- a/src/ledControlls.cpp +++ b/src/ledControlls.cpp @@ -1,6 +1,6 @@ /** * @file ledControlls.cpp - * @author Lyubomir Penev (me@lpenev.com) + * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl) * @brief This file contains all the functions which control the LEDs used to indicate the current max volume * @version 0.1 * @date 2025-10-31 diff --git a/src/potControlls.cpp b/src/potControlls.cpp index 77bebed..11704ea 100644 --- a/src/potControlls.cpp +++ b/src/potControlls.cpp @@ -1,16 +1,16 @@ /** * @file potControlls.cpp - * @author Lyubomir Penev (me@lpenev.com) + * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl) * @brief Here are all the functions responsible for incrementing and decrementing the potentiometer * @version 0.1 * @date 2025-10-31 - * + * * @copyright Copyright (c) 2025 - * + * * Here are all the functions responsible for incrementing and decrementing the MCP4011 digital potentiometer. For more information on the protocol used to control the potentiometer you can look on pages 29 and 31 of the datasheet, and for timing information you can checkout pages 6 and 7. - * + * * Datasheet: https://ww1.microchip.com/downloads/en/devicedoc/21978a.pdf - * + * */ #include @@ -19,7 +19,7 @@ /** * @brief This function moves the potentiometer wiper one step to pin A - * + * */ void potIncrement() { @@ -39,7 +39,7 @@ void potIncrement() /** * @brief This function moves the potentiometer wiper one step to pin B - * + * */ void potDecrement() { diff --git a/src/staffFunctions.cpp b/src/staffFunctions.cpp index ae035d8..f445899 100644 --- a/src/staffFunctions.cpp +++ b/src/staffFunctions.cpp @@ -1,6 +1,6 @@ /** * @file staffFunctions.cpp - * @author Lyubomir Penev (me@lpenev.com) + * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl) * @brief Here are all the implementations of the staff functions * @version 1.0 * @date 2025-10-23 @@ -15,7 +15,7 @@ /** * @brief Used to increase the maximum staff volume - * + * * @param maxVolume Pointer to the variable used to keep track of staff volume */ void staffVolumeUp(int *maxVolume) @@ -28,7 +28,7 @@ void staffVolumeUp(int *maxVolume) /** * @brief Used to decrease the maximum staff volume. If the max volume is set lower than what the current user volume is, then the user volume will also be lowered. - * + * * @param maxVolume Pointer to the variable used to keep track of the max volume * @param userVolume Pointer to the variable used to keep track of the user volume * @param oldUserVolume Pointer to the variable used to keep track of the user volume when in mute or lowered diff --git a/src/userFunctions.cpp b/src/userFunctions.cpp index 558f580..20b4450 100644 --- a/src/userFunctions.cpp +++ b/src/userFunctions.cpp @@ -1,6 +1,6 @@ /** * @file userFunctions.cpp - * @author Lyubomir Penev (me@lpenev.com) + * @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl) * @brief Here are all the implementations for the user functions. * @version 1.0 * @date 2025-10-23 -- 2.49.1