Basic For Loop Question I Cannot Solve Myself

Hi all,

I can do this by declaring each indiviual LED but I will be doing 10 different arrays of LEDs, each triggered progressively. However I have fallen at the first hurdle, I cannot get my for loop to run through my declared array (ARRAY) to show all of those specific LEDs.

Have tried a lot of variations but haven't managed so consulting the forum.

Many thanks in advance for any help
Ben

#include <FastLED.h>

#define LED_PIN  4

#define COLOR_ORDER GRB
#define CHIPSET     WS2811
#define cellColour 255,0,0
const int Array[] = {25,28,106,110,125,146,164,169,180,227}; //This is the array I want to show on first scan
#define BRIGHTNESS 64
const uint8_t kMatrixWidth = 32;
const uint8_t kMatrixHeight = 8;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);

void setup() {
  
  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setBrightness( BRIGHTNESS );
  
}

void loop() {
  
  for(int i=(ARRAY); i<NUM_LEDS; i++)
    {
  leds[i].setRGB(cellColour);  //basically this part here to run through the above array
  delay(100);  
  FastLED.setBrightness( BRIGHTNESS );
  FastLED.show();
    }

}

There are a number of things wrong with the sketch

Please describe exactly what you want to do. It looks like you have an array of LEDs. Is that correct ?

If you wrote

   for(int i=0; i<NUM_LEDS; i++)

your sketch would mostly make sense. But what do you intend to go to do with multiple arrays?

Also, that extra pixel for safety is unnecessary. If you use the library to write to the pixel array, it will simply not do so with LEDs beyond the end.

You can tell it to, but it won't.

Or you may mean to run through your array

for(int i=0; i<10; i++)

And use Array[i] as the pixel number.

a7

presumably Array is a list of pins or indices for individual LEDs

how are they associated with FastLED?

Array is a list of specific LEDs I would like to turn on in one go.

It's for a puzzle where 10 x RFID tags will be scanned with each one progressively showing more LEDs until a 4 digit code is shown.

Thanks, like this?

void loop() {
  
  for(int i=0; i<NUM_LEDS; i++)
    {
  leds[Array[i]].setRGB(cellColour); 
  delay(100);
  FastLED.setBrightness( BRIGHTNESS );
  FastLED.show();
    }

Got it, thanks all.

Yes, but you need to adjust the for loop limit to only be the size of the array you are stepping through, not NUM_LEDS.

I just counted the elements and used 10 in my examl,e above, there are better ways of automatically determining the number of elements in an array, so you don't need to count or worry when you add or remove elements.

When you are doing more like what you are experimenting now to prepare for, you may want to use 2 dimensional arrays instead of N 1 dimensional arrays.

If the numbers are small, and most of the groups are of similar size, there will be little waste just using a 2 dimensional array.

If the groups are larger and all over the place size wize, you may need to consider a more memory friendly data structure to hold that info.

We can help with all that.

a7

OK thanks will check 2 dimensional arrays and see if it works for this project. Not 100% sure how I will do it yet but will likely post on here if I get really stuck.

Many thanks for advice
Ben

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.