Serial Wombat Arduino Library
Public Member Functions | Public Attributes | List of all members
SerialWombatDebouncedInput Class Reference

A pin mode class that debounces inputs. More...

#include <SerialWombatDebouncedInput.h>

Public Member Functions

 SerialWombatDebouncedInput (SerialWombat &serialWombat)
 Constructor for the SerialWombatDebouncedInput class. More...
 
void begin (uint8_t pin)
 Initialize a debounced input (simplified for typical switch to ground) More...
 
void begin (uint8_t pin, uint16_t debounce_mS, bool invert, bool usePullUp)
 Initialize a debounced input. More...
 
bool digitalRead ()
 Returns the debounced state of the input. More...
 
uint16_t readDurationInTrueState_mS ()
 return the number of mS that the debounced input has been in true state More...
 
uint16_t readDurationInFalseState_mS ()
 return the number of mS that the debounced input has been in false state More...
 
bool readTransitionsState ()
 Queries the number of transistions that have occured on the debounced input. More...
 

Public Attributes

uint16_t transitions = 0
 Number of transitions returned by last call to readTransitionsState() More...
 

Detailed Description

A pin mode class that debounces inputs.

The SerialWombatDebouncedInput class is used to debounce inputs such as switches.

See the example sw4b_ard_Debounce1 distributed with the Serial Wombat Arduino Library for an example.

Video Tutorial

https://youtu.be/_EKlrEVaEhg

One SerialWombatDebouncedInput instance should be declared per debounced input.

The class sends commands and reads data from the Serial Wombat. The actual switch debouncing algorithm runs on the Serial Wombat.

The Serial Wombat's firmware algorithm polls the input every 1mS. After it has been different from the reported value for X consecutive mS, the reported value changes to the new polled value. The number of mS required for a change can be configured.

The Debounced Input mode keeps track of how long the debounced result has been in the current state (up to 65535 mS) and can report this to the host. This is useful for implementing user interfaces that react based on how long a button has been held.

The Debounced Input mode keeps track of how many transitions have occured. This can be used to poll the Debounced Input infrequently for status, but still process all button presses/releases that occured since the last poll.

The pin mode has weak pull-up circuitry availble, and the ability to report inverted values. These are both enabled when the simpliest begin() call is used, allowing buttons that switch the Serial Wombat pin to ground to be used without additional hardware.

See also the SerialWombatButtonCounter class which can run on top of this one.

Constructor & Destructor Documentation

◆ SerialWombatDebouncedInput()

SerialWombatDebouncedInput::SerialWombatDebouncedInput ( SerialWombat serialWombat)

Constructor for the SerialWombatDebouncedInput class.

Parameters
serialWombata reference to the Serial Wombat on which the Debounced Input will exist

Member Function Documentation

◆ begin() [1/2]

void SerialWombatDebouncedInput::begin ( uint8_t  pin)

Initialize a debounced input (simplified for typical switch to ground)

This simplified initialization assumes a typical switch between the Serial Wombat pin and ground. Pull ups are turned on, debounce time is set to 30 mS, and inverted reporting is turned on so that a closed switch results in a TRUE reading.

Parameters
pinThe Serial Wombat pin used for the debounced input

◆ begin() [2/2]

void SerialWombatDebouncedInput::begin ( uint8_t  pin,
uint16_t  debounce_mS,
bool  invert,
bool  usePullUp 
)

Initialize a debounced input.

Parameters
pinThe Serial Wombat pin used for the debounced input
debounce_mSnumber of mS the pin must be stable to cause a transition
invertFALSE: pin reading is returned TRUE: inverted pin reading is returned
usePullUpWhether the pin's weak pull up is enabled

◆ digitalRead()

bool SerialWombatDebouncedInput::digitalRead ( )

Returns the debounced state of the input.

This function reads from the public data of the pin which indicates the debounced and invert adjusted state of the input

Returns
TRUE or FALSE. Meaning depends on inversion setting

◆ readDurationInFalseState_mS()

uint16_t SerialWombatDebouncedInput::readDurationInFalseState_mS ( )

return the number of mS that the debounced input has been in false state

Returns
returns a value in mS which saturates at 65535. Returns 0 if currently true.

◆ readDurationInTrueState_mS()

uint16_t SerialWombatDebouncedInput::readDurationInTrueState_mS ( )

return the number of mS that the debounced input has been in true state

Returns
returns a value in mS which saturates at 65535. Returns 0 if currently false.

◆ readTransitionsState()

bool SerialWombatDebouncedInput::readTransitionsState ( )

Queries the number of transistions that have occured on the debounced input.

This function queries the debounced input for current state and transitions since last call. transition count is put in the global member transitions. The debounced input in the Serial Wombat resets its count to zero after this call.

Returns
TRUE or FALSE, current status of debounced input

Member Data Documentation

◆ transitions

uint16_t SerialWombatDebouncedInput::transitions = 0

Number of transitions returned by last call to readTransitionsState()