// --- LED pin definitions grouped into an array --- const byte leds[4][3] = { {13, 11, 12}, // red11, green11, blue11 {10, 8, 9}, // red12, green12, blue12 { 7, 5, 6}, // red21, green21, blue21 { 4, 2, 3} // red22, green22, blue22 }; const int but = A0; unsigned long previousMillis = 0; const unsigned long interval = 2000; // 2 seconds per state bool ledsOn = true; bool lastButtonState = HIGH; unsigned long lastDebounceTime = 0; const unsigned long debounceDelay = 50; int currentState = 1; // --- Helper to set RGB for an indexed LED --- void setRGB(int idx, bool r, bool g, bool b) { digitalWrite(leds[idx][0], r ? HIGH : LOW); digitalWrite(leds[idx][1], g ? HIGH : LOW); digitalWrite(leds[idx][2], b ? HIGH : LOW); } // --- State functions --- void S1() { setRGB(0, HIGH, LOW, HIGH); setRGB(1, HIGH, LOW, HIGH); setRGB(2, LOW, HIGH, HIGH); setRGB(3, LOW, HIGH, HIGH); } void S2() { setRGB(0, LOW, LOW, HIGH); setRGB(1, LOW, LOW, HIGH); setRGB(2, LOW, HIGH, HIGH); setRGB(3, LOW, HIGH, HIGH); } void S3() { setRGB(0, LOW, HIGH, HIGH); setRGB(1, LOW, HIGH, HIGH); setRGB(2, HIGH, LOW, HIGH); setRGB(3, HIGH, LOW, HIGH); } void S4() { setRGB(0, LOW, HIGH, HIGH); setRGB(1, LOW, HIGH, HIGH); setRGB(2, LOW, LOW, HIGH); setRGB(3, LOW, LOW, HIGH); } // Turn all LEDs off void allOff() { for (int i = 0; i < 4; i++) { setRGB(i, LOW, LOW, LOW); } } // Dispatcher void showState(int s) { switch (s) { case 1: S1(); break; case 2: S2(); break; case 3: S3(); break; case 4: S4(); break; } } // --- Button handling --- void handleButton() { bool reading = digitalRead(but); unsigned long currentMillis = millis(); if (reading != lastButtonState) { lastDebounceTime = currentMillis; } if ((currentMillis - lastDebounceTime) > debounceDelay) { if (reading == LOW && lastButtonState == HIGH) { ledsOn = !ledsOn; if (!ledsOn) { allOff(); } else { showState(currentState); previousMillis = currentMillis; } } } lastButtonState = reading; } void setup() { // Initialize all LED pins for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { pinMode(leds[i][j], OUTPUT); } } pinMode(but, INPUT_PULLUP); showState(currentState); } void loop() { unsigned long currentMillis = millis(); handleButton(); if (ledsOn && (currentMillis - previousMillis >= interval)) { previousMillis = currentMillis; currentState++; if (currentState > 4) currentState = 1; showState(currentState); } }