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:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user