Turning a section of adressable RGB LED's on/off

I'm attempting to turn on multiple LED’s in my strip on a the same time and color. For example, I wanted to turn on LED’s numbered 55,56,57,125,126,127,128,200,201,202 on at the same time and color, however the following code will only allow me to turn on the last numbered LED and not all of them simultaneously. I also want my code to be condensed as I have 248 LED's and don't want to line to turn each section of LED on, so I'm attempting to turn each section and then off. Any help much appreciated.

#include <FastLED.h>

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


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


void loop() {

leds[55,56,57,125,126,127,128,200,201,202] = CRGB::Blue;
            FastLED.show();
               // clear this led for the next time around the loop
            leds[55,56,57,125,126,127,128,200,201,202] = CRGB::Black;
            delay(30);
}
leds[55,56,57,125,126,127,128,200,201,202] = CRGB::Blue;

No, no, no, you can't do what you want like that!

I'll show you how later when I can get to my PC.

Need an array, for loop, set all the leds, then a single "show" command.

I think my problem is that I don't understand how to write an array. My worry was that with a for loop it would increment through each LED's in the array one at a time, where as I want to turn the entire array on at once. However, I'm thinking that the clock speed at 12MHz is fast enough that it will appear as if the entire array turns on at once.

I've looked at every array example I can find and have a general sense of how to code it but I still get errors every time I try it. I'm going to keep trying to code the array but if anyone has any examples or places to look I'd appreciate it.

"My worry was that with a for loop it would increment through each LED's in the array one at a time, where as I want to turn the entire array on at once."

You won't see it. The way the library works is that you're setting up values for the LEDs, and only when you call the FastLED.show(); function do they get shown. This is basically what you were trying to do with your failed attempt. It is no different when you do it correctly. You will only ever be able to do 1 instruction at a time. Don't distract yourself with thoughts about clock speed, none of that matters at your level of understanding.

Thanks everyone for the help! Much appreciated.

I made some progress with your input but still have a little ways to go. Right now my code can light up a section of the LED strip defined by the values in my for loop. So for "i = 50; i < 65" it lights up the LED's from 50 to 65. While I could write a for loop for each section I want to light up, I want to light up a lot of different sections and that would be a ton of for loops and inefficient coding.

I was hoping I could declare which LED address I want to light up in my array. In the below code I wanted to light the LED's at addresses "5, 6, 44, 45, 121, 122, 144, 155". However, my i value controls which section of LED's light up and the "addresses" in don't impact which LED's light up. Even if I comment out my LED "addresses" the code runs the same. Is there a way I could write a series of OR statements in my for loop to turn on certain lots of different sections at the same time. I was thinking something like this, but couldn't get it to work:

(i = 6; i < 10; i++) || (i =15; i < 25; i++) but

How would I specify in my array the specific LED addresses to light up? Or should I be doing it a different way? Thanks again.

-Pete

#include <FastLED.h>

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

int array [8] = {5, 6, 44, 45, 121, 122, 144, 155}; //array with the LED adresses to light up
int i;                                              // counting variable

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

void loop() {


  for (i = 50; i < 65; i++)                     //loop through the array
  {
    leds[i] = CRGB::Red;                       //set the color(LED) red
    FastLED.show();
  }

}

For loops increment numerically.
If your items are not in any regular numerical order, you need an array so that each item in the array is referenced to by its position in the array, which is then in a numerical order a for loop can increment nicely.

int namedArray[] = {13, 15, 199, 2, 111}
...
for ( byte i = 0; i < 5; i++)
namedArray*;*
"While I could write a for loop for each section I want to light up, I want to light up a lot of different sections and that would be a ton of for loops and inefficient coding. "
Didn't realize you knew enough about coding to declare the equivalency of tediousness and inefficiency. I don't think it works that way, though, claiming something as inefficient just because it's a lot of work to you.

I have no problem doing the work, I've been searching and learning about arrays all day. I don't mind sitting down to code for 10 hours or a couple weeks...as long as it takes. I hope I didn't rub you the wrong way, my determination to do work far outweighs my other abilities...definitely coding:)

What I should've explained better was that I would like to to code in an efficient way so that I can build upon my knowledge as I know there is a better way then lots of for loops. I'm also using an Arduino nano which has a limited about of storage space. Currently I have 5 for loops and without anything else it takes up ~15% of my storage space. To run the sequences I want I'd need ~30 for loops. I may switch to an Uno if needed. I will try what you suggested above, I very much appreciate the suggestions and all of the advice!

Arrays still aren't clicking with me :o My codes not giving me anything. Will continue to work on it through the night.

Any ideas what I'm doing wrong. I'm trying to get one array working. To sum it up:
To simultaneously turn the LED's green on at addresses 13, 15, 199, 2, 111.

