Skip to content

Commit

Permalink
Updated PushEventButton to allow for capturing both push and release.
Browse files Browse the repository at this point in the history
Updated PushEventButton to allow for capturing both push and release.
  • Loading branch information
lendres committed Jun 28, 2021
1 parent 79a09a4 commit 42becd9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
Binary file modified documentation/Documentation.pptx
Binary file not shown.
Binary file modified documentation/Two State Button Comparison.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions examples/PushEventButton_Demo_03/PushEventButton_Demo_03.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
This example demonstrates how the PushEventButton captures each individual
button press as discrete events. It does not matter how long the button is
pressed down or released. Only the transition between pressed and released
is captured.
The button should be wired such that when pressed, the "buttonPin" is
connected to ground.
The LED should be wired with the "ledPin" to the positive lead and the
negative lead should be connected to ground. A current limiting resistor
should be used.
*/

#include "PushEventButton.h"

// Change these if your button or LED are on other pins.
int buttonPin = 8;
int ledPin = 9;

// The button will automatically configure the button pin.
// Because the "CAPTUREBOTH" option is used, both the down push
// and the release are captured and used to flash the LED.
PushEventButton button(buttonPin, PushEventButton::CAPTUREBOTH);

void setup()
{
// Setup the output LED.
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}

void loop()
{
// This will return true only once for each button push.
// This example is triggered on the press of the button. As soon as the button is pressed down
// this returns true. It does not matter how long the button is held down, the light flashes
// for the same amount of time. Nothing happens on the button release.
bool buttonPushed = button.pushed();

// Set the LED to the state of the button press.
digitalWrite(ledPin, buttonPushed);

if (buttonPushed)
{
// If the button was pushed, the light will be turned on. We need a brief delay to make sure the
// on state of the LED is long enough to be seen.
delay(100);
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ButtonSuite
version=2.0.0
version=2.1.0
author=Lance A. Endres <[email protected]>
maintainer=Lance A. Endres <[email protected]>
sentence=A library for using a simple mechanical push (momentary) button as a momentary button, a latching button, a counter, an enumerator, and more.
Expand Down
16 changes: 14 additions & 2 deletions src/PushEventButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ bool PushEventButton::pushed()
{
case CAPTUREPUSH:
{
if (status == BUTTONSUITE::JUSTPRESSED)
if (status & BUTTONSUITE::JUSTPRESSED)
{
return true;
}
Expand All @@ -71,7 +71,19 @@ bool PushEventButton::pushed()

case CAPTURERELEASE:
{
if (status == BUTTONSUITE::BUTTONSTATUS::WASSHORTPRESSED || status == BUTTONSUITE::BUTTONSTATUS::WASLONGPRESSED)
if (status & (BUTTONSUITE::WASSHORTPRESSED | BUTTONSUITE::WASLONGPRESSED))
{
return true;
}
else
{
return false;
}
}

case CAPTUREBOTH:
{
if (status & (BUTTONSUITE::JUSTPRESSED | BUTTONSUITE::WASSHORTPRESSED | BUTTONSUITE::WASLONGPRESSED))
{
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/PushEventButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class PushEventButton : public TwoStateButton
enum CAPTURETYPE
{
CAPTUREPUSH,
CAPTURERELEASE
CAPTURERELEASE,
CAPTUREBOTH
};

public:
Expand Down

0 comments on commit 42becd9

Please sign in to comment.