Added debounce read function
This commit is contained in:
37
src/main.cpp
37
src/main.cpp
@@ -7,8 +7,14 @@ int maxVolume = 64;
|
|||||||
int userVolume = 64;
|
int userVolume = 64;
|
||||||
int oldUserVolume = userVolume;
|
int oldUserVolume = userVolume;
|
||||||
|
|
||||||
|
int buttonState;
|
||||||
|
int lastButtonState = LOW;
|
||||||
|
unsigned long lastDebounceTime = 0;
|
||||||
|
unsigned long debounceDelay = 50;
|
||||||
|
|
||||||
void volumeUp();
|
void volumeUp();
|
||||||
void volumeDown();
|
void volumeDown();
|
||||||
|
bool debounceRead();
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
@@ -272,4 +278,35 @@ void volumeDown()
|
|||||||
digitalWrite(9, LOW);
|
digitalWrite(9, LOW);
|
||||||
delay(1);
|
delay(1);
|
||||||
digitalWrite(10, HIGH);
|
digitalWrite(10, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user