Go Down

Topic: Multiple LED's using an Array (Read 126 times) previous topic - next topic

agrady1995

hello! I am trying to turn on/off multiple LED's using an array function.
Code: [Select]
      const byte ledPins[] = {0, 1, 2, 5, 6, 7, 8, 9, 10, 11, 12, A0, A1, A2, A3, A4, A5};
  int pinCount = 17;

  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    pinMode(ledPins[thisPin], OUTPUT);

  }
    //lights
    digitalWrite(ledPins[5,6,7],HIGH);
    delay(500);
    digitalWrite(ledPins[5,6,7],LOW);
    delay(500);
 
}


This just gets the last pin (pin 9, or "ledPins[7]") to flash. Am I supposed to use an & or something? I am going to end up building a sequence with 17 LED's and really wanted to be able to turn multiple on and off like this...

TomGeorge

#1
Nov 15, 2017, 08:46 am Last Edit: Nov 15, 2017, 09:04 am by TomGeorge
Hi,
What controller are you using?
It looks like you are using pin0 and pin1, they are the programming pins for the controller, which will cause erratic operation and make your controller hard to program.

What will you be using for a power suppply?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

PieterP

Code: [Select]
ledPins[5,6,7]
That syntax is invalid.
You could use:
Code: [Select]
for(uint8_t i = 5; i <= 7; i++)
  digitalWrite (ledPins[i], HIGH);


Pieter

agrady1995

Quote
That syntax is invalid.
You could use:

Code: [Select]
for(uint8_t i = 5; i <= 7; i++)
  digitalWrite (ledPins[i], HIGH);



Pieter
MY HERO XOXOXOXO

Delta_G

You already did it on the pinMode part.  How did you figure it out there and not for the digitalWrite part?
If at first you don't succeed, up - home - sudo - enter.

PaulRB

Quote
ledPins[5,6,7]

That syntax is invalid
It is not invalid. It compiles. It runs and does something. Just not what the OP wanted. As the OP discovered, only the final led number is actioned. The reason is that the comma is actually an "operator" in C/C++. The expression "a, b" means "evaluate a but discard the result, then evaluate b and use that result". It may seem a little stupid that the compiler would allow that, but remember that even though the result a is discard, evaluating could have some useful side-effect. For example if you said "a++, b" then b would be used, but a would still have been incremented: next time you use a, you would see that a was one more than last time you used it.

Having said all that, I am not suggesting that anyone actually uses this technique. It's confusing and does not make your code easy to read or understand!

Go Up