development #4
@@ -1,3 +1,13 @@
|
|||||||
#pragma once
|
#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);
|
||||||
|
};
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
const int DEBOUNCE_DELAY = 50;
|
const int DEBOUNCE_DELAY = 200;
|
||||||
|
|
||||||
const int USER_STEPS = 2;
|
const int USER_STEPS = 2;
|
||||||
const int STAFF_STEPS = 16;
|
const int STAFF_STEPS = 16;
|
||||||
|
|||||||
@@ -2,33 +2,35 @@
|
|||||||
#include <commonFunctions.h>
|
#include <commonFunctions.h>
|
||||||
#include <constants.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)
|
||||||
if (reading != *lastButtonState)
|
|
||||||
{
|
{
|
||||||
// reset the debouncing timer
|
lastPin = buttonPin;
|
||||||
*lastDebounceTime = millis();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((millis() - *lastDebounceTime) > DEBOUNCE_DELAY)
|
bool buttonPressed = false;
|
||||||
{
|
|
||||||
// whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
|
|
||||||
|
|
||||||
// if the button state has changed:
|
if (reading != lastButtonState && lastPin == buttonPin)
|
||||||
if (reading != *buttonState)
|
|
||||||
{
|
{
|
||||||
*buttonState = reading;
|
lastDebounceTime = millis(); // reset debounce timer
|
||||||
|
|
||||||
if (*buttonState == HIGH)
|
|
||||||
return true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
||||||
*lastButtonState = reading;
|
{
|
||||||
|
// If button state is stable and just changed to pressed
|
||||||
|
if (reading == HIGH && buttonState == LOW)
|
||||||
|
{
|
||||||
|
buttonPressed = true;
|
||||||
|
}
|
||||||
|
buttonState = reading;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lastPin == buttonPin)
|
||||||
|
{
|
||||||
|
lastButtonState = reading;
|
||||||
|
}
|
||||||
|
return buttonPressed;
|
||||||
}
|
}
|
||||||
32
src/main.cpp
32
src/main.cpp
@@ -10,9 +10,7 @@ int maxVolume = 64;
|
|||||||
int userVolume = 64;
|
int userVolume = 64;
|
||||||
int oldUserVolume = userVolume;
|
int oldUserVolume = userVolume;
|
||||||
|
|
||||||
int buttonState;
|
Reader reader;
|
||||||
int lastButtonState = LOW;
|
|
||||||
unsigned long lastDebounceTime = 0;
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
@@ -44,39 +42,39 @@ void loop()
|
|||||||
{
|
{
|
||||||
updateLeds(maxVolume);
|
updateLeds(maxVolume);
|
||||||
|
|
||||||
// SW1 - Staff Volume Up
|
// // SW1 - Staff Volume Up
|
||||||
if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState))
|
// if (!reader.debounceRead(BTN_STAFF_UP))
|
||||||
{
|
// {
|
||||||
staffVolumeUp(&maxVolume);
|
// staffVolumeUp(&maxVolume);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// SW2 - Staff Volume Down
|
// // SW2 - Staff Volume Down
|
||||||
if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState))
|
// if (!reader.debounceRead(BTN_STAFF_DWN))
|
||||||
{
|
// {
|
||||||
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
|
// staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Remote D - Mute
|
// Remote D - Mute
|
||||||
if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState))
|
if (reader.debounceRead(REMOTE_D))
|
||||||
{
|
{
|
||||||
mute(&userVolume, &oldUserVolume, maxVolume);
|
mute(&userVolume, &oldUserVolume, maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote A - Significantly Lower Volume
|
// Remote A - Significantly Lower Volume
|
||||||
if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState))
|
if (reader.debounceRead(REMOTE_A))
|
||||||
{
|
{
|
||||||
lowerVolume(&userVolume, &oldUserVolume, maxVolume);
|
lowerVolume(&userVolume, &oldUserVolume, maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote B - Volume Up
|
// Remote B - Volume Up
|
||||||
// Make sure we are not is a state of mute or lowered
|
// 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);
|
volumeUp(&userVolume, &oldUserVolume, maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote C - Volume Down
|
// Remote C - Volume Down
|
||||||
if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume))
|
if (reader.debounceRead(REMOTE_C) && !(oldUserVolume > userVolume))
|
||||||
{
|
{
|
||||||
volumeDown(&userVolume, &oldUserVolume);
|
volumeDown(&userVolume, &oldUserVolume);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user