1

I am working with FastLED on a Particle Photon in C++ and am trying to assign a new value to one of the elements of the pixel array.

Essentially, I have a array declared as such:

NSFastLED::CRGB leds[1];

I pass this into an "animation" class I've written in order to change the LED values:

void SomeClass::loop()
{
  // Get the pointer to the current animation from a vector
  Animation *currentAnim = animations.at(currentAnimation);
  currentAnim->animate(leds);

  ...
}

In the animation, I am trying to do something really simple-- set an element of that LED array to some value. For testing, even setting it to a static integer "0" would be fine.

void MyAnimation::animate(NSFastLED::CRGB *leds)
{
  for(int i = 0; i < numLeds; i++)
  {
    Serial.print(leds[i]); // "1"

    leds[i] = 0;

    Serial.print(leds[i]); // "1"
  }
}

The issue is, the array element is not being set at all. As you can see, this is even inside of the animation class that I am having the issue. I've also tried using (leds*)[i] = 0, but that doesn't have any effect either.

Why is it that the value is not being set in the array?

Joel Spolsky
  • 33,372
  • 17
  • 89
  • 105
Andrew M
  • 4,208
  • 11
  • 42
  • 67

1 Answers1

1

Your array data type is NSFastLED::CRGB, this holds RGB values, and can be assigned like below (from https://github.com/FastLED/FastLED/wiki/Pixel-reference )

If you just want to store a number you could use int instead of NSFastLED::CRGB.

// The three color channel values can be referred to as "red", "green", and "blue"...
  leds[i].red   = 50;
  leds[i].green = 100;
  leds[i].blue  = 150;

  // ...or, using the shorter synonyms "r", "g", and "b"...
  leds[i].r = 50;
  leds[i].g = 100;
  leds[i].b = 150;


      // ...or as members of a three-element array:
      leds[i][0] = 50;  // red
      leds[i][1] = 100; // green
      leds[i][2] = 150; // blue
Malcolm McCaffery
  • 2,468
  • 1
  • 22
  • 43
  • I should point out that even assigning a CRGB value doesn't work (either using a built in color in FastLED, such as CRGB::Red), and modifying the r/g/b values also seems to do nothing at all. – Andrew M Oct 29 '15 at 02:06
  • If you just use a declaration like int leds[1]; – Malcolm McCaffery Oct 29 '15 at 02:07
  • So if you set leds[0].r = 1 then print the value of leds[0].r it is not 1? – Malcolm McCaffery Oct 29 '15 at 02:10
  • Do we know the value is not right, or the print is not working? Can you see a breakpoint + watch point to check if the value is actually changing in memory – Malcolm McCaffery Oct 29 '15 at 02:14
  • What is value of numLeds – Malcolm McCaffery Oct 29 '15 at 02:15
  • numLeds is simply 1, since there's only 1 element in the array. I don't have the ability to debug this properly because it's running on a Particle Photon, but I did add an if statement to check the value-- it seems to be the print that is faulty and displaying 0. The "if" fires when I check for the number I'm assigning. – Andrew M Oct 29 '15 at 02:19
  • The site has more examples like this:Serial.readBytes( (char*)(&leds[i]), 3); https://github.com/FastLED/FastLED/wiki/Controlling-leds – Malcolm McCaffery Oct 29 '15 at 02:20
  • Thanks, I'll take a look. Now the issue seems to be that the original array (the one in the loop method) isn't being changed... Not sure if I'm using arrays/pointers correctly to change the original array since I'm not a C++ dev. – Andrew M Oct 29 '15 at 02:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93635/discussion-between-malcolm-mccaffery-and-andrew-m). – Malcolm McCaffery Oct 29 '15 at 02:40