development #4

Merged
elduko merged 6 commits from development into main 2025-10-31 15:22:25 +01:00
4 changed files with 48 additions and 38 deletions
Showing only changes of commit 194e608f9e - Show all commits

View File

@@ -1,3 +1,13 @@
#pragma once
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState);
class Reader
{
private:
unsigned long lastDebounceTime = 0;
bool lastButtonState = LOW;
bool buttonState = HIGH;
int lastPin;
public:
bool debounceRead(int buttonPin);
};

View File

@@ -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;

View File

@@ -2,33 +2,35 @@
#include <commonFunctions.h>
#include <constants.h>
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)
bool buttonPressed = false;
if (reading != lastButtonState && lastPin == buttonPin)
{
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
lastDebounceTime = millis(); // reset debounce timer
}
// if the button state has changed:
if (reading != *buttonState)
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
{
*buttonState = reading;
if (*buttonState == HIGH)
return true;
else
return false;
// If button state is stable and just changed to pressed
if (reading == HIGH && buttonState == LOW)
{
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;
}

View File

@@ -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);
}