Skip to content

Commit f3ef0d2

Browse files
committed
Updates to MomentaryButton and LatchingButton.
Added functionality to MomentaryButton and LatchingButton to make them easier to use. Added additional examples.
1 parent ef582b8 commit f3ef0d2

13 files changed

Lines changed: 193 additions & 43 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ButtonSuite
22

3-
A class for adding functionality to push (momentary) buttons. This library allows a simple momentary push button to be used as a momentary button, latching button, counter, or enumerator. This library uses the Bounce2 library to add debouncing functionality.
3+
A library for adding functionality to push (momentary) buttons. This library allows a simple momentary push button to be used as a momentary button, latching button, counter, or enumerator. This library uses the Bounce2 library to add debouncing functionality.
44

55
## Types of Buttons
66
This library contains two categories of button types. The first category is two state buttons; these are either on or off. The second category provides incrementing buttons that can perform different types of counting. These two categories of buttons are described here. See "Software Design" below for information about how the source code implements these behaviors.

examples/LatchingButton_Demo_01/LatchingButton_Demo_01.ino

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
should be used.
1111
*/
1212

13+
/*
14+
Please note the difference between the examples MomentaryButton_Demo_02 and
15+
LatchingButton_Demo_01. Both examples toggle an LED on and off but one uses
16+
a MomentaryButton and one uses the LatchingButton. While the behavior is the
17+
same for these simple examples, there is an important difference. In the case
18+
of the MomentaryButton, the state of the LED (on or off) is not known. For the
19+
LatchingButton, it is known what state the button (and therefore LED) is in.
20+
*/
21+
1322
#include "LatchingButton.h"
1423

