-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
32 lines (23 loc) · 1.47 KB
/
Button.cpp
File metadata and controls
32 lines (23 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Button.h"
Button::Button(int _value, int _tolerance)
{
this->buttonValue = _value;
this->buttonTolerance = _tolerance;
}
bool Button::buttonIsPressed()
{
buttonState = LOW; // the button is low by default
buttonBoardOutput = analogRead(buttonBoardPin); // read the button board output
if ((buttonBoardOutput >= (buttonValue - buttonTolerance)) && (buttonBoardOutput <= (buttonValue + buttonTolerance))) // if it is in the button range, set the button state HIGH
buttonState = HIGH;
if ((buttonState != lastButtonState)&&(millis()>(stateChangeTime + debounceDelay))) // if the button has changed state and the debounce delay has passed
{
if (buttonState == HIGH) // if it changed to HIGH
{
stateChangeTime = millis();
return true; // return TRUE
}
}
lastButtonState = buttonState; // log the button state
return false; // otherwise, return FALSE
}