Switching multiple digital outputs

Hello everyone,

I've written some code to control 8 LED's via serial commands. It works but the code feels excessive, like there should be an easier way to achieve the same thing. I've searched around but I can't seem to find an answer.

Here's the code:

//8 pin out names
int LED1 = 3;
int LED2 = 4;
int LED3 = 5;
int LED4 = 6;
int LED5 = 7;
int LED6 = 8;
int LED7 = 9;
int LED8 = 10;

int LEDnr = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available() > 0) {
    LEDnr = Serial.read();
    Serial.print("Current LED: ");
    Serial.println(LEDnr);
  }
  if (LEDnr == '1') {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '2') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '3') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '4') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '5') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, HIGH);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '6') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, HIGH);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '7') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, HIGH);
    digitalWrite(LED8, LOW);
  } else if (LEDnr == '8') {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    digitalWrite(LED6, LOW);
    digitalWrite(LED7, LOW);
    digitalWrite(LED8, HIGH);
  }
}

Any help would be appreciated!

Does this work:

//8 pin out names
int LED1 = 3;
int LED2 = 4;
int LED3 = 5;
int LED4 = 6;
int LED5 = 7;
int LED6 = 8;
int LED7 = 9;
int LED8 = 10;

int LEDnr = 0;

const int LEDs[] =
{
    LED1, LED2, LED3, LED4,
    LED5, LED6, LED7, LED8
};
    
    

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for( int i=0; i<8; i++ )
    pinMode( LEDs[i], OUTPUT );

}

void loop() 
{
    // put your main code here, to run repeatedly:
    if (Serial.available() > 0) 
    {
        LEDnr = Serial.read();
        Serial.print("Current LED: ");
        Serial.println(LEDnr);
        if( LEDnr > '0' && LEDnr <= '8' ) //thanks Railroader
        {
            LEDnr -= '1';
                    
            for( int i=0; i<8; i++ )
            {
                if( i == LEDnr )
                    digitalWrite(LEDs[i], HIGH);
                else
                    digitalWrite(LEDs[i], LOW);
            }//for
            
        }//if
    
    }//if
    
}//loop

Keep it simple are words told in many different situations. Your code is simple and understandable but typing intensive.
In the end of the "if Serial avalable" You can call a function "All_Off" before You enter the if-ladder.

There are probably many other sophisticated ways to go...

@Blackfin
Excellent!
What about if( LEDnr > ‘0’ && LEDnr <= ‘8’ )?

Railroader:
@Blackfin
Excellent!
What about if( LEDnr > ‘0’ && LEDnr <= ‘8’ )?

Hmm. Yes, since he’s typing ‘1’ to ‘8’. Then I would also change:

LEDnr -= '0';

to

LEDnr -= '1';

Thanks. Post modified.

Details… Many happenings concists of start-, running- and end- actions. That's trimming, tuning, the code….

Thank you so much Blackfin & Railroader, it works like a charm!

Hi,
Looks good guys, but please don't keep going back and editing code that has posted.
The thread has become untrackable due to back editing.
Its best to post new edited code each time.

Thanks.. Tom... :slight_smile:

You can shorten loop() just a smidge more with:

void loop() 
{
    // put your main code here, to run repeatedly:
    if (Serial.available() > 0) 
    {
        LEDnr = Serial.read();
        Serial.print("Current LED: ");
        Serial.println(LEDnr);
        LEDnr--;
        if( LEDnr >= '0' && LEDnr < '7' )
            for( int i=0; i<8; i++ )
                digitalWrite(LEDs[i], (i==LEDnr)?HIGH:LOW );
    
    }//if
    
}//loop
        if( LEDnr >= '0' && LEDnr < '7' )
            for( int i=0; i<8; i++ )
                digitalWrite(LEDs[i], (i==LEDnr)?HIGH:LOW )

That’s a very complicated way to switch all LEDs off.

Whandall:

        if( LEDnr >= '0' && LEDnr < '7' )

for( int i=0; i<8; i++ )
                digitalWrite(LEDs[i], (i==LEDnr)?HIGH:LOW )



That's a very complicated way to switch all LEDs off.

Unless I messed up in the event ‘i’ is equal to ‘LEDnr’ the pin will be written HIGH; all others will be low.

Or did I miss something?

Obviously.

if( LEDnr >= '0' && LEDnr < '7' )

LEDnr is between ‘0’ and ‘6’

 for( int i=0; i<8; i++ )

i is between 0 and 7.

Ah yes. Should have been “<=7”.

        if( LEDnr >= '0' && LEDnr < '7' )
            for( int i=0; i<8; i++ )
                digitalWrite(LEDs[i], (i == LEDnr - '0') ? HIGH:LOW )

I think this way it’ll work better.