Skip to content

Commit b87dcd9

Browse files
committed
Add ToggleButton derived class.
1 parent db8bf3a commit b87dcd9

5 files changed

Lines changed: 171 additions & 6 deletions

File tree

README.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ https://github.com/JChristensen/JC_Button
33
README file
44

55
## License
6-
Arduino Button Library Copyright (C) 2018 Jack Christensen GNU GPL v3.0
6+
Arduino Button Library Copyright (C) 2018-2019 Jack Christensen GNU GPL v3.0
77

88
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.
99

@@ -16,14 +16,18 @@ The Button library is for debouncing and reading momentary contact switches like
1616

1717
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.
1818

19+
A derived class, ToggleButton, implements button objects that need only "push-on, push-off" functionality.
20+
1921
## Examples
2022
The following example sketches are included with the **Button** library:
2123

2224
- **SimpleOnOff**: Just turns the Arduino's pin 13 LED on and off.
2325
- **LongPress**: Demonstrates detecting long and short button presses.
2426
- **UpDown**: Counts up or down, one number at a time or rapidly by holding the button down.
27+
- **Toggle**: Demonstrates ToggleButton functionality.
28+
2529

26-
## Constructor
30+
## Constructors
2731

2832
### Button(pin, dbTime, puEnable, invert)
2933
##### Description
@@ -50,7 +54,38 @@ Button myButton(3, 50);
5054
Button myButton(4, 25, false, false);
5155

5256
```
53-
## Library Functions
57+
58+
### ToggleButton(pin, initialState, dbTime, puEnable, invert)
59+
##### Description
60+
The constructor defines a toggle button object, which has "push-on, push-off" functionality. The initial state can be on or off. See the section, [ToggleButton Library Functions](https://github.com/JChristensen/JC_Button#togglebutton-library-functions) for functions that apply specifically to the ToggleButton object. The ToggleButton class is derived from the Button class, so all Button functions are available, but because it is inherently a more limited concept, the special ToggleButton functions will be most useful, along with `begin()` and `read()`.
61+
##### Syntax
62+
`ToggleButton(pin, initialState, dbTime, puEnable, invert);`
63+
##### Required parameter
64+
**pin:** Arduino pin number that the button is connected to *(byte)*
65+
##### Optional parameters
66+
**initialState:** Initial state for the button. Defaults to off (false) if not given. *(bool)*
67+
**dbTime:** Debounce time in milliseconds. Defaults to 25ms if not given. *(unsigned long)*
68+
**puEnable:** *true* to enable the microcontroller's internal pull-up resistor, else *false*. Defaults to *true* if not given. *(bool)*
69+
**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)*
70+
##### Returns
71+
None.
72+
##### Example
73+
```c++
74+
// button connected from pin 2 to ground, initial state off,
75+
// 25ms debounce, pullup enabled, logic inverted
76+
ToggleButton myToggle(2);
77+
78+
// same as above but this button is initially "on" and also
79+
// needs a longer debounce time (50ms).
80+
ToggleButton myToggle(3, true, 50);
81+
82+
// a button wired from the MCU pin to Vcc with an external pull-down resistor,
83+
// initial state is off.
84+
Button myButton(4, false, 25, false, false);
85+
86+
```
87+
88+
## Button Library Functions
5489

5590
### begin()
5691
##### Description
@@ -73,7 +108,7 @@ Reads the button and returns a *boolean* value (*true* or *false*) to indicate w
73108
##### Parameters
74109
None.
75110
##### Returns
76-
*true* if the button is pressed, *else* false *(bool)*
111+
*true* if the button is pressed, else *false* *(bool)*
77112
##### Example
78113
```c++
79114
myButton.read();
@@ -153,3 +188,47 @@ The time in milliseconds when the button last changed state *(unsigned long)*
153188
```c++
154189
unsigned long msLastChange = myButton.lastChange();
155190
```
191+
192+
## ToggleButton Library Functions
193+
194+
### changed()
195+
##### Description
196+
Returns a boolean value (true or false) to indicate whether the toggle button changed state the last time `read()` was called.
197+
##### Syntax
198+
`myToggle.changed();`
199+
##### Parameters
200+
None.
201+
##### Returns
202+
*true* if the toggle state changed, else *false* *(bool)*
203+
##### Example
204+
```c++
205+
if (myToggle.changed())
206+
{
207+
// do something
208+
}
209+
else
210+
{
211+
// do something different
212+
}
213+
```
214+
215+
### toggleState()
216+
##### Description
217+
Returns a boolean value (true or false) to indicate the toggle button state as of the last time `read()` was called.
218+
##### Syntax
219+
`myToggle.toggleState();`
220+
##### Parameters
221+
None.
222+
##### Returns
223+
*true* if the toggle is "on", else *false* *(bool)*
224+
##### Example
225+
```c++
226+
if (myToggle.toggleState())
227+
{
228+
// do something
229+
}
230+
else
231+
{
232+
// do something different
233+
}
234+
```