#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 have 5 elements in the array

for ( byte i = 0; i < 7; i++) //Why are you counting 6?
{ Array1 = CRGB::Green; //you're not even using the "leds" function to set anything. this is just meaningless. rtfm.

  • FastLED.show(); //You don't want to do this "show" after each LED is set. Move it . . . .*
  • }*
    <down to here, out of the for loop>
    Then you need instructions to turn them off.
    Go read the guide for the library.
    Learn how to set 1 led the color you want. Then all you're doing is replacing that LED's address with the array instead in a for loop.

I realize this must be easy for you and I get what you're saying, but I can't get it to work. I've been going over the fast library manual all day and I've done every example I have found. I can get one LED or a section of LED's to blink any color I want, or have the LED passed along the the strip over and over, but cannot get my for loop to read in the specific addresses in my array. I guess my coding abilities need some work.

I'm close, it's just getting the for loop to read in the addresses from my array instead of incrementing by one. I can figure out how to turn them off if I can figure out how to turn them on :wink:

#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 [ ] = {13, 15, 199, 2, 111};      //address of LED's to be passed into the for loop
int i;

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

void loop() {

  for (i = 0; i < 5; i++)  //To me this says, turn every led on at led[i address]
  {
    leds[i] = CRGB::Red;
    FastLED.show();
  }

}

Since you have

CRGB leds[NUM_LEDS];

already creating an array of all of your LEDs, which is how they're getting addressed (0 is first led, 1 is second, 2 is third, etc)
and when you change a single LED with

leds[33] = CRGB::Red;

you are saying "in the array that is named 'leds', set the 34th LED to red". It won't light up red until you give a

FastLED.show();

command.

So what you are doing to prepare a group of LEDs before doing a FastLED.show(); is basically

leds[33] = CRGB::Red;
leds[21] = CRGB::Red;
leds[86] = CRGB::Red;
leds[42] = CRGB::Red;
leds[74] = CRGB::Red;
FastLED.show();

Which is fine. Maybe that's what you have to do since your groups are only 5 elements. A for loop will help if your group is much larger. What you are struggling to grasp is that you're replacing just those numbers (33, 21, 86, etc) with a single variable, "i", for example. But "i" needs to be 5 different values, so you use an array of those 5 values so that when "i" is 0, the returned value is the first, "33". When "i" is 1, returned value is 21, etc.
"i" increments by one. That doesn't change. What you're changing is the order of the returned values which are not numerically ordered.

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

That array has no name, give it one like

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

Then you can use

for (i = 0; i < 5; i++) 
  {
    leds[myLEDs[i] ] = CRGB::Red;
}
    FastLED.show();

Note you set all the values THEN you show them, you don't set and show individual changes.

The number returned by the myLEDs[ i ] array is then being used to address an array element in the leds array.

Thanks everyone for the help!!! It is very much appreciated. I was able to get it working thanks to all of your help. If possible, I still have a few questions that have come up.

  1. Right now I want to change the color of the LED every time the program loops through. I set the variable x to change colors based on how many loops the program does, and I can see that the loop is increasing because I used the serial.print command. However, the LED colors always stay Gold and never turn to green.

  2. I was also wondering if there is a command that sets all of LEDs black, so that I don’t have to call another for LED array that's already turned on. Not a big deal either way, just curious.

#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


//THESE ARE MY LED ARRAYS
int myLEDs0[ ] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
int myLEDs1[ ] = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47};
int myLEDs2[ ] = {48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59};
int myLEDs3[ ] = {60, 61, 62, 63, 64, 65, 66, 67, 68, 83, 84, 85, 86, 86, 87, 88};
int myLEDs4[ ] = {69, 70, 77, 78, 79, 80, 81, 82, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98};
int myLEDs5[] = {71, 72, 73, 74, 75, 76, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 124, 125, 126, 127};
int myLEDs6[] = {309, 310, 311, 130, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 128, 129, 130, 131, 132};
int myLEDS7[] = {135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153};
int myLEDS8[] = {154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 191, 192, 193, 194, 195, 196, 197, 198};
int myLEDS9[] = {164, 165, 166, 167, 168, 169, 182, 183, 184, 185, 186, 187, 188, 189};
int myLEDS10[] = {170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181 };
int myLEDS11[] = {199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224};
int myLEDS12[] = {223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246};
int i;                                              // counting variable
int x;
int loop1 = 0, loop2 = 0;

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

