Skip to content

Commit 38639d5

Browse files
committed
Updated/Added examples
1 parent 15ba2cd commit 38639d5

File tree

4 files changed

+207
-41
lines changed

4 files changed

+207
-41
lines changed

examples/Advanced/Advanced.ino

+17-29
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
indicating when events are triggered.
1414
*/
1515

16-
#include <ButtonEvents.h> // you have to include the library if you want to use it!
16+
#include <ButtonEvents.h> // we have to include the library in order to use it
1717

18-
const byte buttonPin = 7; // the button will be connected to pin 7
18+
const byte buttonPin = 7; // our button will be connected to pin 7
1919

20-
ButtonEvents myButton; // create an instance of a ButtonEvents object to associate with our button
20+
ButtonEvents myButton; // create an instance of the ButtonEvents class to attach to our button
2121

2222

2323
// this is where we run one-time setup code
2424
void setup() {
2525

2626
// configure the button pin as a digital input with internal pull-up resistor enabled
2727
pinMode(buttonPin, INPUT_PULLUP);
28-
28+
2929
// attach our ButtonEvents instance to the button pin
3030
myButton.attach(buttonPin);
3131

@@ -41,47 +41,34 @@ void setup() {
4141
// debounce time if necessary.
4242
myButton.debounceTime(15); //apply 15ms debounce
4343

44-
/* The ButtonEvents library is capable of detecting three types of button events:
45-
46-
1) A 'tap' event is triggered after the button has been released, and the double-tap detection
47-
window has elapsed with no further button presses.
48-
49-
2) A 'double-tap' event is triggered after the button has been released and is then pressed again
50-
before the double-tap detection window has elapsed.
51-
52-
3) A 'press-and-hold' event is triggered after the button has been pressed and held down for the hold
53-
duration.
54-
55-
The double-tap detection window is set to 150ms by default. Decreasing this value will result in
56-
more responsive single-tap events, but requires really fast tapping to trigger a double-tap event.
57-
Increasing this value will allow slower taps to still trigger a double-tap event, but will make
58-
single-tap events more laggy, and can cause taps that were meant to be separate to be treated as
59-
double-taps. The necessary timing really depends on your use case, but I have found 150ms to be a
60-
reasonable middle-ground. If you need to change the double-tap detection window, you can do so as
61-
follows:
62-
*/
44+
// The double-tap detection window is set to 150ms by default. Decreasing this value will result in
45+
// more responsive single-tap events, but requires really fast tapping to trigger a double-tap event.
46+
// Increasing this value will allow slower taps to still trigger a double-tap event, but will make
47+
// single-tap events more laggy, and can cause taps that were meant to be separate to be treated as
48+
// double-taps. The necessary timing really depends on your use case, but I have found 150ms to be a
49+
// reasonable starting point. If you need to change the double-tap detection window, you can do so
50+
// as follows:
6351
myButton.doubleTapTime(250); // set double-tap detection window to 250ms
6452

6553
// The hold duration can be increased to require longer holds before an event is triggered, or reduced to
6654
// have hold events trigger more quickly.
6755
myButton.holdTime(2000); // require button to be held for 2000ms before triggering a hold event
68-
69-
56+
7057
// initialize the arduino serial port and send a welcome message
7158
Serial.begin(9600);
72-
Serial.println("ButtonEvents 'advanced' example started");
59+
Serial.println("ButtonEvents 'Advanced' example started");
7360
}
7461

7562

7663
// this is the main loop, which will repeat forever
7764
void loop() {
7865

79-
// the update() method returns true if an event or state change occurred. It serves as a passthru
66+
// The update() method returns true if an event or state change occurred. It serves as a passthru
8067
// to the Bounce2 library update() function as well, so it will stll return true if a press/release
8168
// is detected but has not triggered a tap/double-tap/hold event
8269
if (myButton.update() == true) {
8370

84-
// the event() method returns tap, doubleTap, hold or none depending on which event was detected
71+
// The event() method returns tap, doubleTap, hold or none depending on which event was detected
8572
// the last time the update() method was called. The following code accomplishes the same thing
8673
// we did in the 'Basic' example, but I personally prefer this arrangement.
8774
switch(myButton.event()) {
@@ -103,6 +90,7 @@ void loop() {
10390
Serial.println("HOLD event detected");
10491
break;
10592
}
93+
10694
}
107-
}
95+
}
10896
}

