Multiple leds control by bits.

Sorry im completly amatour.
But how i can control many leds in one command.
Right now my code looks like
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2000);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
etc.
Is any easy way to make it cleaner and control many leds in one line.
Something like
digitalWrite(leds,0,0,1,0,0);
delay(2000);
digitalWrite(leds,1,0,0,0,0);

I hope u got it.
I was looking for something like that long time, sorry if is topic already. I cannot find it.
Thank You.

Yes, there is:

  for (byte i=0; i <5; i++) {
    digitalWrite(led[i], HIGH);
    delay(2000);
    digitalWrite(led[i], LOW);
  }

It’s not going to turn on all leds in same time ?

I mean nore like send 0 as low and 1 as high and control many leds separate by chosing bit in one row like 0,0,1,1,0 and change led pattern by setting new bits for outputs for example 1,1,1,1,1 set all leds on?

ourniti:
It's not going to turn on all leds in same time ?

No, only one.

ourniti:
I mean nore like send 0 as low and 1 as high and control many leds separate by chosing bit in one row like 0,0,1,1,0 and change led pattern by setting new bits for outputs for example 1,1,1,1,1 set all leds on?

OK

void setMyLeds (byte l1, byte l2, byte l3, byte l4, byte l5) {
  digitalWrite(led1, l1==0?LOW:HIGH);
  digitalWrite(led2, l2==0?LOW:HIGH);
  digitalWrite(led3, l3==0?LOW:HIGH);
  digitalWrite(led4, l4==0?LOW:HIGH);
  digitalWrite(led5, l5==0?LOW:HIGH);
}
...
...
setMyLeds(0,0,1,0,0);
delay(2000);
setMyLeds(1,0,0,0,0);

YES it's what im looking for.
Thank you so so much.
:slight_smile:

No problem. Do you have any questions about how it works? It's important to me that you have understood how it works, not just blindly accepted that it does work.

Hi . I'm thinking I learned how it's working. I wrote small test code.
Everything works perfect.
Thank you for your help and knowledge.

//Sample

int led1 = 8;
int led2 = 9;
int led3 = 10;
int led4 = 11;
int led5 = 12;

void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
}

void loop() {
setMyLeds(0, 0, 0, 0, 0);
delay(1000);
setMyLeds(1, 0, 0, 0, 0);
delay(1000);
setMyLeds(0, 1, 0, 0, 0);
delay(1000);
setMyLeds(0, 0, 1, 0, 0);
delay(1000);
setMyLeds(0, 0, 0, 1, 0);
delay(1000);
setMyLeds(0, 0, 0, 0, 1);
delay(1000);
setMyLeds(1, 1, 1, 1, 1);
delay(1000);
}
void setMyLeds (byte l1, byte l2, byte l3, byte l4, byte l5) {
digitalWrite(led1, l1 == 0 ? LOW : HIGH);
digitalWrite(led2, l2 == 0 ? LOW : HIGH);
digitalWrite(led3, l3 == 0 ? LOW : HIGH);
digitalWrite(led4, l4 == 0 ? LOW : HIGH);
digitalWrite(led5, l5 == 0 ? LOW : HIGH);
}