Advertisement
jxsl13

Arduino fast blinking led circling on button press

Sep 28th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. unsigned long currentMillis = 0;
  2.  
  3. const int ledSize = 3;
  4. const int led[] = {13, 12, 8};
  5. int ledState[] = {LOW, LOW, LOW};
  6.  
  7. const int button = 7;
  8. int previousButtonState = HIGH;
  9. int buttonState = HIGH;
  10.  
  11. unsigned long previousMillis = 0;
  12. unsigned long loopCounter = 0;
  13. const long interval = 1; // 1 millisecond
  14.  
  15. // state 0 : blink every second, state 1 blink 3 times
  16. int ledStateMachine[] = {0, 0, 0};
  17. int currentLed = -1;
  18. int togglesLeft[] = {6, 6, 6};
  19.  
  20. void setup()
  21. {
  22.     for (int i = 0; i < ledSize; i++)
  23.         pinMode(led[i], OUTPUT);
  24. }
  25.  
  26. bool buttonWasPressed()
  27. {
  28.     previousButtonState = buttonState;
  29.     buttonState = digitalRead(button);
  30.  
  31.     return previousButtonState == HIGH && buttonState == LOW;
  32. }
  33.  
  34. void handleLedStuff(int i, unsigned long loopCounter)
  35. {
  36.     if (i == currentLed && ledStateMachine[i] == 1 && togglesLeft[i] == 0)
  37.     {
  38.         // no toggles left, reset state to default
  39.         ledStateMachine[i] = 0;
  40.         // set toggles back to default value
  41.         togglesLeft[i] = 6;
  42.     }
  43.     else if (i == currentLed && ledStateMachine[i] == 1 && loopCounter % 300 == 0)
  44.     {
  45.         // toggles left, toggle and decrease toggles every 300 milliseconds
  46.         ledState[i] = !ledState[i];
  47.         togglesLeft[i]--;
  48.     }
  49.     else if (loopCounter % 2000 == 0)
  50.     {
  51.         // manually set led to low on every even second
  52.         // in order not to get out of sync, with the previously fast
  53.         // blinking led
  54.         ledState[i] = LOW;
  55.     }
  56.     else if(loopCounter % 1000 == 0)
  57.     {
  58.         // manually set led to on every odd second
  59.         // needed in order not to get out of sync
  60.         // with the previously fast blinking led
  61.         ledState[i] = HIGH;
  62.     }
  63. }
  64.  
  65. bool blinkingLedExists()
  66. {
  67.     for (int i = 0; i < ledSize; i++)
  68.     {
  69.         if (ledStateMachine[i] == 1)
  70.             return true;
  71.     }
  72.     return false;
  73. }
  74.  
  75. void loop()
  76. {
  77.     // get state
  78.     bool buttonPressed = buttonWasPressed();
  79.     currentMillis = millis();
  80.  
  81.     if (buttonPressed)
  82.     {
  83.         if(!blinkingLedExists())
  84.         {
  85.             // configure current led to be the blinking led
  86.             // only continue if no blinking led exists
  87.             // we don't want weird stuff to happen if the button is pressed too fast
  88.             currentLed = (currentLed + 1) % 3;
  89.             ledStateMachine[currentLed] = 1;
  90.         }
  91.     }
  92.  
  93.     // evaluate every millisecond
  94.     if (currentMillis - previousMillis >= interval)
  95.     {
  96.         previousMillis = currentMillis;
  97.  
  98.         for (int i = 0; i < ledSize; i++)
  99.         {
  100.             handleLedStuff(i, loopCounter);
  101.         }
  102.  
  103.         loopCounter++;
  104.     }
  105.  
  106.     // set state
  107.     for (int i = 0; i < ledSize; i++)
  108.         digitalWrite(led[i], ledState[i]);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement