unsigned long prevMillis = 0;
unsigned long prevMillis2 = 0;
unsigned long prevMillis3 = 0;
unsigned long prevMillis4 = 0;
unsigned long prevMillis5 = 0;
// Counter (seconds)
int counter;
int counter2;
int counter3;
int counter4;
int counter5;
// Finite state machine
#define COUNTING 0
#define DISPLAY 1
int State;
#define COUNTING2 0
#define DISPLAY2 1
int State2;
#define COUNTING3 0
#define DISPLAY3 1
int State3;
#define COUNTING4 0
#define DISPLAY4 1
int State4;
#define COUNTING5 0
#define DISPLAY5 1
int State5;
const int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Starting values
counter = 5; // milisecond convert
State = COUNTING;
counter2 = 2; // milisecond convert
State2 = COUNTING2;
counter3 = 5; // milisecond convert
State3 = COUNTING3;
counter4 = 2; // milisecond convert
State4 = COUNTING4;
counter5 = 5; // milisecond convert
State5 = COUNTING5;
prevMillis = millis();
prevMillis2 = millis();
prevMillis3 = millis();
prevMillis4 = millis();
prevMillis5 = millis();
}
void loop () {
test();
if (millis() - previousMillis >= 1000) {
// save the last time you blinked the LED
previousMillis = millis();
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
void test() {
switch(State) {
case COUNTING:
// the device you want to turn on first
if(millis() - prevMillis >= 1000) {
prevMillis = millis();
Serial.println(--counter);
if(counter <= 0) {
State = DISPLAY;
}
}
break;
case DISPLAY:
switch(State2) {
case COUNTING2:
// the next device after the first is done
if(millis() - prevMillis2 >= 1000) {
prevMillis2 = millis();
Serial.println(--counter2);
if(counter2 <= 0) {
State2 = DISPLAY2;
}
}
break;
case DISPLAY2:
switch(State3) {
case COUNTING3:
// the next device after the first is done
if(millis() - prevMillis3 >= 1000) {
prevMillis3 = millis();
Serial.println(--counter3);
if(counter3 <= 0) {
State3 = DISPLAY3;
}
}
break;
case DISPLAY3:
switch(State4) {
case COUNTING4:
// the next device after the first is done
if(millis() - prevMillis4 >= 1000) {
prevMillis4 = millis();
Serial.println(--counter4);
if(counter4 <= 0) {
State4 = DISPLAY4;
}
}
break;
case DISPLAY4:
switch(State5) {
case COUNTING5:
// the next device after the first is done
if(millis() - prevMillis5 >= 1000) {
prevMillis5 = millis();
Serial.println(--counter5);
if(counter5 <= 0) {
State5 = DISPLAY5;
}
}
break;
case DISPLAY5:
prevMillis = millis();
State = COUNTING;
break;
}
break;
}
break;
}
break;
}
break;
}
}