Added debounce read function

This commit is contained in:
2025-10-08 13:53:32 +02:00
parent 48810ee50c
commit 1435ed32d8

View File

@@ -7,8 +7,14 @@ int maxVolume = 64;
int userVolume = 64;
int oldUserVolume = userVolume;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void volumeUp();
void volumeDown();
bool debounceRead();
void setup()
{
@@ -272,4 +278,35 @@ void volumeDown()
digitalWrite(9, LOW);
delay(1);
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;
}