Skip to content

Fix bug where isPressed returns True a second time #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions src/ezButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ezButton::ezButton(int pin, int mode) {
debounceTime = 0;
count = 0;
countMode = COUNT_FALLING;
transition = NEITHER;

if (mode == INTERNAL_PULLUP || mode == INTERNAL_PULLDOWN) {
pinMode(btnPin, mode);
Expand All @@ -54,9 +55,8 @@ ezButton::ezButton(int pin, int mode) {
unpressedState = HIGH;
}

previousSteadyState = digitalRead(btnPin);
lastSteadyState = previousSteadyState;
lastFlickerableState = previousSteadyState;
lastSteadyState = digitalRead(btnPin);
lastFlickerableState = lastSteadyState;

lastDebounceTime = 0;
}
Expand All @@ -74,17 +74,11 @@ int ezButton::getStateRaw(void) {
}

bool ezButton::isPressed(void) {
if(previousSteadyState == unpressedState && lastSteadyState == pressedState)
return true;
else
return false;
return transition == PRESSED;
}

bool ezButton::isReleased(void) {
if(previousSteadyState == pressedState && lastSteadyState == unpressedState)
return true;
else
return false;
return transition == UNPRESSED;
}

void ezButton::setCountMode(int mode) {
Expand Down Expand Up @@ -116,25 +110,21 @@ void ezButton::loop(void) {
lastFlickerableState = currentState;
}

if ((currentTime - lastDebounceTime) >= debounceTime) {
if ((currentTime - lastDebounceTime) >= debounceTime && currentState != lastSteadyState) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:

// save the the steady state
previousSteadyState = lastSteadyState;
// save the the steady state and set the transition
lastSteadyState = currentState;
}
transition = currentState == pressedState ? PRESSED : UNPRESSED;

if(previousSteadyState != lastSteadyState){
if(countMode == COUNT_BOTH)
count++;
else if(countMode == COUNT_FALLING){
if(previousSteadyState == HIGH && lastSteadyState == LOW)
count++;
}
else if(countMode == COUNT_RISING){
if(previousSteadyState == LOW && lastSteadyState == HIGH)
count++;
}
else if(countMode == COUNT_FALLING && currentState == LOW)
count++;
else if(countMode == COUNT_RISING && currentState == HIGH)
count++;
} else {
transition = NEITHER;
}
}
10 changes: 8 additions & 2 deletions src/ezButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
#define EXTERNAL_PULLUP 0xFE
#define EXTERNAL_PULLDOWN 0xFF

enum Transition : byte {
NEITHER,
PRESSED,
UNPRESSED
};

class ezButton
{
private:
Expand All @@ -59,9 +65,9 @@ class ezButton
int pressedState; // the state when the button is considered pressed
int unpressedState; // the state when the button is considered unpressed

int previousSteadyState; // the previous steady state from the input pin, used to detect pressed and released event
int lastSteadyState; // the last steady state from the input pin
int lastSteadyState; // the last steady state from the input pin, used to detect pressed and released event
int lastFlickerableState; // the last flickerable state from the input pin
Transition transition;

unsigned long lastDebounceTime; // the last time the output pin was toggled

Expand Down