Fading command not working ?

I am using this basic code to try to get a LED to fade.

But its not working. Any suggestion as to where to put the fading command ?

Thank you!

void loop() {

for (int dot = 0; dot<NUM_LEDS; dot++) {
  leds[dot] = CRGB::Red;
  leds[dot].fadeToBlackBy(8);
  FastLED.show();
  //clear this LED for next time:
  leds[dot].fadeToBlackBy(50);
  leds[dot]= CRGB::Black;
  delay(1770);
}
}

But its not working.

It is working exactly like you wrote it.

You set an LED to a colour, then fade it, which means reduce the brightness of the colour by a bit and then show the LED. So the colour you see will always be the same.

If you want it to fade then set the colour, show it, then have a loop where you just fade it and show it repeatedly. As a loop happens very quickly you might want to add a small delay so you can see it fade.

I can't believe that code even compiles.

Most of it is missing! :astonished:

I added some additional delays yet I don’t see any fading tho. I understand that the code is doing what I programmed it to do, but my expectations don’t match the output obviously.

I am thinking that I need to create a loop that will progressively change the value of V (from HSV) and I can control the rate of fade as well.
Would this make more sense to fade a LED down to black?

#include <Adafruit_NeoPixel.h>
#include <FastLED.h>

#define DATA_PIN 6
#define LAMPS 30
#define NUM_LEDS 30
CRGB leds [NUM_LEDS];

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LAMPS, DATA_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN> (leds, NUM_LEDS);
  strip.begin();
  strip.setBrightness(80);
  strip.show();               
}

void loop() {

for (int dot = 0; dot<NUM_LEDS; dot++) {
  leds[dot] = CRGB::Blue;
  leds[dot].fadeToBlackBy(200);
  delay(400);
  FastLED.show();
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(1770);
}
}

ahhh! I need to add a FastLED.show command in there too!

Yes, as well as removing the

leds[dot] = CRGB::Blue;

From that for loop.
Have another for loop just before this and set all your LEDs to blue, then have another for loop following it that has fade delay and show in it.

Not a good idea to mix FastLED and Adafruit Neopixel in the same sketch, not sure if there are any conflicts between them, but it is wasteful of memory having to define two LED arrays for the same strip.

Are you wanting to set the entire strip to the same color, then dim the entire strip, or do you want to do this with individual pixels?

Grumpy_Mike: Yes, as well as removing the

leds[dot] = CRGB::Blue;

From that for loop. Have another for loop just before this and set all your LEDs to blue, then have another for loop following it that has fade delay and show in it.

No need for a loop, FastLED has a command for doing just that:

fill_solid(leds, NUM_LEDS, CRGB::Blue); //set entire LED strip to Blue

FastLED can also fade the entire strip at once:

    fadeToBlackBy(leds, NUM_LEDS, x); //fade LED by x/256 of its present level

You will need to experiment with the fading value and how many times you have to apply it to get all the way to black, since it fades by a percentage of the current level of the pixel.

since it fades by a percentage of the current level of the pixel.

If it did this correctly it would never fade to black would it?

Fortunately it doesn't and only works to an integer approximation.

No need for a loop, FastLED has a command for doing just that:

I was trying to keep as close as possible to the OPs original code.

Yes I had wanted to fade to black. I’ll set my color to a specific value and then I can organize how many times to do the fade loop. I forgot that I need to add the FastLED.show command. I’ll add that within the fade loop.

You would normally want to fade all the LEDs and then show, and then delay, if you want the whole thing to look like it is fading evenly.

At each level of fading, I’d have to have the command FastLED.show(), for each level of fading so it would show a gradual fade ?

I got it to work how I want (well, mostly).

I just have to fade it from black up to the color I want, but it fades from bright to black.
I just added a fadeToBlackBy loop so it fades within the loop.