1524
// Change these if your button or LED are on other pins.
@@ -29,7 +38,7 @@ void setup()
2938
void loop()
3039
{
3140
// Press the button once to turn it on, and again to turn it off.
32-
if (button.getStatus() == ISPRESSED)
41+
if (button.isLatched())
3342
{
3443
digitalWrite(ledPin, HIGH);
3544
}

examples/LatchingButton_Demo_02/LatchingButton_Demo_02.ino

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ LatchingButton button(buttonPin);
2424

2525
void setup()
2626
{
27-
Serial.begin(9600);
28-
Serial.println("*** Latching Button Demo 02 ***");
29-
3027
// Setup the output LED.
3128
pinMode(ledPin, OUTPUT);
3229
digitalWrite(ledPin, LOW);
@@ -35,12 +32,7 @@ void setup()
3532
void loop()
3633
{
3734
// Press the button once to turn it on, and again to turn it off.
38-
bool buttonStatus = button.getStatus();
39-
40-
Serial.print("Button status: ");
41-
Serial.println(buttonStatus);
42-
43-
if (buttonStatus == ISPRESSED)
35+
if (button.getStatus() == ISPRESSED)
4436
{
4537
digitalWrite(ledPin, HIGH);
4638
}

examples/MomentaryButton_Demo_01/MomentaryButton_Demo_01.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ void setup()
2727

2828
void loop()
2929
{
30-
// Press the button once to turn it on, and again to turn it off.
31-
if (button.getStatus() == ISPRESSED)
30+
// Press the button to turn the LED on and release it to turn it off.
31+
if (button.isPressed())
3232
{
3333
digitalWrite(ledPin, HIGH);
3434
}
3535
else
3636
{
3737
digitalWrite(ledPin, LOW);
3838
}
39-
}
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Toggles an LED between on and off with each press of the button.
3+
4+
The button should be wired such that when pressed, the "buttonPin" is
5+
connected to ground.
6+
7+
The LED should be wired with the "ledPin" to the positive lead and the
8+
negative lead should be connected to ground. A current limiting resistor
9+
should be used.
10+
*/
11+
12+
/*
13+
Please note the difference between the examples MomentaryButton_Demo_02 and
14+
LatchingButton_Demo_01. Both examples toggle an LED on and off but one uses
15+
a MomentaryButton and one uses the LatchingButton. While the behavior is the
16+
same for these simple examples, there is an important difference. In the case
17+
of the MomentaryButton, the state of the LED (on or off) is not known. For the
18+
LatchingButton, it is known what state the button (and therefore LED) is in.
19+
*/
20+
21+
#include "MomentaryButton.h"
22+
23+
// Change these if your button or LED are on other pins.
24+
int buttonPin = 8;
25+
int ledPin = 9;
26+
27+
// The MomentaryButton will automatically configure the button pin.
28+
MomentaryButton button(buttonPin);
29+
30+
void setup()
31+
{
32+
// Setup the output LED.
33+
pinMode(ledPin, OUTPUT);
34+
digitalWrite(ledPin, LOW);
35+
}
36+
37+
void loop()
38+
{
39+
// Press the button once to turn the LED on and again it to turn it off. This only captures button release
40+
// events, i.e., nothing happens when the button is pressed down, only after the release.
41+
if (button.wasPressed())
42+
{
43+
// The button was pressed, so toggle the LED state. To toggle it, the current value is read (digitalRead)
44+
// and negated. The result is written back to the pin to change its state.
45+
digitalWrite(ledPin, !digitalRead(ledPin));
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Toggles an LED between on and off with each press of the button.
3+
4+
The button should be wired such that when pressed, the "buttonPin" is
5+
connected to ground.
6+
7+
The LED should be wired with the "ledPin" to the positive lead and the
8+
negative lead should be connected to ground. A current limiting resistor
9+
should be used.
10+
*/
11+
12+
#include "MomentaryButton.h"
13+
14+
// Change these if your button or LED are on other pins.
15+
int buttonPin = 8;
16+
int ledPin = 9;
17+
18+
// The MomentaryButton will automatically configure the button pin.
19+
MomentaryButton button(buttonPin);
20+
21+
void setup()
22+
{
23+
// Setup the output LED.
24+
pinMode(ledPin, OUTPUT);
25+
digitalWrite(ledPin, LOW);
26+
}
27+
28+
void loop()
29+
{
30+
// Press the button to turn the LED on and release it to turn it off.
31+
if (button.getStatus() == ISPRESSED)
32+
{
33+
digitalWrite(ledPin, HIGH);
34+
}
35+
else
36+
{
37+
digitalWrite(ledPin, LOW);
38+
}
39+
}

examples/SwitchRunTimeBahavior_Demo_01/SwitchRunTimeBahavior_Demo_01.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MomentaryButton momentaryButton(buttonPin);
3232
AlwaysOnButton onButton(buttonPin);
3333

3434
// Used for timing and switching between the two states.
35-
unsigned long intervalStartTime;
35+
unsigned long intervalStartTime;
3636
const int intervalLength = 5000;
3737
int currentState;
3838

src/ButtonBase.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,20 @@ void ButtonBase::setDebounceInterval(int debounceInterval)
5252

5353
BUTTONSTATUS ButtonBase::update()
5454
{
55+
// See ButtonEnums.h for the definitions of the different button states.
56+
5557
// Have the debouncer update. This does most of the work of handling the button state.
5658
// The debouncer determines the current state, if the state changed since the last time
5759
// "update" was called, what the time between state changes was, and so on.
5860
_debouncer.update();
5961

60-
// Catch transitions from HIGH to LOW. This is the button press.
62+
// Catch transitions from HIGH to LOW. This happens right when the button is pressed down.
6163
if (_debouncer.fell())
6264
{
6365
#ifdef BUTTONSUITEDEBUG
64-
Serial.println("ButtonBase::update: WASPRESSED");
66+
Serial.println("ButtonBase::update: JUSTPRESSED");
6567
#endif
66-
return WASPRESSED;
68+
return JUSTPRESSED;
6769
}
6870

6971
// Look to see if the button is currently pressed.

src/ButtonEnums.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
enum BUTTONSTATUS
3333
{
3434
// Was just pressed down (first call to "update" after the button was pressed).
35-
WASPRESSED,
35+
JUSTPRESSED,
3636

3737
// Is still pressed down.
3838
ISPRESSED,

src/LatchingButton.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,23 @@ void LatchingButton::setLongPressInterval(int longPressInterval)
5353
_longPressInterval = longPressInterval;
5454
}
5555

56+
bool LatchingButton::isLatched()
57+
{
58+
getStatus();
59+
return _latched;
60+
}
61+
5662
BUTTONSTATUS LatchingButton::getStatus()
5763
{
58-
// Catch transitions from HIGH to LOW.
59-
switch (update())
64+
// Get the status.
65+
BUTTONSTATUS buttonStatus = update();
66+
67+
switch (buttonStatus)
6068
{
61-
case WASPRESSED:
69+
case JUSTPRESSED:
6270
{
6371
// Capture the event of the button press. Some classes may require this to act on it.
64-
return WASPRESSED;
72+
return JUSTPRESSED;
6573
}
6674

6775
case WASSHORTPRESSED:
@@ -88,7 +96,9 @@ BUTTONSTATUS LatchingButton::getStatus()
8896
case NOTPRESSED:
8997
default:
9098
{
91-
// Nothing happened so we return based on the state.
99+
// Nothing changed with the button, so we return a value based on the latched state.
100+
// This means that it does not matter if the button is currently pressed or not pressed,
101+
// it returns pressed when latched and not pressed when not latched.
92102
return convertStateToButtonStatus();
93103
}
94104
}

0 commit comments

Comments
 (0)