examples/Basic/Basic.ino

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
ButtonEvents - An Arduino library for catching tap, double-tap and press-and-hold events for buttons.
3+
4+
Written by Edward Wright (fasteddy@thewrightspace.net)
5+
Available at https://github.com/fasteddy516/ButtonEvents
6+
7+
Utilizes the Bounce2 library by Thomas O. Fredericks
8+
Available at https://github.com/thomasfredericks/Bounce2
9+
10+
Example Sketch - Basic Usage:
11+
This sketch demonstrates the most simple use of the ButtonEvents library. It will monitor a button
12+
connected to pin 7 and send strings to the serial monitor indicating when one of the supported
13+
button events has occured. The events that can be detected are:
14+
15+
1) A 'tap' event is triggered after the button has been released, and the double-tap detection
16+
window has elapsed with no further button presses.
17+
18+
2) A 'double-tap' event is triggered after the button has been released and is then pressed again
19+
before the double-tap detection window has elapsed. The default double-tap detection window
20+
duration is 150ms.
21+
22+
3) A 'press-and-hold' event is triggered after the button has been pressed and held down for the hold
23+
duration. The default hold duration is set to 500ms.
24+
25+
The raw signal on the input pin has a 35ms debounce applied to it (using the Bounce2 library) by
26+
default. Unless otherwise specified, the signal on the input is assumed to be active low, meaning
27+
that when the button is pressed is generates a low signal on the pin.
28+
29+
The double-tap detection window, hold duration, debounce time and input type (active low/high) can
30+
all be adjusted as needed - please see the 'Advanced' example sketch included with this library for
31+
details. For this 'Basic' example, we will stick with the default settings.
32+
*/
33+
34+
#include <ButtonEvents.h> // we have to include the library in order to use it
35+
36+
const byte buttonPin = 7; // our button will be connected to pin 7
37+
38+
ButtonEvents myButton; // create an instance of the ButtonEvents class to attach to our button
39+
40+
41+
// this is where we run one-time setup code
42+
void setup() {
43+
44+
// Configure the button pin as a digital input with internal pull-up resistor enabled. This pin
45+
// setup needs to be done before we attach our ButtonEvents instance to the pin.
46+
pinMode(buttonPin, INPUT_PULLUP);
47+
48+
// attach our ButtonEvents instance to the button pin
49+
myButton.attach(buttonPin);
50+
51+
// initialize the arduino serial port and send a welcome message
52+
Serial.begin(9600);
53+
Serial.println("ButtonEvents 'basic' example started");
54+
}
55+
56+
57+
// this is the main loop, which will repeat forever
58+
void loop() {
59+
60+
// the update() method is where all the magic happens - it needs to be called regularly in order
61+
// to function correctly, so it should be part of the main loop.
62+
myButton.update();
63+
64+
// things to do if the button was tapped (single tap)
65+
if (myButton.tapped() == true) {
66+
Serial.println("TAP event detected");
67+
}
68+
69+
// things to do if the button was double-tapped
70+
if (myButton.doubleTapped() == true) {
71+
Serial.println("DOUBLE-TAP event detected");
72+
}
73+
74+
// things to do if the button was held
75+
if (myButton.held() == true) {
76+
Serial.println("HOLD event detected");
77+
}
78+
}

examples/Passthru/Passthru.ino

+17-12
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
to the serial monitor indicating when Bounce2 events are triggered.
1414
*/
1515

16-
#include <ButtonEvents.h> // you have to include the library if you want to use it!
16+
#include <ButtonEvents.h> // we have to include the library in order to use it
1717

18-
const byte buttonPin = 7; // the button will be connected to pin 7
18+
const byte buttonPin = 7; // our button will be connected to pin 7
1919

20-
ButtonEvents myButton; // create an instance of a ButtonEvents object to associate with our button
20+
ButtonEvents myButton; // create an instance of the ButtonEvents class to attach to our button
2121

2222

2323
// this is where we run one-time setup code
@@ -31,30 +31,35 @@ void setup() {
3131

3232
// set the debounce time to 50ms
3333
myButton.interval(50);
34-
34+
3535
// initialize the arduino serial port and send a welcome message
3636
Serial.begin(9600);
37-
Serial.println("ButtonEvents 'basic' example started");
37+
Serial.println("ButtonEvents 'Passthru' example started");
3838
}
3939

4040

