Go Down

Topic: "call/select" multiples LEDs (Read 506 times) previous topic - next topic

jmatos448

Dec 17, 2017, 11:20 pm Last Edit: Dec 17, 2017, 11:44 pm by jmatos448
Hii,

I've got something like this
-----------------------------------

int led0 = 2;
int led1 = 3;
int led2 = 4;

void setup()

   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);

   digitalWrite(led0, HIGH);
   digitalWrite(led1, HIGH);
   digitalWrite(led2, HIGH);
-------------------------------------

my question is, how can i get something like this??


   digitalWrite(led0, led1, led2, HIGH);

instead of go line by line, set the 3 led in a sngle line.






Grumpy_Mike

Quote
my question is, how can i get something like this??

digitalWrite(led0, led1, led2, HIGH);
Code: [Select]

digitalWrite(led0,HIGH);
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);

dougp

I suspect what you want is a for loop.  Not one line but close.  You'll also need to read up on arrays.  An array is where you store the pin numbers that the for loop will reference to do things like setting pinModes.

The IDE has examples at - File/examples/5.control.
So two neutrinos went into a bar.  Nothing happened.  They were just passing through.

TonyWilk

my question is, how can i get something like this??


   digitalWrite(led0, led1, led2, HIGH);
Do you mean you want a function like:


Code: [Select]
void digitalWrite3( int a, int b, int c, int hilo )
{
 digitalWrite( a, hilo );
 digitalWrite( b, hilo );
 digitalWrite( c, hilo );
}

//then in your setup(), you can call that function like:

digitalWrite3( led1, led2, led3, HIGH );


Yours,
  TonyWilk

septillion

Fixed it:
Code: [Select]
void digitalWrite3(const byte a, const byte b, const byte c, const bool hilo )
{
 digitalWrite( a, hilo );
 digitalWrite( b, hilo );
 digitalWrite( c, hilo );
}

//then in your setup(), you can call that function like:

digitalWrite3( led1, led2, led3, HIGH );

Pin numbers are byte/uint8_t, not int. For dark reasons the value is a uint8_t as well but you just want to use HIGH/LOW / true/false so a bool will do.

But in reality, using arrays is the right way. The moment you start numbering stuff => arrays make it a lot easier.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

TonyWilk

#5
Dec 19, 2017, 06:31 pm Last Edit: Dec 19, 2017, 06:59 pm by TonyWilk
I think that until the OP (hello?) comes back with more info, it's hard to say what he wants.

Loop? Array? - that's making a bit of an assumption.

Based on just what he asked, he could be asking how to write a variadic function...

Setting a random selection of LEDs by doing: setLeds( HIGH, led1 ) or setLeds( LOW, led1, led2, led4 ) might be handy. (*)

Yours,
  TonyWilk

(*) but interesting if you're mixing int's and bytes for pin numbers, compiler's not going to coerce that for you - could just write multiple functions I suppose, messy tho'.


darkgiuseppe

I am more interested on why the OP wants to do this. Maybe we can steer them to a different solution based on why they want this one line feature.
Need parts? Check out my new store! http://centipedestudio.com
Happy making!

Go Up