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.

Joshua Dance
  • 141
  • 1
  • 6
  • 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 '19 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 '19 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 '19 at 19:49

2 Answers2

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

tony gil
  • 362
  • 1
  • 7
  • 26
  • 1
    Wonderful answer. Way better than what I found. Thanks! – Joshua Dance Jul 24 '19 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 '19 at 01:39
  • Exception 9 on Uno? – Juraj Aug 03 '19 at 09:58
  • why the struct? it is useless and will be copied as function parameter. you can pass the array as parameter – Juraj Aug 03 '19 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 03 '19 at 12:37
  • i find your spamming rude and abusive in abstract and have flagged you for it, @Juraj. I am not hard of reading, there is no point in sequentially writing the same rude comment. – tony gil Aug 03 '19 at 12:41
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;
}
Joshua Dance
  • 141
  • 1
  • 6