MultipleButtons.ino 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * PushButton example. This shows a basic example of how to register events with a button.
  3. */
  4. #include <Button.h>
  5. #include <ButtonEventCallback.h>
  6. #include <PushButton.h>
  7. #include <Bounce2.h> // https://github.com/thomasfredericks/Bounce-Arduino-Wiring
  8. // Create an instance of PushButton reading digital pin 5
  9. PushButton button = PushButton(5, ENABLE_INTERNAL_PULLUP);
  10. PushButton button2 = PushButton(3, ENABLE_INTERNAL_PULLUP);
  11. PushButton button3 = PushButton(2, ENABLE_INTERNAL_PULLUP);
  12. void setup(){
  13. // Open up the serial port so that we can write to it
  14. Serial.begin(9600);
  15. // Configure the button as you'd like - not necessary if you're happy with the defaults
  16. button.configureButton(configurePushButton);
  17. // When the button is first pressed, call the function onButtonPressed (further down the page)
  18. button.onPress(onButtonPressed);
  19. button2.onPress(onButtonPressed);
  20. button3.onPress(onButtonPressed);
  21. // Once the button has been held for 1 second (1000ms) call onButtonHeld. Call it again every 0.5s (500ms) until it is let go
  22. button.onHoldRepeat(1000, 500, onButtonHeld);
  23. // When the button is released, call onButtonReleased
  24. button.onRelease(onButtonReleased);
  25. button2.onRelease(onButtonReleased);
  26. button3.onRelease(onButtonReleased);
  27. Serial.println("Boot complete");
  28. }
  29. void loop(){
  30. // Check the state of the button
  31. button.update();
  32. button2.update();
  33. button3.update();
  34. }
  35. // Use this function to configure the internal Bounce object to suit you. See the documentation at: https://github.com/thomasfredericks/Bounce2/wiki
  36. // This function can be left out if the defaults are acceptable - just don't call configureButton
  37. void configurePushButton(Bounce& bouncedButton){
  38. // Set the debounce interval to 15ms - default is 10ms
  39. bouncedButton.interval(15);
  40. }
  41. // btn is a reference to the button that fired the event. That means you can use the same event handler for many buttons
  42. void onButtonPressed(Button& btn){
  43. if(btn.is(button)){
  44. Serial.print("Button 1");
  45. } else if (btn.is(button2)){
  46. Serial.print("Button 2");
  47. } else if (btn.is(button3)){
  48. Serial.print("Button 3");
  49. } else {
  50. Serial.print("Hmmm, no button was");
  51. }
  52. Serial.println(" pressed");
  53. }
  54. // duration reports back how long it has been since the button was originally pressed.
  55. // repeatCount tells us how many times this function has been called by this button.
  56. void onButtonHeld(Button& btn, uint16_t duration, uint16_t repeatCount){
  57. Serial.print("button has been held for ");
  58. Serial.print(duration);
  59. Serial.print(" ms; this event has been fired ");
  60. Serial.print(repeatCount);
  61. Serial.println(" times");
  62. if(button2.isPressed()){
  63. Serial.println("Button 2 is also pressed");
  64. }
  65. }
  66. // duration reports back the total time that the button was held down
  67. void onButtonReleased(Button& btn, uint16_t duration){
  68. Serial.print("button released after ");
  69. Serial.print(duration);
  70. Serial.println(" ms");
  71. }