Reading elements in crgb array

I would like to read which leds are included in CRGBSet leds1(leds(0, 6));

in other words i would like to see in cod if a specific led is in this array programmatically

I will scan through a multople arrays to se which strip contains a specific.

Thanks in advance

Welcome to the forum

Please post and example sketch, using code tags when you do, that illustrates what you are trying to do

Presumably the array is declared in the sketch then what problem do you have in determining whether a particular CRGB values is in that array ?

No need for additional code or scanning. You already know which strips contain which CRGBSet subsets because it's your code that created the CRGBSet subsets in the first place.

If that's not what you're asking, then you need to ask for what you want more clearly.

You can just go through the array and check each LED to see if it matches the one you’re looking for. That way, you can figure out which strip contains a specific LED. Have you tried scanning the other arrays the same way to see if it works consistently?

You are clearly referencing a piece of code, hopefully within a complete, functional sketch. Fine. Please either provide that complete, functional sketch here(to learn how to paste code, see the How to get the best... message), or, provide a link to the website page where you found the code you're asking about.
Thank you.

1 Like

leds1 (0, 6).fill_rainbow(0, 25); fills LED zero through six with rainbow effect.

Your instance of FastLED is leds
Your instance of CRGBSet is leds1 which contains from LED 0 through LED 6.
Your instance of CRGBSet is leds1 which contains six LEDs starting from LED 0.

For reference...

Docs... FastLED: CRGBSet

Example... FastLED_examples/CRGBSet_example.ino at master · marmilicious/FastLED_examples · GitHub

What way is that?

How?

So I have an array of arrays and want to see if the led (element) is in the strip I am scanning.
My code (in progress) is below but the bottom line is I don't know how to get the led numbers in the strip. I am a professional C# programmer and feel embarrassed I can't figure this out.


boolean arrayContains(CRGB* arrayName, int element, int arraySize) {
  
  //return true;
  
  for (int i = 0; i < arraySize; i++) {
    Serial.write(i);
    Serial.write(element);
    //Serial.write(arrayName[i]);
    Serial.write("----------");

    if ( arrayName[i]== element) {
      return true;  // Element found
    }
  }
  return false;  // Element not found after checking all elements
}

void fillTreeSolidHorizontal() {
  uint8_t NumColors = sizeof(CRGBColorArray) / sizeof(CRGBColorArray[0]);

  for (uint8_t colorIdx = 0; colorIdx < NumColors; colorIdx++) {
    for (uint8_t lrow = 0; lrow < 8; lrow++) {
      for (uint8_t x = 0; x < size_step_array; x++) {
         if (arrayContains(ledarray[x], 19, rowleds[x])){
          //ledarray[x][3] = CRGBColorArray[colorIdx];
          ledarray[x][lrow] = CRGBColorArray[colorIdx];
        
        //ledarray[x][1] = CRGBColorArray[colorIdx];
      }
    FastLED.show();
    delay(700);

    }


  }
}
}




CRGBSet leds1(leds(0, 6));
CRGBSet leds2(leds(9, 13));
CRGBSet leds3(leds(18, 2));
CRGBSet leds4(leds(27, 27));
//CRGBSet leds5(leds(35, 35));

struct CRGB* ledarray[] = { leds1, leds2, leds3, leds4 };

I flagged your post for moderation, as dual accounts are not permitted, AFAIK - and I realize, this may be inadvertent, but it needs cleaning up, at minimum.

About your question, I'm unclear why you think each LED should have a "number" - are you thinking an addressable LED somehow has an address embedded within the library? Because they don't, their 'addressability' relates only to their connection order in the string.
IF that's not what you're asking, then perhaps you need to restate your problem in a different way.

Address/color the first element in each sub-array with a different color.

I am not a fan of that method you are using. It is too intricate for me, and I lose control of it/them. I prefer to have all my leds in a flat array, and write to sub-sets of that flat array.

Here is an example of using subsets...

int section[] = { 13, 3, 7, 6, 9, 4, 7 };          // LEDs in segments of the lightning bolt

Contained here...

@anon72301209 ,

You appear to have created a new account to reply to this topic. Please use your original account @ski113 . You are allowed one account on the forum, creating duplicate accounts can lead to suspension from the forum.

Thank you.

Say what this means, or what you want it to mean

   if (arrayContains(ledarray[x], 19, rowleds[x])){

Like this checks to see if pixel 19 is in the xth set of pixels.

I don't notice rowleds defined anywhere.

I don't see how you are going to avoid setting up a data structure that records all your sets, here simple sets of contikulous pixel numbers.

If you had two arrays

byte start[] = {0, 9, 18, 27, 35,};
byte lenth[] = {6, 4,  2,  5,  5,};
const nSets = sizeof start / sizeof *start; // compiler counts!

or use an array of structs.

I'm sure you coukd write a loop over those and find a range that a given pixel number is in, or fail to find one becuase it isn't. Isn't part of any of the ranges.

Maintain or initialise start and lenth. I bet there's a C++ wizard who can improve this to mean you only had to write one description of the range(s) and the code woukd take care of the other uses the range needs to be put to.

a7

this is how CRGBSet is defined

(so PIXEL_TYPE is CRGB).

it is not legit. leds1 , leds2 , etc., are CRGBSet objects, not CRGB* pointers, so you cannot put them into struct CRGB* ledarray[] . The types are incompatible.


And even if you fix that, the arrayContains function assumes it can compare arrayName[i] == element to check if a given LED number exists in the array. This does not work because CRGBSet does not store LED numbers; indexing (operator[]) returns a reference to a CRGB color, not the LED number. So arrayContains cannot determine whether a specific LED number is present.

1 Like

Wild assumption, but are you trying to detect if the actual LED exists???

The declaration of the CRGBSet objects is also nonsense. It's trying to call the leds array like a function (eg ... leds(0, 6)). The correct syntax would be:
CRGBSet leds1(leds, 0, 6);

I don't know what the struct keyword is doing there either:

You can loop through the array using a simple for loop and compare each LED to the one you’re looking for. For example, if your array is leds1 and you want to check for targetLED, you iterate through the indices and check if leds1[i] == targetLED. If it matches, you know that the LED is in that strip. This works the same way for multiple arrays, loop through each one.

How do you read the state of a WS2812?

Is it the state of an LED you need to read, if so then save it in an array, or the number of the LED ?

Makes some sense, it would make better sense if you showed how to find the sets that include a given pixel number. Code or pseudocode.

In FastLED, you own the array, so indexing will give you the state (assuming it's been shown).

In Adafruit's Neopixel library, you can use the .getPixelColor() method.

In either case, it does not address the issue, the state of a given pixel does not enter into the search for the pixel's inclusion in sets.

a7

1 Like