Advertisement
learnelectronics

Detect single, double, and long press

Sep 25th, 2017
21,863
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.  * arduino0169@gmail.com
  9.  *
  10.  * Find the library here: http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
  11.  */
  12.  
  13. #include "OneButton.h"                              //we need the OneButton library
  14.  
  15.  
  16. OneButton button(A1, true);                         //attach a button on pin A1 to the library
  17.  
  18.  
  19.  
  20. void setup() {
  21.  
  22.   pinMode(13, OUTPUT);                              // sets the digital pin as output
  23.   pinMode(12, OUTPUT);                              // sets the digital pin as output
  24.   pinMode(11, OUTPUT);                              // sets the digital pin as output
  25.  
  26.      
  27.   button.attachDoubleClick(doubleclick);            // link the function to be called on a doubleclick event.
  28.   button.attachClick(singleclick);                  // link the function to be called on a singleclick event.
  29.   button.attachLongPressStop(longclick);            // link the function to be called on a longpress event.
  30. }
  31.  
  32.  
  33.  
  34. void loop() {
  35.  
  36.   button.tick();                                    // check the status of the button
  37.  
  38.  
  39.   delay(10);                                        // a short wait between checking the button
  40. } // loop
  41.  
  42.  
  43.  
  44. void doubleclick() {                                // what happens when button is double-clicked
  45.  digitalWrite(11,HIGH);                             // light the green LED
  46.  delay(1000);                                       // wait one second
  47.  digitalWrite(11,LOW);                              // turn off green LED
  48. }
  49.  
  50. void singleclick(){                                 // what happens when the button is clicked
  51.   digitalWrite(12,HIGH);                            // light the red LED
  52.  delay(1000);                                       // wait one second
  53.  digitalWrite(12,LOW);                              // turn off the gren led
  54. }
  55.  
  56. void longclick(){                                   // what happens when buton is long-pressed
  57.   digitalWrite(13,HIGH);                            // light the blue LED
  58.  delay(1000);                                       // wait one second
  59.  digitalWrite(13,LOW);                              // turn off the blue LED
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement