Extracted debounceRead to seperate file
All checks were successful
Build Code / build (push) Successful in 1m17s
All checks were successful
Build Code / build (push) Successful in 1m17s
This commit is contained in:
3
include/commonFunctions.h
Normal file
3
include/commonFunctions.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState);
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
const int DEBOUNCE_DELAY = 50;
|
||||||
|
|
||||||
const int USER_STEPS = 2;
|
const int USER_STEPS = 2;
|
||||||
const int STAFF_STEPS = 16;
|
const int STAFF_STEPS = 16;
|
||||||
|
|
||||||
|
|||||||
36
src/commonFunctions.cpp
Normal file
36
src/commonFunctions.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <commonFunctions.h>
|
||||||
|
#include <constants.h>
|
||||||
|
|
||||||
|
bool debounceRead(int buttonPin, int *lastButtonState, unsigned long *lastDebounceTime, int *buttonState)
|
||||||
|
{
|
||||||
|
int reading = digitalRead(buttonPin);
|
||||||
|
|
||||||
|
// If the switch changed, due to noise or pressing:
|
||||||
|
if (reading != *lastButtonState)
|
||||||
|
{
|
||||||
|
// reset the debouncing timer
|
||||||
|
*lastDebounceTime = millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
|
||||||
|
// if the button state has changed:
|
||||||
|
if (reading != *buttonState)
|
||||||
|
{
|
||||||
|
*buttonState = reading;
|
||||||
|
|
||||||
|
if (*buttonState == HIGH)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
||||||
|
*lastButtonState = reading;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
49
src/main.cpp
49
src/main.cpp
@@ -4,6 +4,7 @@
|
|||||||
#include <ledControlls.h>
|
#include <ledControlls.h>
|
||||||
#include <userFunctions.h>
|
#include <userFunctions.h>
|
||||||
#include <staffFunctions.h>
|
#include <staffFunctions.h>
|
||||||
|
#include <commonFunctions.h>
|
||||||
|
|
||||||
int maxVolume = 64;
|
int maxVolume = 64;
|
||||||
int userVolume = 64;
|
int userVolume = 64;
|
||||||
@@ -12,9 +13,6 @@ int oldUserVolume = userVolume;
|
|||||||
int buttonState;
|
int buttonState;
|
||||||
int lastButtonState = LOW;
|
int lastButtonState = LOW;
|
||||||
unsigned long lastDebounceTime = 0;
|
unsigned long lastDebounceTime = 0;
|
||||||
unsigned long debounceDelay = 50;
|
|
||||||
|
|
||||||
bool debounceRead(int buttonPin);
|
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
@@ -47,72 +45,39 @@ void loop()
|
|||||||
updateLeds(maxVolume);
|
updateLeds(maxVolume);
|
||||||
|
|
||||||
// SW1 - Staff Volume Up
|
// SW1 - Staff Volume Up
|
||||||
if (!debounceRead(BTN_STAFF_UP))
|
if (!debounceRead(BTN_STAFF_UP, &lastButtonState, &lastDebounceTime, &buttonState))
|
||||||
{
|
{
|
||||||
staffVolumeUp(&maxVolume);
|
staffVolumeUp(&maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// SW2 - Staff Volume Down
|
// SW2 - Staff Volume Down
|
||||||
if (!debounceRead(BTN_STAFF_DWN))
|
if (!debounceRead(BTN_STAFF_DWN, &lastButtonState, &lastDebounceTime, &buttonState))
|
||||||
{
|
{
|
||||||
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
|
staffVolumeDown(maxVolume, &userVolume, &oldUserVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote D - Mute
|
// Remote D - Mute
|
||||||
if (debounceRead(REMOTE_D))
|
if (debounceRead(REMOTE_D, &lastButtonState, &lastDebounceTime, &buttonState))
|
||||||
{
|
{
|
||||||
mute(&userVolume, &oldUserVolume, maxVolume);
|
mute(&userVolume, &oldUserVolume, maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote A - Significantly Lower Volume
|
// Remote A - Significantly Lower Volume
|
||||||
if (debounceRead(REMOTE_A))
|
if (debounceRead(REMOTE_A, &lastButtonState, &lastDebounceTime, &buttonState))
|
||||||
{
|
{
|
||||||
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) && !(oldUserVolume > userVolume))
|
if (debounceRead(REMOTE_B, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume))
|
||||||
{
|
{
|
||||||
volumeUp(&userVolume, &oldUserVolume, maxVolume);
|
volumeUp(&userVolume, &oldUserVolume, maxVolume);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remote C - Volume Down
|
// Remote C - Volume Down
|
||||||
if (debounceRead(REMOTE_C) && !(oldUserVolume > userVolume))
|
if (debounceRead(REMOTE_C, &lastButtonState, &lastDebounceTime, &buttonState) && !(oldUserVolume > userVolume))
|
||||||
{
|
{
|
||||||
volumeDown(&userVolume, &oldUserVolume);
|
volumeDown(&userVolume, &oldUserVolume);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool debounceRead(int buttonPin)
|
|
||||||
{
|
|
||||||
int reading = digitalRead(buttonPin);
|
|
||||||
|
|
||||||
// If the switch changed, due to noise or pressing:
|
|
||||||
if (reading != lastButtonState)
|
|
||||||
{
|
|
||||||
// reset the debouncing timer
|
|
||||||
lastDebounceTime = millis();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((millis() - lastDebounceTime) > debounceDelay)
|
|
||||||
{
|
|
||||||
// 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 != buttonState)
|
|
||||||
{
|
|
||||||
buttonState = reading;
|
|
||||||
|
|
||||||
if (buttonState == HIGH)
|
|
||||||
return true;
|
|
||||||
else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// save the reading. Next time through the loop, it'll be the lastButtonState:
|
|
||||||
lastButtonState = reading;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user