const int ledPins[] = { 23, 22, 33, 25, 26, 27 }; // LED pins from left to right
const int numLeds = sizeof(ledPins) / sizeof(ledPins[0]);
const int buttonPin = 4; // Pushbutton
int sequenceDirection = 1; // Initial direction: forward
int currentLed = 0; // Index of the current LED
void setup() {
for (int i = 0; i < numLeds; i++) {
pinMode(ledPins[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
sequenceDirection *= -1;
delay(100);
Serial.println("Button Pressed! Direction: " + String(sequenceDirection));
}
currentLed += sequenceDirection; // Move to the next LED
// Wrap around the LED index if it goes beyond the array bounds
if (currentLed >= numLeds) {
currentLed = 0;
} else if (currentLed < 0) {
currentLed = numLeds - 1;
}
// Turn off all LEDs
for (int i = 0; i < numLeds; i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on the current LED
digitalWrite(ledPins[currentLed], HIGH);
delay(250); // Delay between LED changes
}