Skip to content

Commit 915bb0e

Browse files
committed
Improvements for noise immunity. Closes #33.
1 parent 40f09af commit 915bb0e

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

library.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=JC_Button
2-
version=2.1.4
2+
version=2.1.5
33
author=Jack Christensen <jack.christensen@outlook.com>
44
maintainer=Jack Christensen <jack.christensen@outlook.com>
55
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.
6-
paragraph=Copyright (C) 2018-2019 by Jack Christensen and licensed under GNU GPL v3.0.
6+
paragraph=Copyright (C) 2018-2024 by Jack Christensen and licensed under GNU GPL v3.0.
77
category=Signal Input/Output
88
url=https://github.com/JChristensen/JC_Button
99
architectures=*

src/JC_Button.cpp

+24-10
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,32 @@ void Button::begin()
2121
// does debouncing, captures and maintains times, previous state, etc.
2222
bool Button::read()
2323
{
24-
uint32_t ms = millis();
24+
m_time = millis();
2525
bool pinVal = digitalRead(m_pin);
2626
if (m_invert) pinVal = !pinVal;
27-
if (ms - m_lastChange < m_dbTime) {
28-
m_changed = false;
29-
}
30-
else {
31-
m_lastState = m_state;
32-
m_state = pinVal;
33-
m_changed = (m_state != m_lastState);
34-
if (m_changed) m_lastChange = ms;
27+
28+
switch (m_fsm) {
29+
case STABLE:
30+
if (pinVal != m_state) { // maybe a change, but debounce first
31+
m_dbStart = m_time;
32+
m_fsm = DEBOUNCE;
33+
}
34+
else { // nothing to see here
35+
m_changed = false;
36+
}
37+
break;
38+
39+
case DEBOUNCE:
40+
if (m_time - m_dbStart >= m_dbTime) {
41+
m_fsm = STABLE;
42+
if (pinVal != m_state) { // a real change (else just noise)
43+
m_lastState = m_state;
44+
m_state = pinVal;
45+
m_lastChange = m_time;
46+
m_changed = true;
47+
}
48+
}
49+
break;
3550
}
36-
m_time = ms;
3751
return m_state;
3852
}

src/JC_Button.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ class Button
6060
uint32_t lastChange() const {return m_lastChange;}
6161

6262
private:
63+
enum fsmStates_t {STABLE, DEBOUNCE}; // states for the state machine
64+
fsmStates_t m_fsm {STABLE}; // initial state machine state
6365
uint8_t m_pin; // arduino pin number connected to button
6466
uint32_t m_dbTime; // debounce time (ms)
6567
bool m_puEnable; // internal pullup resistor enabled
6668
bool m_invert; // if true, interpret logic low as pressed, else interpret logic high as pressed
67-
bool m_state = false; // current button state, true=pressed
68-
bool m_lastState = false; // previous button state
69-
bool m_changed = false; // state changed since last read
70-
uint32_t m_time = 0; // time of current state (ms from millis)
71-
uint32_t m_lastChange = 0; // time of last state change (ms)
69+
bool m_state {false}; // current button state, true=pressed
70+
bool m_lastState {false}; // previous button state
71+
bool m_changed {false}; // state changed since last read
72+
uint32_t m_time {0}; // time of current state (ms from millis)
73+
uint32_t m_lastChange {0}; // time of last state change (ms)
74+
uint32_t m_dbStart; // debounce interval start time
7275
};
7376

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

0 commit comments

Comments
 (0)