bounce2buttons.ino 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. DESCRIPTION
  3. ====================
  4. Simple example of the Bounce library that switches the debug LED when
  5. either of 2 buttons are pressed.
  6. */
  7. // Include the Bounce2 library found here :
  8. // https://github.com/thomasfredericks/Bounce2
  9. #include <Bounce2.h>
  10. #define BUTTON_PIN_1 2
  11. #define BUTTON_PIN_2 3
  12. #define LED_PIN 13
  13. // Instantiate a Bounce object
  14. Bounce debouncer1 = Bounce();
  15. // Instantiate another Bounce object
  16. Bounce debouncer2 = Bounce();
  17. void setup() {
  18. // Setup the first button with an internal pull-up :
  19. pinMode(BUTTON_PIN_1,INPUT_PULLUP);
  20. // After setting up the button, setup the Bounce instance :
  21. debouncer1.attach(BUTTON_PIN_1);
  22. debouncer1.interval(5); // interval in ms
  23. // Setup the second button with an internal pull-up :
  24. pinMode(BUTTON_PIN_2,INPUT_PULLUP);
  25. // After setting up the button, setup the Bounce instance :
  26. debouncer2.attach(BUTTON_PIN_2);
  27. debouncer2.interval(5); // interval in ms
  28. //Setup the LED :
  29. pinMode(LED_PIN,OUTPUT);
  30. }
  31. void loop() {
  32. // Update the Bounce instances :
  33. debouncer1.update();
  34. debouncer2.update();
  35. // Get the updated value :
  36. int value1 = debouncer1.read();
  37. int value2 = debouncer2.read();
  38. // Turn on the LED if either button is pressed :
  39. if ( value1 == LOW || value2 == LOW ) {
  40. digitalWrite(LED_PIN, HIGH );
  41. }
  42. else {
  43. digitalWrite(LED_PIN, LOW );
  44. }
  45. }