const byte pins[] = {2, 7, 11}; // red orange green
const byte pinCount = sizeof pins / sizeof * pins;
unsigned long stepDuration[] = {5000, 2000, 10000, 2000};
const byte stepCount = sizeof stepDuration / sizeof * stepDuration;
const int ledStates[stepCount][pinCount] = {
{HIGH, LOW , LOW }, // step 0 = RED
{HIGH, HIGH, LOW }, // step 1 = RED ORANGE
{LOW , LOW , HIGH}, // step 2 = GREEN
{LOW , HIGH, LOW }, // step 3 = ORANGE
};
unsigned long lastStepTime;
byte currentStep = 0;
// set led states according to current step and record when it happened
void setState() {
for (byte i = 0; i < pinCount; i++) digitalWrite(pins[i], ledStates[currentStep][i]);
lastStepTime = millis();
}
void setup() {
for (byte pin : pins) pinMode(pin, OUTPUT);
setState(); // initial conditions
}
void loop() {
if (millis() - lastStepTime >= stepDuration[currentStep]) { // did we spend enough time at this step?
if (++currentStep >= stepCount) currentStep = 0; // yes, calculate what's next step
setState(); // and update the led pattern accordingly
}
}