|
1 | 1 | # Toggle [](https://www.ardu-badge.com/Toggle) [](https://registry.platformio.org/libraries/dlloydev/Toggle)
|
2 | 2 |
|
3 |
| -Arduino button library for debouncing switch contacts and input 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. |
| 3 | +Arduino button debounce library for various switch types, port expanders and other 8-bit data sources. Fast and robust debounce algorithm. |
4 | 4 |
|
5 | 5 | ## Features
|
6 | 6 |
|
7 |
| -### Flexible Inputs |
| 7 | +- Fast, robust and symetrical [debouncing](https://github.com/Dlloydev/Toggle/wiki/Debounce-Algorithm) of both press and release button transitions |
| 8 | +- Works with various [switch types](https://github.com/Dlloydev/Toggle/wiki/Switch-Connections) |
| 9 | +- Identifies 7 unique transitions for 3-position switches |
| 10 | +- Use momentary button as toggle switch (configurable) |
| 11 | +- Return up to 225 codes from one button |
8 | 12 |
|
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. See [examples](https://github.com/Dlloydev/Toggle/tree/main/examples). |
| 13 | +## Examples |
10 | 14 |
|
11 |
| -### Debounce Algorithm |
| 15 | +- Lets give some examples a [trial run](https://github.com/Dlloydev/Toggle/wiki/Wokwi-Examples). |
12 | 16 |
|
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. |
14 |
| - |
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 | |
25 |
| - |
26 |
| -### Sampling |
27 |
| - |
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): |
29 |
| - |
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!* |
31 |
| -
|
32 |
| -Using the Toggle library, this switch could be debounced with a 15ms sample period and ignoring dropouts of up to 45ms. |
33 |
| - |
34 |
| -## Switch Connections |
35 |
| - |
36 |
| -Switching between GND and digital pin is the default solution used by this library. External pull-up resistors can be added if desired, but the internal pullups should be sufficient for most applications. What might be of consideration is providing sufficient wetting current to overcome switch contact oxidation. |
37 |
| - |
38 |
| -A set of connections are shown where 0.1μF capacitors are optionally added. This adds contact wetting current and hardware signal filtering (beneficial for interrupt applications) . |
39 |
| - |
40 |
| - |
41 |
| - |
42 |
| -#### Connections shown below are for 2-position switches (SPST, SPDT, DPDT) using 1 input: |
43 |
| - |
44 |
| - |
45 |
| - |
46 |
| - |
47 |
| - |
48 |
| - |
49 |
| - |
50 |
| -#### Connections shown below are for 3-position switches (SP3T, DP3T) using 2 inputs: |
51 |
| - |
52 |
| - |
53 |
| - |
54 |
| -#### Toggle Switch (SP3T, On-Off-On): |
55 |
| - |
56 |
| - |
57 |
| - |
58 |
| - |
59 |
| - |
60 |
| -#### Slide Switch (Single Pole, 3-position): |
61 |
| - |
62 |
| - |
63 |
| - |
64 |
| - |
65 |
| - |
66 |
| -#### Rotary Switch (Double Pole, 3-position): |
67 |
| - |
68 |
| - |
69 |
| - |
70 |
| -#### The switch functions when using 2 input pins: |
71 |
| - |
72 |
| -``` |
73 |
| -isUP(); |
74 |
| -isMID(); |
75 |
| -isDN(); |
76 |
| -UPtoMID(); |
77 |
| -MIDtoDN(); |
78 |
| -DNtoMID(); |
79 |
| -MIDtoUP(); |
80 |
| -``` |
81 |
| - |
82 |
| -The switch has 3 positions referred to as UP, MID (center) and DN (down). The first 3 functions will continuously return true if the switch is at that position. The last 4 functions return true (once only) if the switch has just transitioned to that position. |
| 17 | +- See sketch [examples](https://github.com/Dlloydev/Toggle/tree/main/examples). |
83 | 18 |
|
84 | 19 |
|
85 | 20 |
|
@@ -401,9 +336,9 @@ if (retrigger(500)) {
|
401 | 336 | byte pCode = sw1.pressCode(1); // (1) serial print results
|
402 | 337 | ```
|
403 | 338 |
|
404 |
| -##### Example Sketch |
| 339 | +##### Example Sketch [Press_Code.ino](https://github.com/Dlloydev/Toggle/blob/main/examples/Press_Code/Press_Code.ino) |
405 | 340 |
|
406 |
| -[Press_Code.ino](https://github.com/Dlloydev/Toggle/blob/main/examples/Press_Code/Press_Code.ino) |
| 341 | +[Trial run on *WOKWi*](https://wokwi.com/projects/334284248581145170) |
407 | 342 |
|
408 | 343 |
|
409 | 344 |
|
|
0 commit comments