const int switchL = 2;
const int switchR = 3;
const int firstPin = 8;
const int lastPin = 13;
int currentPin = firstPin;
int direction = 1; // 1 = hacia la derecha, -1 = hacia la izquierda
unsigned long lastUpdate = 0;
const unsigned long stepDelay = 500;
bool ledOn = false;
void setup() {
for (int i = firstPin; i <= lastPin; i++) pinMode(i, OUTPUT);
pinMode(switchL, INPUT_PULLUP);
pinMode(switchR, INPUT_PULLUP);
}
void loop() {
// lee la dirección deseada en CADA vuelta del loop, sin bloquear
if (digitalRead(switchL) == 1) direction = -1;
else if (digitalRead(switchR) == 1) direction = 1;
unsigned long now = millis();
if (now - lastUpdate >= stepDelay) {
lastUpdate = now;
if (ledOn) {
digitalWrite(currentPin, LOW);
ledOn = false;
currentPin += direction;
if (currentPin > lastPin) currentPin = firstPin;
if (currentPin < firstPin) currentPin = lastPin;
} else {
digitalWrite(currentPin, HIGH);
ledOn = true;
}
}
}