suggestion to simplify a tiny LED on/off code

Dear all,

Anyone can show me some tips how to simplify this code: turn on/off LEDs in turns.

void setup() {
}

void loop() {
  // loop from the lowest pin to the highest:
     
    digitalWrite(3, HIGH);
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    delay(2000);
    digitalWrite(3, LOW);
    delay(2000);
    digitalWrite(3, HIGH);
    digitalWrite(5, LOW);
    delay(2000);
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);
    delay(2000);
    digitalWrite(6, HIGH);
    digitalWrite(9, LOW);
    delay(2000);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    delay(2000);
    digitalWrite(10, HIGH);
    delay(1000);
    digitalWrite(10, LOW);
    delay(1000);
    digitalWrite(5, LOW);
    delay(1000);
    digitalWrite(9, LOW);
    delay(1000);
    digitalWrite(6, LOW);
    delay(1000);
    digitalWrite(3, LOW);
    delay(2000);
}
const byte PinMap[] = {3, 5, 6, 9, 10};

#define NbLeds sizeof(PinMap)

void setup() {
  for(byte i = 0; i < NbLeds; i++){
    pinMode(PinMap[i], OUPUT);
  }
}

void loop() {
  //Turn them all on
  for(byte i = 0; i < NbLeds; i++){
    digitalWrite(PinMap[i], HIGH);
  }

  //loop trough the rest
  for(byte i = 0; i < NbLeds; i++){
    digitalWrite(PinMap[i], LOW); //turn the leds off
    delay(2000);
    digitalWrite(PinMap[i], HIGH); //turn the last back on
  }
  
  //Part two, one by one off
  for(byte i = 0; i < sizeof(PinMap); i++){
    delay(1000);
    digitalWrite(PinMap[NbLeds - i - 1], LOW);
  }
  
  delay(2000);
}

But problem with this is you can’t do anything else with the Arduino because it’s waiting all the time. Are you planning to do something else with the Arduino as well?

And if you want something non blocking (so you can do other stuff) something like this should work.

const byte PinMap[] = {3, 5, 6, 9, 10};
const unsigned int DelayMap[] = {0, 2000, 1000, 2000};

#define NbLeds sizeof(PinMap)

void setup() {
  for(byte i = 0; i < NbLeds; i++){
    pinMode(PinMap[i], OUPUT);
  }
}

void loop() {
  ledUpdate();
  
  //do some other NON BLOCKING stuff
}

void ledUpdate(){
  static byte stage = 0;
  static unsigned int lastMillis = 0;
  static byte currentLed = 0;
  
  unsigned int currentMillis = millis();
  
  if(currentMillis - lastMillis >= DelayMap[stage]){
    lastMillis = currentMillis;
    
    switch(stage){
      case 0: //turn on all the LED's
        for(byte i = 0; i < NbLeds; i++){
          digitalWrite(PinMap[i], HIGH);
        }
        stage++;
        break;
      case 1: //walk up
        if(currentLed != 0){
          digitalWrite(PinMap[currentLed - 1], HIGH); //turn on the last
        }
        
        if(currentLed == NbLeds){
          stage++; //goto next stage
        }
        else{
          digitalWrite(PinMap[currentLed], LOW);
          curentLed++; //goto next led
        }
        break;
      case 2; //walk down
        if(currentLed == 0){
          stage++;
        }
        else{
          digitalWrite(PinMap[currentLed - 1], LOW);
        }
        break;
      case 3;
        stage = 0;
        break;
    }
  }
}