2

Using the FastLED library, I want to light up multiple lights at once.

Currently I am passing in just one light to the function.

spell( int LEDNumber )
{
  FastLED.clear();
  leds[LEDNumber] = CRGB (255,255,255); //white
  FastLED.show();
  time = 500;
  delay(t);
}

I would like to pass in more than just 1 light at a time.

3
  • 2
    This is really a basic C/C++ programming question, not an Arduino question. In any case, I'm glad you were able to figure it out.
    – Duncan C
    Jul 20, 2019 at 21:50
  • 1
    @DuncanC There is indeed a sort of no-man's land there, since Arduino does restrict how you can use C++, especially vis-a-vis memory restrictions. I am not sure that I would be so strict with Joshua, or anyone for the matter.
    – tony gil
    Jul 22, 2019 at 19:12
  • 1
    Tony, yes, there are some gray areas, but this is a programming 101 question for C/C++.
    – Duncan C
    Jul 22, 2019 at 19:49

2 Answers 2

2

You may create a struct with "x" elements, one for each LED:

struct ArrayOfBooleans {
   bool array[4];
};

ArrayOfBooleans myOutputArray;
myOutputArray.array[0] = true;
myOutputArray.array[1] = true;
myOutputArray.array[2] = false;
myOutputArray.array[3] = true;

lightEmUp(myOutputArray);

void lightEmUp(ArrayOfBooleans myOutputArray) {
   bool isFirstLightOn = myOutputArray.array[0];
   // etc
}

NoTE: If you run into Exception 9 issues (using this logic on another board), please check this answer to another post

6
  • 1
    Wonderful answer. Way better than what I found. Thanks! Jul 24, 2019 at 16:10
  • 1
    I just recently ran into this issue and this was the solution that i implemented. keeps things cleaner, as well as making upgrading (adding or removing parameters) easier.. tks for accepting.
    – tony gil
    Jul 25, 2019 at 1:39
  • Exception 9 on Uno?
    – Juraj
    Aug 3, 2019 at 9:58
  • why the struct? it is useless and will be copied as function parameter. you can pass the array as parameter
    – Juraj
    Aug 3, 2019 at 10:05
  • you could do that, but a struct may have other data as well - such as multiple arrays for multiple dimensions of parameters. such as other parameters. a struct as a class is far from useless, especially if your development is method oriented.
    – tony gil
    Aug 3, 2019 at 12:37
1

Easier than I thought. Found it while reading the docs. Imagine that. :)

To pass in multiple variables, just separate them with a comma.

Docs: https://www.arduino.cc/en/Reference/FunctionDeclaration

Relevant code:

int myMultiplyFunction(int x, int y){
  int result;
  result = x * y;
  return result;
}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.