SHARE
TWEET

Untitled

a guest Feb 16th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define DEBOUNCE 10
  2. #define COOLOFF 10
  3.  
  4. #define PRESS_LONG 1000
  5.  
  6. #define BUTTON_LEFT 2
  7. #define BUTTON_RIGHT 3
  8. #define LED_OUTPUT 4
  9.  
  10. volatile unsigned long buttonLeftPressed = 0;
  11. volatile unsigned long buttonLeftReleased = 0;
  12.  
  13. int output = 0;
  14.  
  15. void buttonLeftPress() {
  16.   unsigned long t = millis();
  17.  
  18.   if(buttonLeftPressed > 0
  19.   && t - buttonLeftPressed <= DEBOUNCE) {
  20.     return;
  21.   }
  22.  
  23.   if(buttonLeftReleased > 0
  24.   && t - buttonLeftReleased <= COOLOFF) {
  25.     return;
  26.   }
  27.  
  28.   buttonLeftPressed = t;
  29. }
  30.  
  31. void buttonLeftRelease() {
  32.   unsigned long t = millis();
  33.  
  34.   if(buttonLeftReleased > 0
  35.   && t - buttonLeftReleased <= DEBOUNCE) {
  36.     return;
  37.   }
  38.  
  39.   if(buttonLeftPressed > 0
  40.   && t - buttonLeftPressed <= COOLOFF) {
  41.     return;
  42.   }
  43.  
  44.   buttonLeftReleased = t;
  45. }
  46.  
  47. void checkButtons() {
  48.   if(buttonLeftReleased > 0) {
  49.     unsigned long duration = buttonLeftReleased - buttonLeftPressed;
  50.  
  51.     if(duration >= PRESS_LONG) {
  52.       output += 20;
  53.     }
  54.     else {
  55.       output = 2;
  56.     }
  57.    
  58.     buttonLeftPressed = 0;
  59.     buttonLeftReleased = 0;
  60.   }
  61. }
  62.  
  63. void setup() {
  64.   Serial.begin(9600);
  65.  
  66.   pinMode(BUTTON_LEFT, INPUT);
  67.  
  68.   attachInterrupt(digitalPinToInterrupt(BUTTON_LEFT), buttonLeftPress, FALLING);
  69.   attachInterrupt(digitalPinToInterrupt(BUTTON_LEFT), buttonLeftRelease, RISING);
  70. }
  71.  
  72. void loop() {
  73.   checkButtons();
  74.  
  75.   while(output > 0) {
  76.     Serial.println(output, DEC);
  77.     delay(100);
  78.     output--;
  79.   }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top