| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Arduino button debounce library for various switch types, port expanders and other 8-bit data sources. Fast and robust debounce algorithm.
To use this library
#include <Toggle.h>Toggle();
Toggle(inA);
Toggle(inA, inB);
Toggle(*in);The constructor defines a button object. If the default constructor is used when declaring an array of pointers to button objects, then it must be followed by a call to begin in setup.
inA: Arduino pin number that the button or switch is connected to (byte) , or *in: Arduino variable representing the input signal (byte)
inB: Second Arduino pin number (byte). Use 2 inputs when connecting to 3 position switches.
None.
Toggle myInput(2); // Button connected pin 2 to GND, INPUT_PULLUP, debounced
Toggle myInput(2, 3); // SPDT switch connected pins 2 and 3 to GND, INPUT_PULLUP, debounced
Toggle myInput(*Input); // Byte variable as input, debouncedThis function is placed at the top of the loop. Initializes the Button object and the pin it is connected to.
myInput.poll();
bit: selects the bit number from an input data (byte)
None.
There are 5 primary functions when using 1 input pin or data bit. Shown below is a plot showing the returned values that are verically offset for clarity:
This function checks the status register to see if the button or switch has changed state and reports whether the onPress or onRelease flag has been set. Calling onChange() does not modify the status register.
myInput.onChange();
None.
no change (0) , onPress (1), onRelease (2) (byte)
if (myInput.onChange() == 2) {
// button was released
} else if (myInput.onChange() == 1) {
// button was pressed
} else {
// no change
}These functions check the the status register to see if the button or switch has set the onPress or onRelease flag. These functions will return the flag status and clear the respective flag.
myInput.onPress();
myInput.onRelease();
None.
true or false, (bool)
if (myInput.onPress())
{
// do something (true only once per press)
}These functions checks the curent debounced output and its history to see if the button or switch is pressed or released.
myInput.isPressed();
myInput.isReleased();
bit: selects the bit number from an input data (byte)
true or false, (bool)
if (myButton.isPressed()) {
// do something
}
else {
// do something else
}This function can be used to convert a momentary push button to a toggle switch. By default, the retuen value will toggle true-false then false-true for each onPress action of the button.
None.
true or false, (bool). Toggles as configured by setToggleTrigger(). Default is trigger onPress
Simply clears the ms timer used for the timer functions.
myInput.clearTimer();
None.
None.
This function sets the duration in milliseconds that the returned value is true. The mode parameter sets what blink responds to: onChange (0), onPress( 1) default, onRelease (2).
myInput.blink(ms);
ms: The number of milliseconds (unsigned int)
mode: Blink onChange (0), onPress( 1) default, onRelease (2) (byte)
true or false, depending on whether the elapsed time has expired after any state change set by the mode parameter. (bool)
These functions return true if the button or switch has been in the pressedFor or releasedFor state for at least the given number of milliseconds.
myInput.pressedFor(ms);
myInput.releasedFor(ms);
ms: The number of milliseconds (unsigned int)
true or false, depending on whether the elapsed time has expired after the state change. (bool)
if (myInput.pressedFor(500)) {
// true (once only) if button has been pressed for 500ms
}This function checks the duration in milliseconds that the button or switch is is in the state as selected by timer mode and returns true (once only) each time the given millisecond duration has expired.
myInput.retrigger(ms);
ms: The number of milliseconds (unsigned int)
true or false, returns true (once only) each time the given ms duration has expired while the button is in the state as selected by timer mode. (bool)
if (retrigger(500)) {
// count every 500ms interval while the button is being pressed
} byte pCode = sw1.pressCode(1); // (1) serial print resultsThis function sets the various options for the input source.
myInput.inMode::input_option;
None.
myInput.setInputMode(sw1.inMode::input_input); // high impedance input
myInput.setInputMode(sw1.inMode::input_pullup); // pullup resistor enabled (default)
myInput.setInputMode(sw1.inMode::input_pulldown); // pulldown resistor enabled (ESP32)
myInput.setInputMode(sw1.inMode::input_byte); // input byte (8-bit)Set true if the button or switch pulls the signal high when pressed. Default is false (button or switch pulls the signal low when pressed).
myInput.setInputInvert(true);
None.
Sets the sample period in microseconds. Default is 5000 μs.
myInput.setSampleUs(us);
None.
Gets the elapsed ms since the last state change selected by timer mode.
myInput.getElapsedMs();
Elapsed milliseconds (unsigned int).
| Back | FazBrowse Home | New Git URL |