void loop()
{
  
[b]  loop1++;                                   //INCREASE THE LOOP BY ONE
    Serial.print(loop1);  
  if (loop1 < 2)                              //IF THE LOOP IS LESS THEN TWO, SET THE COLOR TO GOLD
  {
    x = CRGB::Gold;            
  }

  if (loop2 >= 2)                          //IF THE LOOP IS GREATER THEN OR EQUAL TO 2, COLOR=GREEN
  {
    x = CRGB::Green;
  }[/b]

  for (i = 0; i < 24; i++)
  {
    leds[myLEDs0[i] ] = x;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 24; i++)
  {
    leds[myLEDs0[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);
  //

  for (i = 0; i < 24; i++)
  {
    leds[myLEDs1[i] ] = x;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 24; i++)
  {
    leds[myLEDs1[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  //

  for (i = 0; i < 12; i++)
  {
    leds[myLEDs2[i] ] = x;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 12; i++)
  {
    leds[myLEDs2[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);
  //

  for (i = 0; i < 16; i++)
  {
    leds[myLEDs3[i] ] = x;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 16; i++)
  {
    leds[myLEDs3[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  //

  for (i = 0; i < 18; i++)
  {
    leds[myLEDs4[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 18; i++)
  {
    leds[myLEDs4[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  //

  for (i = 0; i < 20; i++)
  {
    leds[myLEDs5[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 20; i++)
  {
    leds[myLEDs5[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  ////////////////////////////////////////////
  for (i = 0; i < 24; i++)
  {
    leds[myLEDs6[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 24; i++)
  {
    leds[myLEDs6[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  //
  for (i = 0; i < 19; i++)
  {
    leds[myLEDS7[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 19; i++)
  {
    leds[myLEDS7[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  //

  for (i = 0; i < 18; i++)
  {
    leds[myLEDS8[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 18; i++)
  {
    leds[myLEDS8[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);
  //

  for (i = 0; i < 14; i++)
  {
    leds[myLEDS9[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 14; i++)
  {
    leds[myLEDS9[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);
  ///////////////////////////////////////////////

  for (i = 0; i < 12; i++)
  {
    leds[myLEDS10[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 12; i++)
  {
    leds[myLEDS10[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

  ////////////////////////////////////////////////////

  for (i = 0; i < 24; i++)
  {
    leds[myLEDS11[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 24; i++)
  {
    leds[myLEDS11[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);
  //

  for (i = 0; i < 24; i++)
  {
    leds[myLEDS12[i] ] = CRGB::Gold;
  }
  FastLED.show();
  delay (10);
  for (i = 0; i < 24; i++)
  {
    leds[myLEDS12[i] ] = CRGB::Black;
  }
  FastLED.show();
  delay (10);

}
 x = CRGB::Gold;

You have declared x to be an int, that means it only has 16 bits. This is way fewer bits than a CRGB variable type which is 32 bits long. So x should be a long int.

You code goes on to set all LEDs to gold and then 10mS later it proceeds to set them all to black. You you think this will be at all noticeable?
You then do the same thing in a most turgid way, if you learned how to use arrays all that could be reduced to just 3 extra lines instead of the dozens you have.

I was also wondering if there is a command that sets all of LEDs black, so that I don't have to call another for LED array that's already turned on.

No big deal, just write a function to set all the LEDs to black and call that function whenever you need it.

There is a
FastLED.clear ();
to wipe all LEDs instead of doing the 'black'. Should've seen that if you read the guide.

Thank-you, I was able to change my x value and it now changes colors! :slight_smile: You guys/gals are the best!

Grumpy_Mike:

 x = CRGB::Gold;

You have declared x to be an int, that means it only has 16 bits. This is way fewer bits than a CRGB variable type which is 32 bits long. So x should be a long int.

You code goes on to set all LEDs to gold and then 10mS later it proceeds to set them all to black. You you think this will be at all noticeable?
You then do the same thing in a most turgid way, if you learned how to use arrays all that could be reduced to just 3 extra lines instead of the dozens you have.
No big deal, just write a function to set all the LEDs to black and call that function whenever you need it.

I don't doubt you but I'm curious how you would suggest I reduce my code. I was thinking of making an array of all my arrays. That way I can have one for loop that will go through all of the the arrays. I was also thinking of making an array of colors but will do more reading on arrays. Is that along the lines of what you'd suggest?

I've tested out different delays from 1 second to 10ms and I like the 10ms. It's really "burns" through the array. I might slow it down to 30ms but can always change that later on.

INTP thank-you for the advice. Maybe I’ve been looking at the wrong Fast Led Guide? I’ve been using these:

but I'm curious how you would suggest I reduce my code.

Basically when ever you find yourself writing the same thing over and over you are doing it wrong. Just write it once in a loop. Yes there will be some magic numbers that differ each time you write it, so you put those numbers into an array and call it up with the incrementing loop index.

Look at this tutorial:-
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

In your case that code that outputs an array to your LED buffer and plods through each array one at a time can be made into an outer loop and only do the transfer code once.