Advertisement
baldengineer

quick state machine

Feb 4th, 2016
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.56 KB | None | 0 0
  1. const int MaxState = 8;
  2. int state = 1;
  3. unsigned long currentMillis;
  4. unsigned long previousMillis;
  5. unsigned long interval=2000;
  6. //arrays are zero based, so ignore the first element
  7. //fill this with the pin numbers of your LEDs, in order
  8. int ledArray[] = {0,1,2,3,4,5,6,7};
  9. void loop() {
  10.    
  11.   currentMillis = millis();
  12.   if (currentMillis - previousMillis >= interval) {
  13.     digitalWrite(ledArray[state], HIGH);
  14.     state++;
  15.  
  16.     if (state == MaxState) {
  17.       // turn off all the LEDs
  18.       state = 1;
  19.     }
  20.     previousMillis = currentMillis;
  21.   }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement