Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ezButton Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
- *
- * millisDelay library download http://www.forward.com.au/pfod/ArduinoProgramming/millisDelay.zip
- *
- * Functionality of Sketch:
- * Non Blocking
- * Detect Short or Long Button Push with fast and slow LED flash rates respectively
- * Quick Flash / Short Button Push has a shorter run duration than Slow Flash / Long Button Push
- * Create condition that if a flash is running a new button push is not actioned
- * Serial Print all actions
- *
- */
- #include <ezButton.h> // You can use Library Manager to install
- #include <millisDelay.h> // Link in header
- const int LED = 3;
- int runState = 0;
- //On State Timeout delays
- const unsigned long FAST_DELAY_TIME = 10000;
- const unsigned long SLOW_DELAY_TIME = 20000;
- millisDelay ledDelay;
- // Create ezButton instance
- const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds
- const int LONG_PRESS_TIME = 1000; // 1000 milliseconds
- ezButton button(2); // create ezButton object that attach to pin 2;
- unsigned long pressedTime = 0;
- unsigned long releasedTime = 0;
- // Setup LED Blink Rates
- byte ledState = 0;
- unsigned long shortPeriods[] = {300, 100}; //Short Push Fast Flash rate of LED
- unsigned long longPeriods[] = {800, 300}; //Long Push Slow Flash rate of LED
- unsigned long currentFlashMillis;
- unsigned long startFlashMillis;
- void setup() {
- Serial.begin(9600);
- button.setDebounceTime(50); // set debounce time to 50 milliseconds
- pinMode(LED, OUTPUT);
- digitalWrite(LED, LOW);
- }
- void loop() {
- button.loop(); // MUST call the loop() function first
- if((ledDelay.isRunning() == true) && (button.isPressed() == true)){
- Serial.println("Loop in Progress");
- }
- if(ledDelay.isRunning() == false){
- if(button.isPressed())
- pressedTime = millis();
- if(button.isReleased()) {
- releasedTime = millis();
- long pressDuration = releasedTime - pressedTime;
- if( pressDuration < SHORT_PRESS_TIME ){
- Serial.println("Fast Flashing Started");
- ledDelay.start(FAST_DELAY_TIME);
- runState=1;}
- if( pressDuration > LONG_PRESS_TIME ){
- Serial.println("Slow Flash Rate Started");
- ledDelay.start(SLOW_DELAY_TIME);
- runState=2;}
- }
- }
- if(runState==1){
- currentFlashMillis = millis();
- if (currentFlashMillis - startFlashMillis >= shortPeriods[ledState % 2])
- {
- ledState++;
- digitalWrite(LED, ledState % 2);
- startFlashMillis = currentFlashMillis;
- }
- }
- if(runState==2){
- currentFlashMillis = millis();
- if (currentFlashMillis - startFlashMillis >= longPeriods[ledState % 2])
- {
- ledState++;
- digitalWrite(LED, ledState % 2);
- startFlashMillis = currentFlashMillis;
- }
- }
- if (ledDelay.justFinished()) {
- Serial.println("LED Sequence Finished");
- digitalWrite(LED, LOW);
- runState=0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement