1+ /*
2+ This example demonstrates how the PushEventButton captures each individual
3+ button press as discrete events. It does not matter how long the button is
4+ pressed down or released. Only the transition between pressed and released
5+ is captured.
6+
7+ The button should be wired such that when pressed, the "buttonPin" is
8+ connected to ground.
9+
10+ The LED should be wired with the "ledPin" to the positive lead and the
11+ negative lead should be connected to ground. A current limiting resistor
12+ should be used.
13+ */
14+
15+ #include " PushEventButton.h"
16+
17+ // Change these if your button or LED are on other pins.
18+ int buttonPin = 8 ;
19+ int ledPin = 9 ;
20+
21+ // The button will automatically configure the button pin.
22+ // Because the "CAPTUREBOTH" option is used, both the down push
23+ // and the release are captured and used to flash the LED.
24+ PushEventButton button (buttonPin, PushEventButton::CAPTUREBOTH);
25+
26+ void setup ()
27+ {
28+ // Setup the output LED.
29+ pinMode (ledPin, OUTPUT);
30+ digitalWrite (ledPin, LOW);
31+ }
32+
33+ void loop ()
34+ {
35+ // This will return true only once for each button push.
36+ // This example is triggered on the press of the button. As soon as the button is pressed down
37+ // this returns true. It does not matter how long the button is held down, the light flashes
38+ // for the same amount of time. Nothing happens on the button release.
39+ bool buttonPushed = button.pushed ();
40+
41+ // Set the LED to the state of the button press.
42+ digitalWrite (ledPin, buttonPushed);
43+
44+ if (buttonPushed)
45+ {
46+ // If the button was pushed, the light will be turned on. We need a brief delay to make sure the
47+ // on state of the LED is long enough to be seen.
48+ delay (100 );
49+ }
50+ }
0 commit comments