1
1
Arduino OneButton Library
2
2
===
3
3
4
- This Arduino libary is improving the usage of a singe button for input.
4
+ This Arduino library is improving the usage of a singe button for input.
5
5
It shows how to use an digital input pin with a single pushbutton attached
6
6
for detecting some of the typical button press events like single clicks, double clicks and long-time pressing.
7
7
This enables you to reuse the same button for multiple functions and lowers the hardware investments.
8
8
9
9
This is also a sample for implementing simple finite-state machines by using the simple pattern above.
10
10
11
- You can find more detail on this library at
11
+ You can find more details on this library at
12
12
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
13
13
14
+
14
15
## Getting Started
15
16
16
17
Clone this repository into ` Arduino/Libraries ` or use the built-in Arduino IDE Library manager to install
17
18
a copy of this library. You can find more detail about installing libraries
18
19
[ here, on Arduino's website] ( https://www.arduino.cc/en/guide/libraries ) .
19
20
20
- ``` c++
21
+ ``` CPP
21
22
#include < Arduino.h>
22
23
#include < OneButton.h>
23
24
```
24
25
25
- ### Initialize a Button
26
-
27
26
Each physical button requires its own ` OneButton ` instance. You can initialize them like this:
28
27
29
- ``` c++
28
+
29
+ ### Initialize a Button to GND
30
+
31
+ ``` CPP
32
+ #define BUTTON_PIN 4
33
+
34
+ /* *
35
+ * Initialize a new OneButton instance for a button
36
+ * connected to digital pin 4 and GND, which is active low
37
+ * and uses the internal pull-up resistor.
38
+ */
39
+
40
+ OneButton btn = OneButton(
41
+ BUTTON_PIN, // Input pin for the button
42
+ LOW, // Button is active LOW
43
+ true // Enable internal pull-up resistor
44
+ );
45
+ ```
46
+
47
+
48
+ ### Initialize a Button to VCC
49
+
50
+ ``` CPP
30
51
#define BUTTON_PIN 4
31
52
32
53
/* *
33
54
* Initialize a new OneButton instance for a button
34
- * connected to digital pin 4, which is active high,
35
- * and does not use the internal pull-up resistors.
55
+ * connected to digital pin 4, which is active high.
56
+ * As this does not use any internal resistor
57
+ * an external resistor (4.7k) may be required to create a LOW signal when the button is not pressed.
36
58
*/
37
59
38
60
OneButton btn = OneButton(
@@ -42,12 +64,13 @@ OneButton btn = OneButton(
42
64
);
43
65
```
44
66
67
+
45
68
### Attach State Events
46
69
47
70
Once you have your button initialized, you can handle events by attaching them to the button
48
71
instance. Events can either be static functions or lambdas (without captured variables).
49
72
50
- ``` c++
73
+ ``` CPP
51
74
// Handler function for a single click:
52
75
static void handleClick () {
53
76
Serial.println("Clicked!");
@@ -62,31 +85,34 @@ btn.attachDoubleClick([]() {
62
85
});
63
86
```
64
87
88
+
65
89
### Don't forget to ` tick() ` !
66
90
67
91
In order for ` OneButton ` to work correctly, you must call ` tick() ` on __ each button instance__
68
92
within your main ` loop() ` . If you're not getting any button events, this is probably why.
69
93
70
- ``` c++
94
+ ``` CPP
71
95
void loop () {
72
96
btn.tick();
73
97
74
98
// Do other things...
75
99
}
76
100
```
77
101
102
+
78
103
## State Events
79
104
80
105
Here's a full list of events handled by this library:
81
106
82
- | Attach Function| Description|
83
- | ---| ---|
84
- | ` attachClick ` | Fires as soon as a single click is detected.|
85
- | ` attachDoubleClick ` | Fires as soon as a double click is detected.|
86
- | ` attachPressStart ` | Fires as soon as the button is pressed down.|
87
- | ` attachLongPressStart ` | Fires as soon as the button is held down for 1 second.|
88
- | ` attachDuringLongPress ` | Fires periodically as long as the button is held down.|
89
- | ` attachLongPressStop ` | Fires when the button is released after a long hold.|
107
+ | Attach Function | Description |
108
+ | ----------------------- | ------------------------------------------------------ |
109
+ | ` attachClick ` | Fires as soon as a single click is detected. |
110
+ | ` attachDoubleClick ` | Fires as soon as a double click is detected. |
111
+ | ` attachPressStart ` | Fires as soon as the button is pressed down. |
112
+ | ` attachLongPressStart ` | Fires as soon as the button is held down for 1 second. |
113
+ | ` attachDuringLongPress ` | Fires periodically as long as the button is held down. |
114
+ | ` attachLongPressStop ` | Fires when the button is released after a long hold. |
115
+
90
116
91
117
### Event Timing
92
118
@@ -97,30 +123,34 @@ the following functions to change the timing.
97
123
click event is not attached, the library will assume a valid single click after one click duration,
98
124
otherwise it must wait for the double click timeout to pass.
99
125
100
- | Function| Default (ms)| Description|
101
- | ---| ---| ---|
102
- | ` setDebounceTicks(int) ` | ` 50 ` | Period of time in which to ignore additional level changes.|
103
- | ` setClickTicks(int) ` | ` 600 ` | Timeout used to distinguish single clicks from double clicks.|
104
- | ` setPressTicks(int) ` | ` 1000 ` | Duration to hold a button to trigger a long press.|
126
+ | Function | Default (ms) | Description |
127
+ | ----------------------- | ------------ | ------------------------------------------------------------- |
128
+ | ` setDebounceTicks(int) ` | ` 50 ` | Period of time in which to ignore additional level changes. |
129
+ | ` setClickTicks(int) ` | ` 600 ` | Timeout used to distinguish single clicks from double clicks. |
130
+ | ` setPressTicks(int) ` | ` 1000 ` | Duration to hold a button to trigger a long press. |
131
+
105
132
106
133
### Additional Functions
107
134
108
135
` OneButton ` also provides a couple additional functions to use for querying button status:
109
136
110
- | Function| Description|
111
- | ---| ---|
112
- | ` bool isLongPressed() ` | Detect whether or not the button is currently inside a long press.|
113
- | ` int getPressedTicks() ` | Get the current number of milliseconds that the button has been held down for.|
137
+ | Function | Description |
138
+ | ----------------------- | ------------------------------------------------------------------------------ |
139
+ | ` bool isLongPressed() ` | Detect whether or not the button is currently inside a long press. |
140
+ | ` int getPressedTicks() ` | Get the current number of milliseconds that the button has been held down for. |
141
+
114
142
115
143
### ` tick() ` and ` reset() `
116
144
117
145
You can specify a logic level when calling ` tick(bool) ` , which will skip reading the pin and use
118
146
that level instead. If you wish to reset the internal state of your buttons, call ` reset() ` .
119
147
148
+
120
149
## Troubleshooting
121
150
122
151
If your buttons aren't acting they way they should, check these items:
123
152
124
153
1 . Check your wiring and pin numbers.
125
- 1 . Did you call ` tick() ` on each button instance in your loop?
126
- 1 . Did you alter your clock timers in any way without adjusting ticks?
154
+ 2 . Did you call ` tick() ` on each button instance in your loop?
155
+ 3 . Did you alter your clock timers in any way without adjusting ticks?
156
+
0 commit comments