duration.ino 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. DESCRIPTION
  3. ====================
  4. Reports through serial (57600 baud) the time since
  5. a button press (transition from HIGH to LOW).
  6. */
  7. // Include the Bounce2 library found here :
  8. // https://github.com/thomasfredericks/Bounce2
  9. #include <Bounce2.h>
  10. #define BUTTON_PIN 2
  11. #define LED_PIN 13
  12. // Instantiate a Bounce object :
  13. Bounce debouncer = Bounce();
  14. unsigned long buttonPressTimeStamp;
  15. void setup() {
  16. Serial.begin(57600);
  17. // Setup the button with an internal pull-up :
  18. pinMode(BUTTON_PIN,INPUT_PULLUP);
  19. // After setting up the button, setup the Bounce instance :
  20. debouncer.attach(BUTTON_PIN);
  21. debouncer.interval(5);
  22. // Setup the LED :
  23. pinMode(LED_PIN,OUTPUT);
  24. }
  25. void loop() {
  26. // Update the Bounce instance :
  27. debouncer.update();
  28. // Call code if Bounce fell (transition from HIGH to LOW) :
  29. if ( debouncer.fell() ) {;
  30. Serial.println( millis()-buttonPressTimeStamp );
  31. buttonPressTimeStamp = millis();
  32. }
  33. }