Making an array

I’m having trouble understanding arrays. I’ve read a lot of examples, posted on this forum…but still not able to translate what I want to my code.

I want to make make an array that contains addresses of different RGB LED’s. The goal is to I simultaneously turn the LED’s in addresses 13, 15, 199,200, 201, 2, 111 etc. on and green. Then call other array at different colors and times. Right now I’m just attempting to get the first array working. Any suggestions appreciated.

#include <FastLED.h>
#include <Adafruit_NeoPixel.h>

#define NUM_LEDS 248
#define CLOCK_PIN 5
#define DATA_PIN 4
CRGB leds[NUM_LEDS];
#define COLOR_ORDER BGR
#define BRIGHTNESS  20

int Array1[] = {13, 15, 199, 2, 111};
int i;                                              // counting variable

void setup()
{
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  LEDS.setBrightness(BRIGHTNESS);
}

void loop()
{

  for ( byte i = 0; i < 7; i++)
  { Array1[i] = CRGB::Green;                      //set the color(LED) red
    FastLED.show();
  }
}

}

You need to make more clear what you mean by "addresses" for the LEDs. Usually, each lead is connected to an I/O pin through a resistor. Setting that pin HIGH turns the LED on, setting it LOW turns it off. So, give you problem some thought, and try to explain what it is you want to do.

@econjack - he is using an APA102 strip with 248 leds.

The CRGB leds[NUM_LEDS]; is your representation in memory of your physical array of leds.

If you set leds[10] to green and leds[12] to blue, nothing is visible. That’s just an array of data in memory. to make the array of leds visible (that is for each physical LED, read the value in the leds array and send that info to your apa102 LED) then you call the function FastLED.show();

So if you want to turn to a special color only the led numbers that are in your Array1[] array you need to do something like this:

for(int index=0; index<5; index++) {// 5 is the number of element in Array1[], see comment
     leds[Array1[index]] = CRGB::Green; // set each led as identified from the Array1 to green
}
FastLED.show(); // once all values are set, refresh the full strip

A good coding practice would be to not use 5 in the for loop and hardcode the length of Array1[] but if the length is known and does not change and can be calculated at compile time then when you define the array do

int Array1[] = {13, 15, 199, 2, 111};
const unsigned int array1Count = sizeof(Array1)/sizeof(Array1[0]);// will be 5

Then instead of putting 5 in the for loop code later, you can use array1Count

if it changes all the time, then do that calculation in a non const variable just before entering the loop or use whatever variable you have that holds that length.

int Array1[] = {13, 15, 199, 2, 111};

Declares an array with 5 elements

 for ( byte i = 0; i < 7; i++)
  {
    Array1[i] = CRGB::Green;

Puts 7 entries in the 4 element array so it is writing to memory not allocated to the array.

Declares an array with 5 elements Puts 7 entries in the 4 element array

I see that OP isn't the only one that has trouble with array sizes... 8)

Whoops on my part.

Now, shall I edit my original post so that Paul's comment makes no sense because I know how much he likes that :)

Of course not, because I am more polite than that, so here is a more general explanation of the problem that avoids mentioning the exact size of the array or how many entries are being put into it.

The OP is putting more entries into the array using the for loop than has been allowed for in its declaration.

UKHeliBob: Whoops on my part.

Now, shall I edit my original post so that Paul's comment makes no sense because I know how much he likes that :)

Use strikethrough using the [s] tag to correct; might keep all parties happy ;)