Brightening individual LEDs using FastLED library

Hello!

I have Arduino Micro and a LED strip that I am controlling using FastLED library. I am currently setting colors in RGB, although I am considering switching to HSV. However, that would require writing my whole code again in HSV so I am trying to find a solution in RGB first.

My problem is this: I don't know how to brighten aka increase brightness of an individual LED in the strip. I am trying to go through the LEDS and increase their brightness one by one and then dim the LEDs in reversed order.

First I set the brightness of all LEDs to 64 as below, which works fine.

FastLED.setBrightness(64);

Then, I have tried to do as was suggested here Brightening equivalent of nscale8 · Issue #238 · FastLED/FastLED · GitHub (below)

x=128;
leds[i].maximizeBrightness(x);

or as suggested here https://www.reddit.com/r/arduino/comments/9kk6gj/programming_for_neopixel/ (below)

x = 128;
leds[i].maximizeBrightness();
leds[i] %= x;

but both of the above just seem to dim the LEDs. First they are certain brightness, then (when they're supposed to brighten) they dim somewhat and then, when they're supposed to dim, they dim even more...

Using

x = 128;
leds[i] += x;

doesn't do anything.

Any tips and suggestions would be appreciated. I have read multiple discussion of the same topic and googled my ass off but none of the suggestions work.

Update:

I tried the following.

  for (int i=0; i<_num_of_leds; i++)
  {
    _leds[i].maximizeBrightness();
    _leds[].fadeLightBy(128);
    FastLED.show();
  }
  for (int i=0; i<_num_of_leds; i++)
  {
    _leds[i].maximizeBrightness();
    _leds[].fadeLightBy(64);
    FastLED.show();
  }

During the first for loop the LEDs dimmed and during the second they brightened back to the original (as far as my eye can tell, at least). That's the opposite of what I wanted to happen! Still progress I suppose... Any explanations for this? :'D

Reading your code, I would expect it to do what you describe. Can you explain why you think the opposite should happen? Clearly there's some concept you are misunderstanding, but I'm not sure what that is.

You should only use .show() after all LEDs have been updated as desired, so after the for loop, not inside it.

Yes, it's totally possible that I am misunderstanding something... Here's how I thought the code would work:

The initial brightness level is 64 of 256. Then in the first loop maximizeBrightness() increases the brightness to 256/256. Then fadeLightBy(128) fades the light to 128/256 = 50% of its previous value which is 0,5*256=128. That is why I think it should grow brighter and not dimmer.

The in the second loop maximizeBrightness() increases the brightness again to 256/256 and fadeLightBy(64) should dim the led to 25% of 256 which is 64.

The reason why the .show() is inside the for loop is because I am actually using I2C messaging to control the leds. So the real (still simplified) structure is:

for()
{
    Master sending message to slave.
}

slave_receiving_message()
{
    _leds[i].maximizeBrightness();
    _leds[].fadeLightBy(128);
    FastLED.show();
}

I just presented only the for loops because the messaging works fine, I wanted to present a simpler code and I am not at liberty to share the full program. But I'll keep your advice on mind for future reference! It is quite possible that I would do that kind of a thing in a hurry :smiley:

So the LEDs were set to a lower level of brightness by part of the code that you didn't share, and you were asking why the part of the code you did share was making it brighter? I hope you can understand how I feel, now, about the time and trouble I took to try to answer your question.

I don't understand how your explanation of the i2c comms has any relavence to my suggestion about the use of .show().

I am not at liberty to share the full program.

So you write a small program that illustrates your problem that we can try for ourselves.

mjaiaa:
I am not at liberty to share the full program.

Huawei might steal it to use in their systems! :astonished:

PaulRB:
So the LEDs were set to a lower level of brightness by part of the code that you didn't share, and you were asking why the part of the code you did share was making it brighter?

I said the initial brightness already in the first post:

mjaiaa:
First I set the brightness of all LEDs to 64 as below, which works fine.

Sorry you wasted so much of your time because of not reading properly, Paul B... I usually hate these kind of forums and you set a perfect example of why.

Now, is there anyone who could help me with my actual issue? The one which is summarized below.

mjaiaa:
The initial brightness level is 64 of 256. Then in the first loop maximizeBrightness() increases the brightness to 256/256. Then fadeLightBy(128) fades the light to 128/256 = 50% of its previous value which is 0,5*256=128. That is why I think it should grow brighter and not dimmer.

mjaiaa:
Paul B... I usually hate these kind of forums and you set a perfect example of why.

Glad to be of service! :grinning: :grinning: :grinning:

Now, is there anyone who could help me with my actual issue?

Only in general terms because you refuse to post any workable code.

To change the brightness you add or subtract a constant to all three of the RGB components.
Or if working with HSV values you reduce the value of S from its default value of 1.

First I set the brightness of all LEDs to 64 as below, which works fine.

The set brightness is global, it applies to all the LEDs. Don’t use it if you want individual control of each led.

I eventually solved my problem and now I have time to explain it here. Hopefully this helps someone else too.

As already said, to change the brightness of a LED the RGB values need to be added/subtracted. maximizeBrightness(x) does that in a way that won’t change the ratio between RGB values i.e. the colour of the light. As a parameter maximizeBrightness(x) takes a value between 0-255, which is used together with the highest one of the RGB values to calculate the new RGB values. The given parameter x will be the new value of the highest of the RGB values and the other two are calculated accordingly. (This was for me the hardest thing to understand). For more in depth explanation and math, you should check the source code (pixeltypes.h).
The default value for x is 255, so if you call maximizeBrightness() without any parameter, it will literally maximize the brightness.

For example:

  • A LED1 is green with value (0,128,0). When led1.maximizeBrightness(200) is called, the new value will be (0,200,0).
  • Another LED, a LED2 is purple with value (160,40,150). When led2.maximizeBrightness(70) is called, the new value will be (70,17,65).
  • Note: if the original colour is such that one or more of the RGB values is already 256, e.g. orange (256,150,45), you can’t brighten the LED anymore with maximizeBrightness().

Now, about setBrightness(). I knew that it affects the whole LED strip. What I didn’t know is that it affects a variable called m_Scale in CFastLED class. m_Scale controls the power consumption of the LED strip which appears as an increase/decrease of brightness to the eye. So yes, setBrightness() and maximizeBrightness() both have an effect on the appearing brightness, but in the code they have nothing to do with each other.

mjaiaa:
about setBrightness(). I knew that it affects the whole LED strip. What I didn't know is that it affects a variable called m_Scale in CFastLED class. m_Scale controls the power consumption of the LED strip which appears as an increase/decrease of brightness to the eye.

So knowing that, going back to your first post:

mjaiaa:
First I set the brightness of all LEDs to 64 as below, which works fine.

FastLED.setBrightness(64);

was that line of code, in fact, setting the initial brightness, or were other lines of code doing that?