|
1 | 1 | # Toggle [](https://www.ardu-badge.com/Toggle) [](https://registry.platformio.org/libraries/dlloydev/Toggle)
|
2 | 2 |
|
3 |
| - Arduino button library for deglitching and debouncing switch contacts and logic data. Works with all switch types, port expander and other 8-bit data sources. Three algorithm modes available that can ignore up to several consecutive spurious transitions. |
| 3 | + Arduino button library for debouncing switch contacts and logic data. New pressCode() function detects fast, short and long button presses for 225 combinations. Works with all switch types, port expanders and other 8-bit data sources. Debounce algorithm ignores several consecutive spurious transitions. |
4 | 4 |
|
5 | 5 | ## Features
|
6 | 6 |
|
7 | 7 | ### Flexible Inputs
|
8 | 8 |
|
9 | 9 | The inputs can be from a single pin or several pins allowing the use of 2 or 3-position switches and up to seven debounced states. When linking to a data (byte) input, the debouncer can work with any selected bit or it can debounce all 8-bits in one Toggle instance. Examples: [`Input_Bit_Test.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Bit_Test/Input_Bit_Test.ino) , [`Input_Bit.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Bit/Input_Bit.ino), [`Input_Port_Test.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Port_Test/Input_Port_Test.ino) and [`Input_Port.ino`](https://github.com/Dlloydev/Toggle/blob/main/examples/Input_Port/Input_Port.ino).
|
10 | 10 |
|
11 |
| -### Algorithms |
| 11 | +### Algorithm |
12 | 12 |
|
13 |
| -```c++ |
14 |
| -setAlgorithm(2); // Robust Mode, 2 glitches ignored |
15 |
| -setAlgorithm(1); // Average Mode, 1 glitch ignored |
16 |
| -setAlgorithm(0); // Common Mode, can respond to spurious transitions |
17 |
| -``` |
| 13 | +The debounce algorithm adds only 2 sample periods of time lag to the output signal. A 3-sample stable period is required for an output bit to change. Therefore, to set an output bit, 3 consecutive 1's are required. When 3 consecutive 0's are detected, that bit value is cleared. |
18 | 14 |
|
19 |
| -In Robust Mode, the algorithm adds only 2 sample periods of time lag to the output signal. A 3-sample stable period is required for an output bit to change. Therefore, to set an output bit, 3 consecutive 1's are required. When 3 consecutive 0's are detected, that bit value is cleared. |
| 15 | +| ppDat | pDat | dat | **R** | **S** | **Q** | **State** | |
| 16 | +| ----- | ---- | ---- | ----- | ----- | ---------- | --------- | |
| 17 | +| 0 | 0 | 0 | 1 | 0 | 0 | Reset | |
| 18 | +| 0 | 0 | 1 | 0 | 0 | Last State | No Change | |
| 19 | +| 0 | 1 | 0 | 0 | 0 | Last State | No Change | |
| 20 | +| 0 | 1 | 1 | 0 | 0 | Last State | No Change | |
| 21 | +| 1 | 0 | 0 | 0 | 0 | Last State | No Change | |
| 22 | +| 1 | 0 | 1 | 0 | 0 | Last State | No Change | |
| 23 | +| 1 | 1 | 0 | 0 | 0 | Last State | No Change | |
| 24 | +| 1 | 1 | 1 | 0 | 1 | 1 | Set | |
20 | 25 |
|
21 | 26 | ### Sampling
|
22 | 27 |
|
23 |
| -Rather than use a basic timer strategy, the Toggle library uses sampling and only requires up to three samples on the input to to provide a clean (debounced) output. The sample period defaults to 5000 μs (5 ms) which works well the default Robust Mode. With these defaults, only 15ms is required for detecting a button switch being pressed or released. This may seem low when thinking of regular debouncig, but in order for this method to falsely detect a transition, it would require that there be a gap of greater than 15ms between bounces. From *[A Guide to Debouncing](http://www.ganssle.com/item/debouncing-switches-contacts-code.htm)*, (Anatomy of a Bounce): |
| 28 | +The sample period defaults to 5000 μs. With this setting, only 15ms is required for detecting a button switch being pressed or released. This may seem low when thinking of regular debouncig, but in order for this method to falsely detect a transition, it would require that there be a gap of greater than 15ms between bounces. From *[A Guide to Debouncing](http://www.ganssle.com/item/debouncing-switches-contacts-code.htm)*, (Anatomy of a Bounce): |
24 | 29 |
|
25 | 30 | > *Consider switch E again, that one with the pretty face that hides a vicious 157 msec bouncing heart. One test showed the switch going to a solid one for 81 msec, after which it dropped to a perfect zero for 42 msec before finally assuming its correct high state. Think what that would do to pretty much any debounce code!*
|
26 | 31 |
|
@@ -117,11 +122,8 @@ None.
|
117 | 122 | ##### Example
|
118 | 123 |
|
119 | 124 | ```c++
|
120 |
| -/* a button or switch is connected from pin 2 to ground |
121 |
| - 5000 μs sample interval (default) |
122 |
| - pullup enabled (default) |
123 |
| - inverted = false (default) |
124 |
| - algorithm = 2 glitches "Robust Mode" (default) */ |
| 125 | +// a button or switch is connected from pin 2 to ground, 5000 μs sample interval (default), |
| 126 | +// pullup enabled (default), inverted = false (default) |
125 | 127 | Toggle myInput(2);
|
126 | 128 |
|
127 | 129 | // same as above, but a 3 position switch is connected to pins 2 and 3
|
@@ -155,7 +157,7 @@ None.
|
155 | 157 |
|
156 | 158 | ## Primary Functions
|
157 | 159 |
|
158 |
| -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. Here, the algorithm was set to 0 glitches. In this case, the debounce interval is hard coded to 10 sample periods. The response is near instant "press" detection and delayed "release" detection which is similar to how most common debouncers operate: |
| 160 | +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: |
159 | 161 |
|
160 | 162 | 
|
161 | 163 |
|
@@ -476,26 +478,6 @@ Elapsed milliseconds *(unsigned int)*.
|
476 | 478 |
|
477 | 479 |
|
478 | 480 |
|
479 |
| -## setAlgorithm() |
480 |
| - |
481 |
| -##### Description |
482 |
| - |
483 |
| -Sets the debouncer algorithm to one of three modes. |
484 |
| - |
485 |
| -- **Robust Mode (2):** This is the default mode where up to 2 spurious signal transitions (glitches) are ignored. This adds 2 sample periods time lag to the output signal. |
486 |
| -- **Average Mode (1):** This is mode ignores up to 1 spurious signal transition (glitch) and adds 1 sample period time lag to the output signal. |
487 |
| -- **Common Mode (0):** This is mode is similar to most debouncers where the response is near instant to a button or switch press, and the release won't be recognized until a debounce time period has expired. In this case, the debounce time period is calculated and set at 10 times the sample period. |
488 |
| - |
489 |
| -##### Syntax |
490 |
| - |
491 |
| -`myInput.setAlgorithm(2);` |
492 |
| - |
493 |
| -##### Returns |
494 |
| - |
495 |
| -Control and Status Register (csr) value *(byte)*. |
496 |
| - |
497 |
| - |
498 |
| - |
499 | 481 | ---
|
500 | 482 |
|
501 | 483 |
|
|
0 commit comments