bounce.ino 947 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. DESCRIPTION
  3. ====================
  4. Simple example of the Bounce library that switches the debug LED when a button is pressed.
  5. */
  6. // Include the Bounce2 library found here :
  7. // https://github.com/thomasfredericks/Bounce2
  8. #include <Bounce2.h>
  9. #define BUTTON_PIN 2
  10. #define LED_PIN 13
  11. // Instantiate a Bounce object
  12. Bounce debouncer = Bounce();
  13. void setup() {
  14. // Setup the button with an internal pull-up :
  15. pinMode(BUTTON_PIN,INPUT_PULLUP);
  16. // After setting up the button, setup the Bounce instance :
  17. debouncer.attach(BUTTON_PIN);
  18. debouncer.interval(5); // interval in ms
  19. //Setup the LED :
  20. pinMode(LED_PIN,OUTPUT);
  21. }
  22. void loop() {
  23. // Update the Bounce instance :
  24. debouncer.update();
  25. // Get the updated value :
  26. int value = debouncer.read();
  27. // Turn on or off the LED as determined by the state :
  28. if ( value == LOW ) {
  29. digitalWrite(LED_PIN, HIGH );
  30. }
  31. else {
  32. digitalWrite(LED_PIN, LOW );
  33. }
  34. }