Io sono un fan dei metodi ricorsivi, meno righe di codice ed algoritmo più efficiente.
Ti propongo una possibili soluzione "alternativa" al tuo approccio:
const byte outputLed[] = {13, 12, 11, 10, 9};
const byte button1 = 3;
const byte button2 = 2;
int maxIndex = sizeof(outputLed);
int onIndex = 0 ;
int offIndex = maxIndex -1;
void setup(){
Serial.begin(9600);
for (int i=0; i<sizeof(outputLed); i++){
pinMode(outputLed[i], OUTPUT);
}
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
delay(1000);
}
void loop(){
if(digitalRead(button1) == LOW){
// Debounce pulsante (meglio hardware)
delay(100);
// Accensione/spegnimento del led corrispondente all'indice
digitalWrite(outputLed[onIndex], HIGH);
digitalWrite(outputLed[offIndex], LOW);
onIndex = (onIndex +1) % maxIndex;
offIndex = (offIndex +1) % maxIndex;
}
// Se l'ultimo led è acceso e premo il pulsante, modifico maxIndex
// in modo da escludere l'ultimo led dalla sequenza
if((digitalRead(button2) == LOW)
&& (digitalRead(outputLed[maxIndex]) == HIGH))
{
maxIndex = sizeof(outputLed) - 1;
offIndex = maxIndex -1;
}
}