You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Arduino Button Library Copyright (C) 2018 Jack Christensen GNU GPL v3.0
7
+
8
+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 as published by the Free Software Foundation.
9
+
10
+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11
+
12
+
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/gpl.html>
6
13
7
14
## Introduction
8
15
The Button library is for debouncing and reading momentary contact switches like tactile button switches. "Long presses" of arbitrary length can be detected. Works well in state machine constructs. Use the read() function to read each button in the main loop, which should execute as fast as possible.
9
16
17
+
The simplest way to use a button with an AVR microcontroller is to wire the button between a GPIO pin and ground, and turn on the AVR internal pullup resistor. The Button class constructor takes four arguments, but three have default values that work for a button wired in this manner.
18
+
10
19
## Examples
11
20
The following example sketches are included with the **Button** library:
12
21
13
22
-**SimpleOnOff**: Just turns the Arduino's pin 13 LED on and off.
14
23
-**LongPress**: Demonstrates detecting long and short button presses.
15
24
-**UpDown**: Counts up or down, one number at a time or rapidly by holding the button down.
16
25
17
-
## Button library functions
26
+
## Constructor
18
27
19
-
### Button(pin, puEnable, invert, dbTime)
28
+
### Button(pin, dbTime, puEnable, invert)
20
29
##### Description
21
30
The constructor defines a button object.
22
31
##### Syntax
23
-
`Button(pin, puEnable, invert, dbTime);`
24
-
##### Parameters
25
-
**pin:** Arduino pin number that the button is connected to *(byte)*
26
-
**puEnable:***true* to enable the microcontroller's internal pull-up resistor, else *false**(boolean)*
27
-
invert: *false* interprets a high logic level to mean the button is pressed, *true* interprets a low level as pressed. *true* should be used when a pull-up resistor is employed, *false* for a pull-down resistor. *(boolean)*
28
-
**dbTime:** Debounce time in milliseconds *(unsigned long)*
32
+
`Button(pin, dbTime, puEnable, invert);`
33
+
##### Required parameter
34
+
**pin:** Arduino pin number that the button is connected to *(byte)*
35
+
##### Optional parameters
36
+
**dbTime:** Debounce time in milliseconds. Defaults to 25ms if not given. *(unsigned long)*
37
+
**puEnable:***true* to enable the microcontroller's internal pull-up resistor, else *false*. Defaults to *true* if not given. *(bool)*
38
+
**invert:***false* interprets a high logic level to mean the button is pressed, *true* interprets a low level as pressed. *true* should be used when a pull-up resistor is employed, *false* for a pull-down resistor. Defaults to *true* if not given. *(bool)*
29
39
##### Returns
30
40
None.
31
41
##### Example
32
42
```c++
33
-
Button myButton = Button(2, true, true, 25); //25 ms debounce
43
+
// button connected from pin 2 to ground, 25ms debounce, pullup enabled, logic inverted
44
+
Button myButton(2);
45
+
46
+
// same as above but this button needs a longer debounce time (50ms)
47
+
Button myButton(3, 50);
48
+
49
+
// a button wired from the MCU pin to Vcc with an external pull-down resistor
50
+
Button myButton(4, 25, false, false);
51
+
34
52
```
53
+
##Library Functions
35
54
55
+
### begin()
56
+
##### Description
57
+
Initializes the Button object and the pin it is connected to.
58
+
##### Syntax
59
+
`myButton.begin();`
60
+
##### Parameters
61
+
None.
62
+
##### Returns
63
+
None.
64
+
##### Example
65
+
```c++
66
+
myButton.begin();
67
+
```
36
68
### read()
37
69
##### Description
38
70
Reads the button and returns a *boolean* value (*true* or *false*) to indicate whether the button is pressed. The read() function needs to execute very frequently in order for the sketch to be responsive. A good place for read() is at the top of loop(). Often, the return value from read() will not be needed if the other functions below are used.
@@ -41,7 +73,7 @@ Reads the button and returns a *boolean* value (*true* or *false*) to indicate w
41
73
##### Parameters
42
74
None.
43
75
##### Returns
44
-
*true* if the button is pressed, *else* false *(boolean)*
76
+
*true* if the button is pressed, *else* false *(bool)*
45
77
##### Example
46
78
```c++
47
79
myButton.read();
@@ -50,28 +82,30 @@ myButton.read();
50
82
### isPressed()
51
83
### isReleased()
52
84
##### Description
53
-
These methods check the button state at the point in time when it was last read, and return false or true accordingly. These functions **do not** cause the button to be read.
85
+
These functions check the button state from the last call to `read()` and return false or true accordingly. These functions **do not** cause the button to be read.
54
86
##### Syntax
55
87
`myButton.isPressed();`
56
88
`myButton.isReleased();`
57
89
##### Parameters
58
90
None.
59
91
##### Returns
60
-
*true* or *false*, depending on whether the button has been pressed (released) or not *(boolean)*
92
+
*true* or *false*, depending on whether the button has been pressed (released) or not *(bool)*
61
93
##### Example
62
94
```c++
63
-
if ( myButton.isPressed() ) {
64
-
//do some stuff
95
+
if ( myButton.isPressed() )
96
+
{
97
+
//do something
65
98
}
66
-
else {
67
-
//do some different stuff
99
+
else
100
+
{
101
+
//do something else
68
102
}
69
103
```
70
104
71
105
### wasPressed()
72
106
### wasReleased()
73
107
##### Description
74
-
These methods check the button state to see if it changed between the last two reads and return false or true accordingly. These functions **do not** cause the button to be read. Note that these functions may be more useful than `isPressed()` and `isReleased()` since they actually detect a **change** in the state of the button, which is usually what we want in order to cause some action.
108
+
These functions check the button state to see if it changed between the last two calls to `read()` and return false or true accordingly. These functions **do not** cause the button to be read. Note that these functions may be more useful than `isPressed()` and `isReleased()` since they actually detect a **change** in the state of the button, which is usually what we want in order to cause some action.
75
109
##### Syntax
76
110
`myButton.wasPressed();`
77
111
`myButton.wasReleased();`
@@ -81,28 +115,34 @@ None.
81
115
*true* or *false*, depending on whether the button was pressed (released) or not *(boolean)*
82
116
##### Example
83
117
```c++
84
-
if ( myButton.wasPressed() ) { ...
118
+
if ( myButton.wasPressed() )
119
+
{
120
+
//do something
121
+
}
85
122
```
86
123
87
124
### pressedFor(ms)
88
125
### releasedFor(ms)
89
126
##### Description
90
-
These methods check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions **do not** cause the button to be read.
127
+
These functions check to see if the button is pressed (or released), and has been in that state for the specified time in milliseconds. Returns false or true accordingly. These functions are useful to detect "long presses". Note that these functions **do not** cause the button to be read.
91
128
##### Syntax
92
129
`myButton.pressedFor(ms);`
93
130
`myButton.releasedFor(ms);`
94
131
##### Parameters
95
132
**ms:** The number of milliseconds *(unsigned long)*
96
133
##### Returns
97
-
*true* or *false*, depending on whether the button was pressed (released) for the specified time *(boolean)*
134
+
*true* or *false*, depending on whether the button was pressed (released) for the specified time *(bool)*
98
135
##### Example
99
136
```c++
100
-
if ( myButton.pressedFor(1000) ) { //has the button been pressed for one second?
137
+
if ( myButton.pressedFor(1000) )
138
+
{
139
+
// button has been pressed for one second
140
+
}
101
141
```
102
142
103
143
### lastChange()
104
144
##### Description
105
-
Under certain circumstances, it may be useful to know when a button last changed state. lastChange() returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function).
145
+
Under certain circumstances, it may be useful to know when a button last changed state. `lastChange()` returns the time the button last changed state, in milliseconds (the value is derived from the Arduino millis() function).
106
146
##### Syntax
107
147
`myButton.lastChange();`
108
148
##### Parameters
@@ -111,5 +151,5 @@ None.
111
151
The time in milliseconds when the button last changed state *(unsigned long)*
0 commit comments