InputEvents v1.4.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
FoltmanDebounceAdapter.h
1#ifndef FoltmanDebounceAdapter_h
2#define FoltmanDebounceAdapter_h
3
4#include "Arduino.h"
5#include "DebounceAdapter.h"
6
12 public:
14 : DebounceAdapter(pinAdapter)
15 { }
16
17 void begin() {
19 lastChangeMs = millis();
20 nextState = lastState = pinAdapter->read();
21 }
22
23 bool read() override {
24 bool newState = pinAdapter->read();
25 if (nextState == lastState) {
26 // Steady state so far
27 if (newState != nextState) {
28 // Initiating state change
29 nextState = newState;
30 lastChangeMs = millis();
31 }
32 } else {
33 // Change pending
34 if (newState != nextState) {
35 // Glitch: reset the counter
36 nextState = lastState;
37 lastChangeMs = millis();
38 } else if (millis() - lastChangeMs >= debounceInterval) {
39 // Got debounceInterval ms of glitchless signal
40 lastState = newState;
41 }
42 }
43 return lastState;
44 }
45
46 private:
47 uint32_t lastChangeMs;
48 bool lastState, nextState;
49};
50#endif
This is the interface/base class for debounce adapters.
Definition: DebounceAdapter.h:11
void begin()
Initialise the debouncer and pin adapter. Must be safe for repeated calls (Idempotent)
Definition: DebounceAdapter.h:27
This is the default InputEvents debouncer. Many thanks to @kfoltman.
Definition: FoltmanDebounceAdapter.h:11
bool read() override
Return the debounced state of the pin adapter.
Definition: FoltmanDebounceAdapter.h:23
void begin()
Initialise the debouncer and pin adapter. Must be safe for repeated calls (Idempotent)
Definition: FoltmanDebounceAdapter.h:17
The interface specification for button, encoder button and switch pins.
Definition: PinAdapter.h:8
virtual bool read()=0
Read the current state of the pin.