DebouncedInput - PaulMurrayCbr/DebounceInput GitHub Wiki

DebouncedInput attaches to an input pin, providing methods to detect debounced state and state changes on the pin. DebouncedInput is a simple/easy wrapper using an underlying DebounceFilter4ms to do the work.

The one-argument constructor initialises the pin to INPUT_PULLUP, which is the usual case for most sketches. If you don't want this, then use the no-arg constructor and attach() the pin in your setup().

The pin should be 'read' frequently - at the top of your loop( ) where you usually read all of your inputs. The underlying code will sample the pin no more than once every 4ms. To bypass this, you will need to use DebounceFilter directly rather than use this easy wrapper class.

The state change functions will indicate if the state changed as a result of the last read. This is done irrespective of whether or not the pin was actually sampled. This means that if you do two reads in sucession, by calling read() or by calling the convenience readXX() methods, then the second call will reset the state-chage bit.

So don't do that.

See the Examples for usage tips.

Constructor

DebounceInput

Construct an instance unattached to a pin. The debounced signal will be a constant false (LOW).

DebounceInput(int pin)

Use pin as a source of input samples. Performs an initial pinMode(pin, INPUT_PULLUP) on the pin. The pin is read and the filter is reset to the current state of the pin to stop spurious state changes.

If you do not want the pinMode set to INPUT_PULLUP, use the no-argument constructor and attach() the pin in your setup().

Members

byte risingThreshhold

A value which the internal filter must exceed to change from false (LOW) to true (HIGH). The default value is 0x90. The internal filter has a maximum value of 255, which means that if you set risingThreshhold to 255, then the filter can never rise and will remain stuck in the false (LOW) state.

byte fallingThreshhold

A value which the internal filter must be less than to change from true (HIGH) to false (LOW). The default value is 0x70. The internal filter has a minimum value of 0, which means that if you set fallingThreshhold to 0, then the filter can never fall and will remain stuck in the true (HIGH) state.

Methods

void attach(int pin)

Attach the DebouncedInput to a pin. The pin is read and the filter is reset to the current state of the pin to stop spurious state changes.

void detach()

Disconnects from the current pin (if any). The filter is reset to false (LOW).

boolean attached()

True if the DebouncedInput is currently attached to a pin.

boolean read()

Read the pin, return the debounced state.

boolean high()

true if the debounced signal is HIGH.

boolean low()

true if the debounced signal is LOW. This is a convenience method mainly intended for normally-open pushbuttons. .low() means the pushbutton is being held down.

boolean changing()

true if the debounced signal changed state *as a result of the most recent read().

boolean falling()

true if the debounced signal changed from HIGH to LOW as a result of the most recent read(). In the case of a pushbutton, this means that the button was pressed.

boolean rising()

true if the debounced signal changed from LOW to HIGH as a result of the most recent read(). In the case of a pushbutton, this means that the button was released.

boolean readChanging()

Perform a read(), then return true if the debounced signal changed state as a result.

boolean readFalling()

Perform a read(), then return true if the debounced signal went from HIGH to LOW as a result.

boolean readRising()

Perform a read(), then return true if the debounced signal went from LOW to HIGH as a result.