Debounce
Last updated
Was this helpful?
Last updated
Was this helpful?
When we use a push button as a toggle switch, it often generates some flickering effects, due to mechanical and physical limitations. I.e, the processor may recognize the button being pushed several times in a short period of time. This example demonstrates how to debounce an input, which means checking twice in a short period of time to make sure the push-button is definitely being pressed, and only being pressed once.
SwiftIO board
Button
Jumper wires
Swift has a basic Boolean type, called Bool. Boolean values are referred to as logical, because they can be either true
or false
. Swift provides two Boolean constant values, true and false. var triggered
is define as Bool to store whether the DigitalInput is change.
Due to mechanical and physical issues, push-buttons often generate spurious open/close transitions when pressed, these transitions may be read as multiple presses in a very short time fooling the program. This example demonstrates how to debounce an input, which means checking twice in a short period of time to make sure the push-button is definitely pressed. Without debouncing, pressing the button once may cause unpredictable results. This code uses the sleep(ms: 1)
50 times to keep track of the time passed since the button was pressed.
- The DigitalOut class is used to set a High or Low voltage output to a digital output pin.
- The Mode enumerate includes the available input modes.