oid loop() {

for (int dot = 0; dot<NUM_LEDS; dot++) {
  leds[dot] = CRGB::Yellow;
  FastLED.show();
  for(int fadeNum = 0; fadeNum<120; fadeNum++) {
  leds[dot].fadeToBlackBy(5);
  FastLED.show();
  delay(60);
  }
  delay(400);
  FastLED.show();
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(1770);
}
}

Shanedm1: At each level of fading, I’d have to have the command FastLED.show(), for each level of fading so it would show a gradual fade ?

Yes, I was assuming you were going to fade like I said and fade all the LEDs at once not just one at a time.

I just have to fade it from black up to the color I want, but it fades from bright to black.

So to go the other way you have to specify the colour of the LED in a different way. Currently you use CRGB::Yellow which is not very flexible and only sets a colour to the maximum. There are lots of ways of specifying a LED's colour. See the documentation at https://github.com/FastLED/FastLED/wiki/Pixel-reference and also other pages on that wiki to see the full range of controls you have. Possibly the best way is to use the leds[ i ].setRGB( 50, 100, 150 ) method.

To fade up you have to have a target value of RGB you are aiming at and how many steps you want to use. Then take each final value and divide by the number of fading steps to get an increment value for each colour component. Make this increment a float value or it won't work very well. On each iteration of the fade up loop add this increment to the current value to show ( also a float type variable ). However, make this value an int before setting the pixel, like this:-

leds[i].setRGB( int(currentR), int(currentG), int(currentB));

Note the current value starts at zero and gradually gets bigger by the repeated addition of the colour increments.

With regard to that code:-

delay(400);
  FastLED.show();

That second show does nothing as the LEDs have not been changed since the last time the show method was called.

I can now fade gradually to black (off), but I need to fade the other way from black to full color (ie: 255, 255, 0). Bit I need to capture the color just at the last moment it fades to black. Once I have that color, I can then manipulate it back to full brightness. Is there a way to capture this color (ie: sample it) ??

That way I can be assured it will arrive back to the original color (255, 255, 0) or whatever color I originally start out as ?

void loop() {

for (int dot = 0; dot<NUM_LEDS; dot++) {
  leds[dot] = CRGB(255, 255, 0);
  FastLED.show();
  for(int fadeNum = 0; fadeNum<120; fadeNum++) {
  leds[dot].fadeToBlackBy(5);
  FastLED.show();
  delay(90);
  }
  delay(400);
  //cleared for repeat
  leds[dot].fadeToBlackBy(240);
  delay(400);
  leds[dot]= CRGB::Black;
  delay(1770);
}
}

leds[0].r leds[0].g and leds[0].b will give you the current red, green, and blue values for pixel 0, as a byte value. You can use that to get the original values for a color and store for later use. Defining a variable as CRGB led_value; will probably let you just use led_value = leds[0]; but I’m not at a computer to test it at the moment.

I can now fade gradually to black (off), but I need to fade the other way from black to full color (ie: 255, 255, 0).

WTF did I write in reply #12.

If you don’t understand what I wrote then just ask, don’t bloody ignore me.

Grumpy Mike I re-read what you wrote in #12 and I understand the logic involved with gradually fading up.
I guess I thought that there might be a different way that’s similar to the “fadeToBlackBy” method which is simple and works great.

And did you find that way when you looked through all those documentation pages I linked to?

Fading down is easy because you already have the numbers controlling the colour and it is easy to simply reduce them by a certain amount. That is why they included it in the API. Fading up is harder because it involves a target colour which starts at zero.

If you simply want to fade up all the LEDs at once then set the colour and then use the global stet brightness call and start with minimum brightness and set it in a loop progressively brighter until you get to full brightness. But the results are not as good.

When I get a chance later today I’ll take a stab at at. I’ll sample the color just before it turns black. I’ll use that color and then apply a multiplier to it and gradually fade it up and then use the |= to apply a clamped maximum value to it as explained in the wiki. Do you have an example of how the |= is used tho ?

Save the values for R, G, and B for the color you want. At each step of fading up, calculate the current value as (full value × step number) / (total number of steps). That will give you a numerically linear increase at each step.