|
| 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