Skip to content

Commit

Permalink
Improvements for noise immunity. Closes #33.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Nov 1, 2024
1 parent 40f09af commit 915bb0e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=JC_Button
version=2.1.4
version=2.1.5
author=Jack Christensen <[email protected]>
maintainer=Jack Christensen <[email protected]>
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.
paragraph=Copyright (C) 2018-2019 by Jack Christensen and licensed under GNU GPL v3.0.
paragraph=Copyright (C) 2018-2024 by Jack Christensen and licensed under GNU GPL v3.0.
category=Signal Input/Output
url=https://github.com/JChristensen/JC_Button
architectures=*
34 changes: 24 additions & 10 deletions src/JC_Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,32 @@ void Button::begin()
// does debouncing, captures and maintains times, previous state, etc.
bool Button::read()
{
uint32_t ms = millis();
m_time = millis();
bool pinVal = digitalRead(m_pin);
if (m_invert) pinVal = !pinVal;
if (ms - m_lastChange < m_dbTime) {
m_changed = false;
}
else {
m_lastState = m_state;
m_state = pinVal;
m_changed = (m_state != m_lastState);
if (m_changed) m_lastChange = ms;

switch (m_fsm) {
case STABLE:
if (pinVal != m_state) { // maybe a change, but debounce first
m_dbStart = m_time;
m_fsm = DEBOUNCE;
}
else { // nothing to see here
m_changed = false;
}
break;

case DEBOUNCE:
if (m_time - m_dbStart >= m_dbTime) {
m_fsm = STABLE;
if (pinVal != m_state) { // a real change (else just noise)
m_lastState = m_state;
m_state = pinVal;
m_lastChange = m_time;
m_changed = true;
}
}
break;
}
m_time = ms;
return m_state;
}
13 changes: 8 additions & 5 deletions src/JC_Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,18 @@ class Button
uint32_t lastChange() const {return m_lastChange;}

private:
enum fsmStates_t {STABLE, DEBOUNCE}; // states for the state machine
fsmStates_t m_fsm {STABLE}; // initial state machine state
uint8_t m_pin; // arduino pin number connected to button
uint32_t m_dbTime; // debounce time (ms)
bool m_puEnable; // internal pullup resistor enabled
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
bool m_state = false; // current button state, true=pressed
bool m_lastState = false; // previous button state
bool m_changed = false; // state changed since last read
uint32_t m_time = 0; // time of current state (ms from millis)
uint32_t m_lastChange = 0; // time of last state change (ms)
bool m_state {false}; // current button state, true=pressed
bool m_lastState {false}; // previous button state
bool m_changed {false}; // state changed since last read
uint32_t m_time {0}; // time of current state (ms from millis)
uint32_t m_lastChange {0}; // time of last state change (ms)
uint32_t m_dbStart; // debounce interval start time
};

// a derived class for a "push-on, push-off" (toggle) type button.
Expand Down

0 comments on commit 915bb0e

Please sign in to comment.