Skip to content

Commit 268004a

Browse files
committed
README and comments updated.
1 parent abee83b commit 268004a

File tree

7 files changed

+123
-64
lines changed

7 files changed

+123
-64
lines changed

README.md

+64-24
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,69 @@
22
https://github.com/JChristensen/JC_Button
33
README file
44

5-
![CC BY-SA](http://mirrors.creativecommons.org/presskit/buttons/80x15/png/by-sa.png)
5+
## License
6+
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>
613

714
## Introduction
815
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.
916

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+
1019
## Examples
1120
The following example sketches are included with the **Button** library:
1221

1322
- **SimpleOnOff**: Just turns the Arduino's pin 13 LED on and off.
1423
- **LongPress**: Demonstrates detecting long and short button presses.
1524
- **UpDown**: Counts up or down, one number at a time or rapidly by holding the button down.
1625

17-
## Button library functions
26+
## Constructor
1827

19-
### Button(pin, puEnable, invert, dbTime)
28+
### Button(pin, dbTime, puEnable, invert)
2029
##### Description
2130
The constructor defines a button object.
2231
##### 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)*
2939
##### Returns
3040
None.
3141
##### Example
3242
```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+
3452
```
53+
##Library Functions
3554
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+
```
3668
### read()
3769
##### Description
3870
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
4173
##### Parameters
4274
None.
4375
##### Returns
44-
*true* if the button is pressed, *else* false *(boolean)*
76+
*true* if the button is pressed, *else* false *(bool)*
4577
##### Example
4678
```c++
4779
myButton.read();
@@ -50,28 +82,30 @@ myButton.read();
5082
### isPressed()
5183
### isReleased()
5284
##### 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.
5486
##### Syntax
5587
`myButton.isPressed();`
5688
`myButton.isReleased();`
5789
##### Parameters
5890
None.
5991
##### 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)*
6193
##### Example
6294
```c++
63-
if ( myButton.isPressed() ) {
64-
//do some stuff
95+
if ( myButton.isPressed() )
96+
{
97+
//do something
6598
}
66-
else {
67-
//do some different stuff
99+
else
100+
{
101+
//do something else
68102
}
69103
```
70104

71105
### wasPressed()
72106
### wasReleased()
73107
##### 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.
75109
##### Syntax
76110
`myButton.wasPressed();`
77111
`myButton.wasReleased();`
@@ -81,28 +115,34 @@ None.
81115
*true* or *false*, depending on whether the button was pressed (released) or not *(boolean)*
82116
##### Example
83117
```c++
84-
if ( myButton.wasPressed() ) { ...
118+
if ( myButton.wasPressed() )
119+
{
120+
//do something
121+
}
85122
```
86123

87124
### pressedFor(ms)
88125
### releasedFor(ms)
89126
##### 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.
91128
##### Syntax
92129
`myButton.pressedFor(ms);`
93130
`myButton.releasedFor(ms);`
94131
##### Parameters
95132
**ms:** The number of milliseconds *(unsigned long)*
96133
##### 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)*
98135
##### Example
99136
```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+
}
101141
```
102142

103143
### lastChange()
104144
##### 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).
106146
##### Syntax
107147
`myButton.lastChange();`
108148
##### Parameters
@@ -111,5 +151,5 @@ None.
111151
The time in milliseconds when the button last changed state *(unsigned long)*
112152
##### Example
113153
```c++
114-
unsigned long lastChange = myButton.lastChange();
154+
unsigned long msLastChange = myButton.lastChange();
115155
```

examples/LongPress/LongPress.ino

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void setup()
3232
// sequence of states, i.e. ONOFF --> TO_BLINK --> BLINK --> TO_ONOFF --> ONOFF
3333
// note that while the user perceives two "modes", i.e. ON/OFF mode and rapid blink mode,
3434
// two extra states are needed in the state machine to transition between these modes.
35-
enum states_t {ONOFF, TO_BLINK, BLINK, TO_ONOFF};
35+
enum states_t {ONOFF, TO_BLINK, BLINK, TO_ONOFF};
3636

3737
bool ledState; // current LED status
3838
unsigned long ms; // current time from millis()
@@ -43,18 +43,18 @@ void loop()
4343
static states_t STATE; // current state machine state
4444
ms = millis(); // record the current time
4545
myBtn.read(); // read the button
46-
46+
4747
switch (STATE)
4848
{
4949
// this state watches for short and long presses, switches the LED for
5050
// short presses, and moves to the TO_BLINK state for long presses.
51-
case ONOFF:
51+
case ONOFF:
5252
if (myBtn.wasReleased())
5353
switchLED();
5454
else if (myBtn.pressedFor(LONG_PRESS))
5555
STATE = TO_BLINK;
5656
break;
57-
57+
5858
// this is a transition state where we start the fast blink as feedback to the user,
5959
// but we also need to wait for the user to release the button, i.e. end the
6060
// long press, before moving to the BLINK state.
@@ -64,7 +64,7 @@ void loop()
6464
else
6565
fastBlink();
6666
break;
67-
67+
6868
// the fast-blink state. Watch for another long press which will cause us to
6969
// turn the LED off (as feedback to the user) and move to the TO_ONOFF state.
7070
case BLINK:
@@ -77,7 +77,7 @@ void loop()
7777
else
7878
fastBlink();
7979
break;
80-
80+
8181
// this is a transition state where we just wait for the user to release the button
8282
// before moving back to the ONOFF state.
8383
case TO_ONOFF:
@@ -87,15 +87,15 @@ void loop()
8787
}
8888
}
8989