4141
// this is the main loop, which will repeat forever
4242
void loop() {
4343

44-
// update the ButtonEvents (and underlying Bounce2) instance
44+
// Update the ButtonEvents (and underlying Bounce2) instance. Remember that this update() method
45+
// returns true if ButtonEvents *or* Bounce2 detects an event, so - if you are using the return
46+
// value in your code - the results will be slightly different than when using the Bounce2
47+
// update() method by itself.
4548
myButton.update();
46-
47-
// show the value returned by the Bounce2 read() method
48-
Serial.print("The value returned by myButton.read() is ");
49-
Serial.println(myButton.read());
5049

5150
// things to do if the debounced signal from the button pin went high
5251
if (myButton.rose() == true) {
53-
Serial.println("Signal on button pin went HIGH");
52+
Serial.println("Signal on button pin went HIGH");
53+
Serial.print("myButton.read() returns a value of ");
54+
Serial.println(myButton.read());
55+
Serial.println("---");
5456
}
5557

5658
// things to do if the debounced signal from the button pin went low
5759
if (myButton.fell() == true) {
58-
Serial.println("Signal on button pin went LOW");
60+
Serial.println("Signal on button pin went LOW");
61+
Serial.print("myButton.read() returns a value of ");
62+
Serial.println(myButton.read());
63+
Serial.println("---");
5964
}
6065
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
ButtonEvents - An Arduino library for catching tap, double-tap and press-and-hold events for buttons.
3+
4+
Written by Edward Wright (fasteddy@thewrightspace.net)
5+
Available at https://github.com/fasteddy516/ButtonEvents
6+
7+
Utilizes the Bounce2 library by Thomas O. Fredericks
8+
Available at https://github.com/thomasfredericks/Bounce2
9+
10+
Example Sketch - Timing Considerations:
11+
This sketch demonstrates how to avoid/mitigate some issues that can occur when the possibility of
12+
large delays between update() method calls exists in your code. It uses the same button on pin 7
13+
that we have utilized in the other examples, along with another input on pin 8 that we will pretend
14+
is connected to an active low sensor device. Note that this example isn't really meant to be executed
15+
and observed on an Arduino, it is meant to be read through.
16+
*/
17+
18+
#include <ButtonEvents.h> // we have to include the library in order to use it
19+
20+
const byte buttonPin = 7; // our button will be connected to pin 7
21+
const byte sensorPin = 8; // our pretend sensor will be "connected" to pin 8
22+
23+
ButtonEvents myButton; // create an instance of the ButtonEvents class to attach to our button
24+
25+
26+
// this is where we run one-time setup code
27+
void setup() {
28+
29+
// configure our button and sensor input pins
30+
pinMode(buttonPin, INPUT_PULLUP);
31+
pinMode(sensorPin, INPUT_PULLUP);
32+
33+
// attach our ButtonEvents instance to the button pin
34+
myButton.attach(buttonPin);
35+
36+
// initialize the arduino serial port and send a welcome message
37+
Serial.begin(9600);
38+
Serial.println("ButtonEvents 'Timing Considerations' example started");
39+
}
40+
41+
42+
// Because the ButtonEvents library uses comparisons based on millis() rather than interrupts, it
43+
// is critical that the update() method is called frequently in order to process events properly.
44+
// There may be instances in your code where execution is significantly delayed between calls to
45+
// the update() method, causing button events to trigger improperly/unexpectedly. Consider the
46+
// following main loop:
47+
void loop() {
48+
49+
// This is a tightly-packed version of our standard event detection printout logic. The update()
50+
// method is called as part of the if() statement every time the loop executes.
51+
if (myButton.update()) {
52+
switch(myButton.event()) {
53+
case (tap) : { Serial.print("TAP"); break; }
54+
case (doubleTap) : { Serial.print("DOUBLE-TAP"); break; }
55+
case (hold) : { Serial.print("HOLD"); break; }
56+
}
57+
Serial.println(" event detected");
58+
}
59+
60+
// Imagine that our sensor connected to pin 8 requires that we delay the main loop for 2 full
61+
// seconds whenever it pulls the input pin low:
62+
if (digitalRead(sensorPin) == LOW) {
63+
delay(2000); // delay for 2 seconds (2000ms) when our pretend sensor tells us to
64+
}
65+
66+
// Let's pretend that our button is tapped at the same time that our sensor pulls the signal on
67+
// 'sensorPin' low. The initial button press was detected and logged in the update() method
68+
// before the 2 second delay, and those 2 seconds will have elapsed by the time we get through
69+
// the main loop and get around to calling the update() method again. This will result in a
70+
// 'hold' event being triggered even though the button was actually only tapped. The ButtonEvents
71+
// library includes two methods to help avoid this type of incorrect mis-detection.
72+
73+
// The first method is reset(), which resets the last known state of the button (as stored by the
74+
// update() method to 'idle'.
75+
if (digitalRead(sensorPin) == LOW) {
76+
delay(2000); // delay for 2 seconds (2000ms) when our pretend sensor tells us to
77+
myButton.reset(); // reset the saved button state to 'idle' to prevent event mis-detection
78+
}
79+
80+
// Using the reset() method prevents the 'hold' event from incorrectly triggering the next time
81+
// the update() method is called. It actually prevents *any* event from triggering the next
82+
// time update() is called, including what would have been a tap event. Use the reset() method
83+
// when you want button events that get interrupted by delays to be completely ignored.
84+
85+
// The second method is retime(), which restarts the timing logic used by the update() method:
86+
if (digitalRead(sensorPin) == LOW) {
87+
delay(2000); // delay for 2 seconds (2000ms) when our pretend sensor tells us to
88+
myButton.retime(); // restart button event timing logic
89+
}
90+
91+
// Using the reset() method will allow a tap that occurred before the delay to be triggered the
92+
// next time the update() method is called, and allows hold events to trigger after the delay,
93+
// assuming that the button was pressed before the delay, and is held for the full hold duration
94+
// after the delay. Double-taps that are interrupted by delays will never be triggered.
95+
}

0 commit comments

Comments
 (0)