FastLED white lights fade

I’ve managed to fade up the whole string to maximum brightness. However, the two fade-down options don’t seem to work very well. they are listed in the code. appreciate any advice :slight_smile:

#include "FastLED.h"

#define NUM_LEDS 20
#define DATA_PIN 3
CRGB leds[NUM_LEDS];

uint8_t brightness = 0;
uint8_t   fadeval = 224;

void setup() {
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void fadeall(){
    
  for (int i = 0; i<NUM_LEDS; i++){
    leds[i].nscale8(25); 
    }
   }
  
void loop() {
 // delay(2000);
      EVERY_N_MILLISECONDS(10) {
      brightness ++ ;
    }

  fill_solid(leds, NUM_LEDS, CHSV(255, 10, brightness));
  
FastLED.show();

//fade down option one
//leds[NUM_LEDS].nscale8(fadeval); 
//FastLED.show();

//fade down option two
//leds[NUM_LEDS].fadeToBlackBy (64);
//FastLED.show();

//fade down option three
 //EVERY_N_MILLISECONDS(10){
  //for (int brightness_down; brightness_down >= 255, brightness --;);
 //}  
   //FastLED.show();
//}

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can often be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.

done, as instructed :smiley:

OK, it is now readable, but TBH, I am not at all familiar with any of your constructs - such as "EVERY_N_MILLISECONDS(10)" - never seen it before! :astonished:

got them randomly from somewhere and it does the job!

its a gradient command, every 10 milliseconds the brightness increases by one, something like that

Paul__B:
such as “EVERY_N_MILLISECONDS(10)” - never seen it before! :astonished:

It is part of the fastled library and implement a timer controlled non-blocking interval.

If EVERY_N_MILLISECONDS is non-blocking, your attempt to fade down will interfere with the fading up.

In a first step, I would only apply and show the updated brightness when it is time

void loop()
{
  // delay(2000);
  EVERY_N_MILLISECONDS(10)
  {
    // set new brightness
    brightness ++ ;
    // apply to strip
    fill_solid(leds, NUM_LEDS, CHSV(255, 10, brightness));
    //
    FastLED.show();
  }
}

If that works, you can use a variable to keep track if you want to go up or down and decide based on the value of that variable;

void loop()
{
  static int8_t fadeStep = 1;

  EVERY_N_MILLISECONDS(10)
  {
    // if the last time brightness was 255 or 0, change the direction from increase to descrease or vice versa
    if (brightness == 255 or brightness == 0)
    {
      fadeStep *= -1;
    }
    brightness += fadeStep;

    fill_solid(leds, NUM_LEDS, CHSV(255, 10, brightness));
    FastLED.show();
  }
}

Only thing that you have to change further is the initial value of brightness from 0 to 1 or to apply 0 brightness initially; below uses the latter in setup().

void setup()
{
  FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);

  // apply and show initial brightness
  fill_solid(leds, NUM_LEDS, CHSV(255, 10, brightness));
  FastLED.show();

  brightness++;
}

Compiles but not tested.