90-
// reverse the current LED state. if it's on, turn it off. If it's off, turn it on.
90+
// reverse the current LED state. if it's on, turn it off. if it's off, turn it on.
9191
void switchLED()
9292
{
9393
msLast = ms; // record the last switch time
9494
ledState = !ledState;
9595
digitalWrite(LED_PIN, ledState);
9696
}
9797

98-
// switch the LED on and off every BLINK_INETERVAL milliseconds.
98+
// switch the LED on and off every BLINK_INTERVAL milliseconds.
9999
void fastBlink()
100100
{
101101
if (ms - msLast >= BLINK_INTERVAL)

examples/UpDown/UpDown.ino

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) 2018 by Jack Christensen and licensed under
44
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
55
//
6-
// Example skletch that uses both short and long button presses to adjust
6+
// Example sketch that uses both short and long button presses to adjust
77
// a number up and down, between two limits. Short presses increment
88
// or decrement by one, long presses repeat at a specified rate.
99
// Every time the number changes, it is written to the serial monitor.
@@ -43,13 +43,13 @@ void loop()
4343

4444
btnUP.read(); // read the buttons
4545
btnDN.read();
46-
46+
4747
if (count != lastCount) // print the count if it has changed
4848
{
4949
lastCount = count;
5050
Serial.println(count, DEC);
5151
}
52-
52+
5353
switch (STATE)
5454
{
5555
case WAIT: // wait for a button event

keywords.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Button KEYWORD1
2+
begin KEYWORD2
23
read KEYWORD2
34
isPressed KEYWORD2
45
isReleased KEYWORD2

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=JC_Button
2-
version=1.0.2
2+
version=2.0.0
33
author=Jack Christensen <[email protected]>
44
maintainer=Jack Christensen <[email protected]>
55
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.

src/JC_Button.cpp

+8-26
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,12 @@
22
// https://github.com/JChristensen/JC_Button
33
// Copyright (C) 2018 by Jack Christensen and licensed under
44
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
5-
//
6-
// Library for reading momentary contact switches like tactile button
7-
// switches. Intended for use in state machine constructs.
8-
// Use the read() function to read each button in the main loop,
9-
// which should execute as fast as possible.
105

116
#include "JC_Button.h"
127

138
/*----------------------------------------------------------------------*
14-
* Button(pin, puEnable, invert, dbTime) instantiates a button object. *
15-
* pin Is the Arduino pin the button is connected to. *
16-
* puEnable Enables the AVR internal pullup resistor if != 0 (can also *
17-
* use true or false). *
18-
* invert If invert == 0, interprets a high state as pressed, low as *
19-
* released. If invert != 0, interprets a high state as *
20-
* released, low as pressed (can also use true or false). *
21-
* dbTime Is the debounce time in milliseconds. *
22-
* *
23-
* (Note that invert cannot be implied from puEnable since an external *
24-
* pullup could be used.) *
25-
*----------------------------------------------------------------------*/
26-
9+
/ initialize a Button object and the pin it's connected to. *
10+
/-----------------------------------------------------------------------*/
2711
void Button::begin()
2812
{
2913
pinMode(m_pin, m_puEnable ? INPUT_PULLUP : INPUT);
@@ -36,29 +20,27 @@ void Button::begin()
3620
}
3721

3822
/*----------------------------------------------------------------------*
39-
/ returns the state of the button, true==pressed, false==released, *
40-
/ does debouncing, captures and maintains times, previous states, etc. *
23+
/ returns the state of the button, true if pressed, false if released. *
24+
/ does debouncing, captures and maintains times, previous state, etc. *
4125
/-----------------------------------------------------------------------*/
4226
bool Button::read()
4327
{
4428
uint32_t ms = millis();
45-
uint8_t pinVal = digitalRead(m_pin);
29+
bool pinVal = digitalRead(m_pin);
4630
if (m_invert) pinVal = !pinVal;
4731
if (ms - m_lastChange < m_dbTime)
4832
{
49-
m_time = ms;
5033
m_changed = false;
51-
return m_state;
5234
}
5335
else
5436
{
5537
m_lastState = m_state;
5638
m_state = pinVal;
57-
m_time = ms;
5839
m_changed = (m_state != m_lastState);
5940
if (m_changed) m_lastChange = ms;
60-
return m_state;
6141
}
42+
m_time = ms;
43+
return m_state;
6244
}
6345

6446
/*----------------------------------------------------------------------*
@@ -73,7 +55,7 @@ bool Button::isPressed()
7355

7456
bool Button::isReleased()
7557
{
76-
return m_state;
58+
return !m_state;
7759
}
7860

7961
/*----------------------------------------------------------------------*

0 commit comments

Comments
 (0)