This is a library for Arduino to easily handle momentary switches, such as tact switches.
This library performs "debouncing" of switch contacts and can detect various switch actions such as "single/double clicks", "long presses", and "automatic repetitions" when a switch is pressing continuously.
These events can be detected by getter functions such as “isClicked()”. The detection like "Event-Driven" style is also possible by assigning a callback function to each switch action.
Needs to create an instance of VersatileSwitch for each switch. The required argument is a "pin number". All digital input pins can be used for switch.
VersatileSwitch mySwitch(4);If only the “pin number” is specified, the switch is set in
- “connection mode” of switch is
INPUT_PULLUP - "pin voltage" is
LOW, when switch isON
These “connection mode” and “pin voltage” can be given as optional arguments of constructor.
VersatileSwitch mySwitch1(3, INPUT_PULLUP);
VersatileSwitch mySwitch2(4, INPUT, LOW);The “connection mode” is the 2nd argument, and either INPUT or INPUT_PULLUP is given (default value is INPUT_PULLUP ).
The "pin voltrage" is the 3rd argument, and one of LOW, HIGH, or AUTO is given (default value is AUTO ).
The 3rd argument AUTO means the following:
HIGHwhen the 2nd argument isINPUT.LOWwhen the 2nd argument isINPUT_PULLUP.
For all instances, poll() is called periodically to check and update the switch state. This will be usually done within loop().
After calling poll() to check and update the switch state, they can be retrieved with the getter function.
void loop() {
mySwitch.poll();
if (mySwitch.isClicked()) {
Serial.println("Clicked.");
} else if (mySwitch.isLongClicked()) {
Serial.println("Long-clicked.");
}
}Also, assigning a callback function to a switch operation in setup() , the callback function will be called automatically in poll() when those operations are performed.
void setup() {
mySwitch.attachCallback_Clicked(on_switch_clicked);
mySwitch.attachCallback_LongClicked(on_switch_long_clicked);
}
void loop() {
mySwitch.poll();
}
void on_switch_clicked() {
Serial.println("Clicked.");
}
void on_switch_long_clicked() {
Serial.println("Long-clicked.");
}Returns true if the switch is in the "Pressed ( ON )" state.
Returns true if the switch is in the "Released ( OFF )" state.
Returns true if the switch continues to be pressed and is in the “automatic repetitions” state.
Note: In this “automatic repetitions” state, isPressed() returns false .
Returns true if the switch action "click / double-click / long-click" has been determined in poll() just before calling this function. These functions is transient, returning true only after the poll() when the switch action is finalized.
Attach a callback function for each switch action. The type of the argument is void(*)(void) pointer of function, like a following:
void func() {
...
}Each function is callbacked from within poll() when the switch action confirmed.
attachCallback_Finalized() is specially callbacked when a sequencial action such as a click or double-click is finalized. See "About inner-works and timing charts of callback" below for details.
Sets the time constant for debouncing, called as "time of paralyze ( TParalyze )", in msec. The default value is "5".
Sets the time constant to change the state from "start of pressing" to "automatic repetitions", called as "time of pressing ( TPressing )", in msec. The default value is "500".
Sets the interval in "automatic repetitions", called as "time of repeating ( TRepeat )", in msec. The default value is "500".
Sets the time constant, called as "time for accept double click ( TAccept )", needed to confirm that successive clicks are double clicks, not two single clicks, in msec. The default value is "200".
For each poll(), checks the switch position [ ON, OFF ], and if it is different from the prevoius one, state of the switch will be "Paralyzed". This "Paralyzed" state will be continued during " TParalyze " and no action are occured.
At the first poll() after the " TParalyze " has expired, the switch position is checked again and Pressed or Released is callbacked depending on its position. The status of the switch will accordingly change to “RELEASED” or “PRESSED”.
Note that callbacked aReleased, just means that the switch is released, not that a action such as a click or double-click is finalized.
After the status has been changed to “PRESSED”, if the switch was still being pressed at the first poll() after the “TPressing” time expired, the status will be changed to “HELD”. At that timing Held -> Repeated will be callbacked.
Thereafter, Repeated is callbacked for each first poll() beyond “TRepeat” time.
Then, Released -> LongClicked -> Finalized are callebacked at the first poll() after the switch is released.
After the status becomes “PRESSED”, If the switch is released within “TPressing” time , the status will be changed to the special status “RELEASED_AFTER_CLICK”.
In “RELEASED_AFTER_CLICK” state, if the switch is not re-pressed within “TAccept” time, the single-click is confirmed and Released -> Clicked -> Finalized are callbacked, then the status returns to "RELEASED".
In the “RELEASED_AFTER_CLICK” state, if the switch is pressed again within the “TAccept” time, the status changes to another special state called “PRESSED_AFTER_CLICK".
After “PRESSED_AFTER_CLICK”, if the switch is released within “TPressing”, the double-click is confirmed and Released -> DoubleClicked -> Finalized are callbacked, then the status returns to "RELEASED".
After a single-click was confirmed and became in the “PRESSED_AFTER_CLICK”, if the switch is not released within “TPressing”, this sequencial action is confirmed as “Click & Hold”. The Clicked -> Held -> Repeated are callbacked, and then the status is changed to "HELD*".
Note that Finalized is not callbacked after Clicked, unlike in the normal single-click case.
After this, it is the same as the normal "continuous press": Repeated is callbacked for each “TRepeat” time and Released -> LongClicked -> Finalized are callebacked at the timing of releasing switch.




