unsigned long prevMillis = 0;
unsigned long prevMillis2 = 0;
// Counter (seconds)
int counter;
int counter2;
// Finite state machine
#define COUNTING 0
#define DISPLAY 1
int State;
#define COUNTING2 0
#define DISPLAY2 1
int State2;
const int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// Starting values
counter = 10; // milisecond convert
State = COUNTING;
counter2 = 20; // milisecond convert
State2 = COUNTING2;
prevMillis = millis();
prevMillis2 = 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:
counter = 10;
prevMillis = millis();
State = COUNTING;
break;
}
break;
}
}