Advertisement
macca-nz

short_or_long_button_push

Nov 10th, 2020 (edited)
91
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ezButton Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
  3.  *
  4.  * millisDelay library download http://www.forward.com.au/pfod/ArduinoProgramming/millisDelay.zip
  5.  *
  6.  * Functionality of Sketch:
  7.  * Non Blocking
  8.  * Detect Short or Long Button Push with fast and slow LED flash rates respectively
  9.  * Quick Flash / Short Button Push has a shorter run duration than Slow Flash / Long Button Push
  10.  * Create condition that if a flash is running a new button push is not actioned
  11.  * Serial Print all actions
  12.  *
  13.  */
  14.  
  15. #include <ezButton.h>     // You can use Library Manager to install
  16. #include <millisDelay.h>  // Link in header
  17.  
  18. const int LED = 3;
  19. int runState = 0;
  20.  
  21. //On State Timeout delays
  22. const unsigned long FAST_DELAY_TIME = 10000;
  23. const unsigned long SLOW_DELAY_TIME = 20000;
  24. millisDelay ledDelay;
  25.  
  26. // Create ezButton instance
  27. const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
  28. const int LONG_PRESS_TIME  = 1000; // 1000 milliseconds
  29. ezButton button(2);  // create ezButton object that attach to pin 2;
  30. unsigned long pressedTime  = 0;
  31. unsigned long releasedTime = 0;
  32.  
  33. // Setup LED Blink Rates
  34. byte ledState = 0;
  35. unsigned long shortPeriods[] = {300, 100}; //Short Push Fast Flash rate of LED
  36. unsigned long longPeriods[] = {800, 300}; //Long Push Slow Flash rate of LED
  37. unsigned long currentFlashMillis;
  38. unsigned long startFlashMillis;
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   button.setDebounceTime(50); // set debounce time to 50 milliseconds
  43.   pinMode(LED, OUTPUT);
  44.   digitalWrite(LED, LOW);
  45. }
  46.  
  47. void loop() {
  48.   button.loop(); // MUST call the loop() function first
  49.  
  50.   if((ledDelay.isRunning() == true) && (button.isPressed() == true)){
  51.     Serial.println("Loop in Progress");
  52.   }
  53.   if(ledDelay.isRunning() == false){
  54.  
  55.   if(button.isPressed())
  56.     pressedTime = millis();
  57.  
  58.   if(button.isReleased()) {
  59.     releasedTime = millis();
  60.  
  61.     long pressDuration = releasedTime - pressedTime;
  62.  
  63.     if( pressDuration < SHORT_PRESS_TIME ){
  64.       Serial.println("Fast Flashing Started");
  65.       ledDelay.start(FAST_DELAY_TIME);
  66.       runState=1;}
  67.  
  68.     if( pressDuration > LONG_PRESS_TIME ){
  69.       Serial.println("Slow Flash Rate Started");
  70.       ledDelay.start(SLOW_DELAY_TIME);
  71.       runState=2;}
  72.       }
  73.   }
  74.         if(runState==1){
  75.         currentFlashMillis = millis();
  76.     if (currentFlashMillis - startFlashMillis >= shortPeriods[ledState % 2])
  77.       {
  78.       ledState++;
  79.       digitalWrite(LED, ledState % 2);
  80.       startFlashMillis = currentFlashMillis;
  81.       }
  82.     }
  83.  
  84.         if(runState==2){
  85.         currentFlashMillis = millis();
  86.     if (currentFlashMillis - startFlashMillis >= longPeriods[ledState % 2])
  87.       {
  88.       ledState++;
  89.       digitalWrite(LED, ledState % 2);
  90.       startFlashMillis = currentFlashMillis;
  91.       }
  92.     }
  93.       if (ledDelay.justFinished()) {
  94.       Serial.println("LED Sequence Finished");
  95.       digitalWrite(LED, LOW);
  96.       runState=0;
  97.       }
  98. }
Advertisement
RAW Paste Data Copied
Advertisement