const int ledPins[] = {2, 3, 4, 5}; // LED 1 to LED 4
const int buttonPin = 6;
int patternIndex = 0;
int phaseIndex = 0;
unsigned long lastUpdate = 0;
const unsigned long interval = 500; // 1 second
bool lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
// Patterns: each pattern has multiple phases, each phase is a 4-element LED state
const bool patterns[][5][4] = {
{ {1, 1, 1, 1}, {0, 0, 0, 0} }, // Pattern 1
{ {1, 0, 1, 0}, {0, 1, 0, 1} }, // Pattern 2
{ {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1} }, // Pattern 3
{ {1, 1, 0, 0}, {0, 0, 1, 1} }, // Pattern 4
{ {1, 0, 0, 1}, {0, 1, 1, 0} } // Pattern 5
};
const int patternLengths[] = {2, 2, 4, 2, 2}; // Number of phases per pattern
void setup() {
for (int i = 0; i < 4; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP); // Button active LOW
}
void loop() {
unsigned long currentTime = millis();
// Handle LED phase update
if (currentTime - lastUpdate >= interval) {
lastUpdate = currentTime;
// Show current phase
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], patterns[patternIndex][phaseIndex][i] ? HIGH : LOW);
}
// Move to next phase
phaseIndex++;
if (phaseIndex >= patternLengths[patternIndex]) {
phaseIndex = 0;
}
}
// Handle button press to change pattern
bool reading = !digitalRead(buttonPin); // Invert for pull-up logic
if (reading != lastButtonState) {
lastDebounceTime = currentTime;
}
if ((currentTime - lastDebounceTime) > debounceDelay) {
if (reading == HIGH) {
patternIndex = (patternIndex + 1) % 5;
phaseIndex = 0; // Reset phase when switching pattern
}
}
lastButtonState = reading;
}