examples/Toggle/Toggle.ino

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Arduino Button Library
2+
// https://github.com/JChristensen/JC_Button
3+
// Copyright (C) 2018 by Jack Christensen and licensed under
4+
// GNU GPL v3.0, https://www.gnu.org/licenses/gpl.html
5+
//
6+
// Example sketch to demonstrate toggle buttons.
7+
8+
#include <JC_Button.h> // https://github.com/JChristensen/JC_Button
9+
10+
// pin assignments
11+
const byte
12+
LED1_PIN(5), // connect an LED to ground, through an appropriate current limiting resistor
13+
LED2_PIN(6), // connect an LED to ground, through an appropriate current limiting resistor
14+
BUTTON1_PIN(7), // connect a button switch from this pin to ground
15+
BUTTON2_PIN(8); // connect a button switch from this pin to ground
16+
17+
ToggleButton // define the buttons
18+
btn1(BUTTON1_PIN), // this button's initial state is off
19+
btn2(BUTTON2_PIN, true); // this button's initial state is on
20+
21+
void setup()
22+
{
23+
// initialize the button objects
24+
btn1.begin();
25+
btn2.begin();
26+
27+
// set the LED pins as outputs
28+
pinMode(LED1_PIN, OUTPUT);
29+
pinMode(LED2_PIN, OUTPUT);
30+
31+
// show the initial states
32+
digitalWrite(LED1_PIN, btn1.toggleState());
33+
digitalWrite(LED2_PIN, btn2.toggleState());
34+
}
35+
36+
void loop()
37+
{
38+
// read the buttons
39+
btn1.read();
40+
btn2.read();
41+
42+
// if button state changed, update the LEDs
43+
if (btn1.changed()) digitalWrite(LED1_PIN, btn1.toggleState());
44+
if (btn2.changed()) digitalWrite(LED2_PIN, btn2.toggleState());
45+
}

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Button KEYWORD1
2+
ToggleButton KEYWORD1
23
begin KEYWORD2
34
read KEYWORD2
45
isPressed KEYWORD2
@@ -8,3 +9,5 @@ wasReleased KEYWORD2
89
pressedFor KEYWORD2
910
releasedFor KEYWORD2
1011
lastChange KEYWORD2
12+
changed KEYWORD2
13+
toggleState KEYWORD2

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=JC_Button
2-
version=2.0.1
2+
version=2.1.0
33
author=Jack Christensen <jack.christensen@outlook.com>
44
maintainer=Jack Christensen <jack.christensen@outlook.com>
55
sentence=Arduino library to debounce button switches, detect presses, releases, and long presses.
6-
paragraph=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.
6+
paragraph=Copyright (C) 2018-2019 by Jack Christensen and licensed under GNU GPL v3.0.
77
category=Signal Input/Output
88
url=https://github.com/JChristensen/JC_Button
99
architectures=avr

src/JC_Button.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,42 @@ class Button
7070
uint32_t m_time; // time of current state (ms from millis)
7171
uint32_t m_lastChange; // time of last state change (ms)
7272
};
73+
74+
// a derived class for a "push-on, push-off" (toggle) type button.
75+
// initial state can be given, default is off (false).
76+
class ToggleButton : public Button
77+
{
78+
public:
79+
80+
// constructor is similar to Button, but includes the initial state for the toggle.
81+
ToggleButton(uint8_t pin, bool initialState=false, uint32_t dbTime=25, uint8_t puEnable=true, uint8_t invert=true)
82+
: Button(pin, dbTime, puEnable, invert), m_toggleState(initialState) {}
83+
84+
// read the button and return its state.
85+
// should be called frequently.
86+
bool read()
87+
{
88+
Button::read();
89+
if (wasPressed())
90+
{
91+
m_toggleState = !m_toggleState;
92+
m_changed = true;
93+
}
94+
else
95+
{
96+
m_changed = false;
97+
}
98+
return m_toggleState;
99+
}
100+
101+
// has the state changed?
102+
bool changed() {return m_changed;}
103+
104+
// return the current state
105+
bool toggleState() {return m_toggleState;}
106+
107+
private:
108+
bool m_toggleState;
109+
bool m_changed;
110+
};
73111
#endif

0 commit comments

Comments
 (0)