learnelectronics

Detect single, double, and long press

Sep 25th, 2017
23,337
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 1 0
  1. /*
  2.  * One Button two button red button blue button
  3.  *
  4.  * learnelectronics
  5.  * 23 Sept 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  *
  9.  * Find the library here: http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
  10.  */
  11.  
  12. #include "OneButton.h"                              //we need the OneButton library
  13.  
  14.  
  15. OneButton button(A1, true);                         //attach a button on pin A1 to the library
  16.  
  17.  
  18.  
  19. void setup() {
  20.  
  21.   pinMode(13, OUTPUT);                              // sets the digital pin as output
  22.   pinMode(12, OUTPUT);                              // sets the digital pin as output
  23.   pinMode(11, OUTPUT);                              // sets the digital pin as output
  24.  
  25.      
  26.   button.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  27.   button.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  28.   button.attachLongPressStop(longclick);            // link the function to be called on a longpress event.
  29. }
  30.  
  31.  
  32.  
  33. void loop() {
  34.  
  35.   button.tick();                                    // check the status of the button
  36.  
  37.  
  38.   delay(10);                                        // a short wait between checking the button
  39. } // loop
  40.  
  41.  
  42.  
  43. void doubleclick() {                                // what happens when button is double-clicked
  44.  digitalWrite(11,HIGH);                             // light the green LED
  45.  delay(1000);                                       // wait one second
  46.  digitalWrite(11,LOW);                              // turn off green LED
  47. }
  48.  
  49. void singleclick(){                                 // what happens when the button is clicked
  50.   digitalWrite(12,HIGH);                            // light the red LED
  51.  delay(1000);                                       // wait one second
  52.  digitalWrite(12,LOW);                              // turn off the gren led
  53. }
  54.  
  55. void longclick(){                                   // what happens when buton is long-pressed
  56.   digitalWrite(13,HIGH);                            // light the blue LED
  57.  delay(1000);                                       // wait one second
  58.  digitalWrite(13,LOW);                              // turn off the blue LED
  59. }
Advertisement
Add Comment
Please, Sign In to add comment