Advertisement
macca-nz

Multitask with single millis

Jul 12th, 2021
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Multi tasking and non blocking sketch
  3.  *  Loop uses a single mullis() timer statement and create a non-blocking loop
  4.  *  4 x LEDS, 1 x button and 5 x blink rates
  5.  *  Each button push changes the LEDs blink rates
  6.  *  loop resets on 5th button press
  7.  *  Pins used should work on Uno and ESP32.
  8.  */
  9.          
  10.           // LED Pins
  11. const int greenLed = 2;
  12. const int orangeLed = 3;
  13. const int redLed = 4;
  14. const int blueLed = 5;
  15.  
  16.           // Button Pin
  17. const int buttonPin = 12;
  18.  
  19.           // Variables
  20. uint8_t   greenState = LOW, orangeState = LOW, redState = LOW, blueState = LOW,
  21.           greenRate, orangeRate, redRate, blueRate,
  22.           greenCount = 0, orangeCount = 0, redCount = 0, blueCount = 0,
  23.           buttonState, lastBtnState = HIGH, pressCount = 0;
  24.  
  25. bool      buttonPress = false, blink = false, print = false,
  26.           updateGreen = false, updateOrange = false, updateRed = false, updateBlue = false;
  27.  
  28. long      currentTime;
  29.  
  30.           // Constants
  31. #define   DUTY 50
  32. #define   X_FAST 2
  33. #define   FAST 5
  34. #define   MEDIUM 10
  35. #define   SLOW 20
  36. #define   X_SLOW 30
  37.  
  38.      // Setup our Inputs and Outputs
  39. void setup(){
  40.     Serial.begin(115200);
  41.     while(!Serial);
  42.     pinMode(buttonPin, INPUT_PULLUP);
  43.     pinMode(greenLed, OUTPUT);
  44.     pinMode(orangeLed, OUTPUT);
  45.     pinMode(redLed, OUTPUT);
  46.     pinMode(blueLed, OUTPUT);
  47.     digitalWrite(greenLed, greenState);
  48.     digitalWrite(orangeLed, orangeState);
  49.     digitalWrite(redLed, redState);
  50.     digitalWrite(blueLed, blueState);
  51. }
  52.  
  53. void loop(){
  54.  
  55.     buttonState = digitalRead(buttonPin);       // Watch for button press
  56.     if ( buttonState != lastBtnState ){
  57.             buttonPress = true;                      
  58.     }
  59.    
  60.     if ( millis() - currentTime >= DUTY ){
  61.         currentTime = millis();                 // You must reset currentTime for next update
  62.         greenCount++;                           // Increment counters each millis() loop
  63.         orangeCount++;
  64.         redCount++;
  65.         blueCount++;
  66.         if ( buttonPress ){                     // True when the button is pressed
  67.             if ( buttonState == LOW ){
  68.                 pressCount++;
  69.                     if ( pressCount == 4){      // Assign blink rates based on button press count
  70.                         greenRate = MEDIUM;
  71.                         orangeRate = SLOW;
  72.                         redRate = X_FAST;
  73.                         blueRate = X_SLOW;
  74.                         Serial.println("Green @ 500mS, Orange @ 1000mS, Red @ 100mS and Blue @ 1500mS\n");              
  75.                     }
  76.                     if ( pressCount == 3){
  77.                         greenRate = X_SLOW;
  78.                         orangeRate = X_FAST;
  79.                         redRate = SLOW;
  80.                         blueRate = FAST;
  81.                         Serial.println("Green @ 1500mS, Orange @ 100mS, Red @ 1000mS and Blue @ 250mS\n");
  82.                     }      
  83.                     if ( pressCount == 2){
  84.                         greenRate = MEDIUM;
  85.                         orangeRate = X_SLOW;
  86.                         redRate = X_FAST;
  87.                         blueRate = MEDIUM;
  88.                         Serial.println("Green @ 500mS, Orange @ 1500mS, Red @ 100mS and Blue @ 500mS\n");
  89.                     }      
  90.                     if ( pressCount == 1){
  91.                         greenRate = SLOW;
  92.                         orangeRate = FAST;
  93.                         redRate = MEDIUM;
  94.                         blueRate = X_FAST;
  95.                         Serial.println("Green @ 1000mS, Orange @ 250mS, Red @ 500mS and Blue @ 100mS\n");
  96.                     }
  97.                     if ( pressCount > 4 ){      // Reset button press counter when MAX is reached                      
  98.                         pressCount = 0;
  99.                         Serial.println("*****     ALL LEDS OFF     *****\n");
  100.                     }
  101.                     if ( pressCount > 0 ){      // Enable LEDS when true
  102.                         blink = true;
  103.                     } else {
  104.                         blink = false;          // LEDS All "OFF" when the is true ( pressCount == 0 )
  105.                     }            
  106.             }
  107.             buttonPress = false;                // So we don't keep repeating the button press actions          
  108.         }
  109.         if ( blink ){                           // When true LEDS blink at assigned rate
  110.             if ( greenCount >= greenRate){
  111.                 greenCount = 0;
  112.                 greenState = !greenState;
  113.                 updateGreen = true;             // So we only write a LED state change
  114.             }      
  115.             if ( orangeCount >= orangeRate){
  116.                 orangeCount = 0;
  117.                 orangeState = !orangeState;
  118.                 updateOrange = true;
  119.             }
  120.             if ( redCount >= redRate){
  121.                 redCount = 0;
  122.                 redState = !redState;
  123.                 updateRed = true;
  124.             }
  125.             if ( blueCount >= blueRate){
  126.                 blueCount = 0;
  127.                 blueState = !blueState;
  128.                 updateBlue = true;
  129.             }
  130.         } else {                                // When ( blink == false ) All LEDS are "OFF"
  131.             greenState = LOW;
  132.             orangeState = LOW;
  133.             redState = LOW;
  134.             blueState = LOW;
  135.             updateGreen = true;
  136.             updateOrange = true;
  137.             updateRed = true;
  138.             updateBlue = true;
  139.         }
  140.         if (updateGreen){                       // We only need to write to the outputs when a state changes
  141.             digitalWrite(greenLed, greenState);
  142.             updateGreen = false;
  143.         }
  144.         if (updateOrange){
  145.             digitalWrite(orangeLed, orangeState);
  146.             updateOrange = false;
  147.         }
  148.         if (updateRed){
  149.             digitalWrite(redLed, redState);
  150.             updateRed = false;
  151.         }
  152.         if (updateBlue){
  153.             digitalWrite(blueLed, blueState);
  154.             updateBlue = false;
  155.         }
  156.                        
  157.     }                                           // End millis() loop
  158.     lastBtnState = buttonState;                 // To enable button de-bounce we assign current button to last button state every loop
  159. }                                               // End main loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement