diff --git a/include/commonFunctions.h b/include/commonFunctions.h index dd7b579..8e0d36e 100644 --- a/include/commonFunctions.h +++ b/include/commonFunctions.h @@ -1,3 +1,16 @@ #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 lastButtonStateNO = LOW; + bool buttonStateNO = HIGH; + bool lastButtonStateNC = HIGH; + bool buttonStateNC = LOW; + int lastPin; + +public: + bool debounceReadNO(int buttonPin); + bool debounceReadNC(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/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/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/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/commonFunctions.cpp b/src/commonFunctions.cpp index c7b3102..83b5438 100644 --- a/src/commonFunctions.cpp +++ b/src/commonFunctions.cpp @@ -2,33 +2,68 @@ #include #include -bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState) +bool Reader::debounceReadNO(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 != lastButtonStateNO && 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 && buttonStateNO == LOW) { - *buttonState = reading; - - if (*buttonState == HIGH) - return true; - else - return false; + buttonPressed = true; } + buttonStateNO = reading; } - // save the reading. Next time through the loop, it'll be the lastButtonState: - *lastButtonState = reading; + if (lastPin == buttonPin) + { + 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/ledControlls.cpp b/src/ledControlls.cpp index b9f45a8..73b6383 100644 --- a/src/ledControlls.cpp +++ b/src/ledControlls.cpp @@ -1,7 +1,22 @@ +/** + * @file ledControlls.cpp + * @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 + * + * @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) diff --git a/src/main.cpp b/src/main.cpp index 31fc94c..4ec4343 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() { @@ -29,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(); @@ -45,38 +45,39 @@ void loop() updateLeds(maxVolume); // SW1 - Staff Volume Up - if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState)) + if (reader.debounceReadNC(BTN_STAFF_UP)) { staffVolumeUp(&maxVolume); } // SW2 - Staff Volume Down - if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState)) + if (reader.debounceReadNC(BTN_STAFF_DWN)) { - staffVolumeDown(maxVolume, &userVolume, &oldUserVolume); + staffVolumeDown(&maxVolume, &userVolume, &oldUserVolume); } // Remote D - Mute - if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState)) + if (reader.debounceReadNO(REMOTE_D)) { mute(&userVolume, &oldUserVolume, maxVolume); } // Remote A - Significantly Lower Volume - if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState)) + 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 (debounceRead(REMOTE_B, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) + if (reader.debounceReadNO(REMOTE_B) && !(oldUserVolume > userVolume)) { volumeUp(&userVolume, &oldUserVolume, maxVolume); } // Remote C - Volume Down - if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume)) + // 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..11704ea 100644 --- a/src/potControlls.cpp +++ b/src/potControlls.cpp @@ -1,7 +1,26 @@ +/** + * @file potControlls.cpp + * @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 #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 ad28b9b..f445899 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, 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,31 +28,31 @@ 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 diff --git a/src/userFunctions.cpp b/src/userFunctions.cpp index eabe136..20b4450 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, 571147@student.fontys.nl) * @brief Here are all the implementations for the user functions. * @version 1.0 * @date 2025-10-23