i need help with fading in using fastLED

hello there

i am at my whits end trying to figure out how to get a led to fade in and stay at full brightness until the next trigger is activated. iv tried many different ways of achieving this with no joy.

here's the code, what i am trying to achieve is when digitalValue goes high the specified led turns on with a gradual fade in then stay on as long as the input stays high, then fade out when the next trigger is activated.

#include <FastLED.h>

#define NUM_LEDS 5
#define DATA_PIN 12
#define CLOCK_PIN 13
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 100

const int input1 = 3;
const int input2 = 4;
const int input3 = 5;
const int input4 = 6;
uint8_t hue = 160;
uint8_t sat = 255;
uint8_t val = 0;
void setup() {

 Serial.begin(115200); 
    delay( 2000 );
   FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
   FastLED.setBrightness(BRIGHTNESS);
   pinMode(input1, INPUT);
   pinMode(input2, INPUT);
   pinMode(input3, INPUT);
   pinMode(input4, INPUT);
 
}

void loop() {
int digitalValue1 = digitalRead(input1);
int digitalValue2 = digitalRead(input2);
int digitalValue3 = digitalRead(input3);
int digitalValue4 = digitalRead(input4);
for(int i = 0; i < NUM_LEDS; i++ )

if (digitalValue1 == HIGH){
   leds[0].setRGB (0, 0, 255); 
   leds[1].setRGB (0, 0, 0);
   leds[2].setRGB (0, 0, 0);
   leds[3].setRGB (0, 0, 0);
   leds[4].setRGB (0, 0, 0);
   FastLED.show();
}
else if (digitalValue2 == HIGH){
   leds[0].setRGB (0, 0, 0);
   leds[1].setRGB (0, 0, 255);
   leds[2].setRGB (0, 0, 0);
   leds[3].setRGB (0, 0, 0);
   leds[4].setRGB (0, 0, 0); 
FastLED.show(); 
}
else if (digitalValue3 == HIGH){
   leds[0].setRGB (0, 0, 0);
   leds[1].setRGB (0, 0, 0);
   leds[2].setRGB (0, 0, 255);
   leds[3].setRGB (0, 0, 0);
   leds[4].setRGB (0, 0, 0); 
FastLED.show(); 
}
else if (digitalValue4 == HIGH){
   leds[0].setRGB (0, 0, 0);
   leds[1].setRGB (0, 0, 0);
   leds[2].setRGB (0, 0, 0);
   leds[3].setRGB (0, 0, 255);
   leds[4].setRGB (0, 0, 0); 
FastLED.show(); 
}
else{ leds[i].setRGB (255, 255, 255);
FastLED.show();
}
}

i am a extreme novice at coding so iv probably made this look way more complicated than it needs to be. also its my first time posting anything to this site so if iv posted this in the wrong place or done anything wrong i can only apologise.

any help will be much appreciated. :slight_smile:

P9813_led_driver_basic.ino (1.79 KB)

Please modify that post and add code tags. If you don’t know how then read the how to use this forum sticky post.

As to the code you posted you are making no attempt to fade up the LEDs so it is hard to say why you are going wrong. You would be better off showing one of your failed attempts.

soz iv edited the original post.

heres one of the failed attempts where ive got closest to what i want to achieve,

#include <FastLED.h>
#define NUM_LEDS 5
#define DATA_PIN 12
#define CLOCK_PIN 13
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];
#define BRIGHTNESS 255

const int input1 = 3;
const int input2 = 4;
const int input3 = 5;
const int input4 = 6;
uint8_t hue = 160;
uint8_t sat = 255;
uint8_t val = 0;
boolean fadeDirection = 1;

void setup() {
   Serial.begin(115200); 
      //delay( 2000 );
     FastLED.addLeds<P9813, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);  // BGR ordering is typical
     FastLED.setBrightness(BRIGHTNESS);
     pinMode(input1, INPUT);
     pinMode(input2, INPUT);
     pinMode(input3, INPUT);
     pinMode(input4, INPUT);

   
}

void loop() {
 int digitalValue1 = digitalRead(input1);
 int digitalValue2 = digitalRead(input2);
 int digitalValue3 = digitalRead(input3);
 int digitalValue4 = digitalRead(input4);
 

    if (digitalValue1 == HIGH){
        pos1();
       } 
  
      else if (digitalValue2 == HIGH){
        pos2();
       }

       else if (digitalValue2 == HIGH){
        pos2();
       }

       else if (digitalValue2 == HIGH){
        pos2();
       }

       else {
        pos5();
       }
      
}
  void pos1() {
  if (fadeDirection == 1){   
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(hue,sat,val);
     leds[1] = CHSV(0,0,0);
     leds[2] = CHSV(0,0,0);
     leds[3] = CHSV(0,0,0);
     leds[4] = CHSV(0,0,0);
     FastLED.show();
      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection; 
      
      } 
    }
  }
  }

  void pos2() {
  if (fadeDirection == 1){   
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(0,0,0);
     leds[1] = CHSV(hue,sat,val);
     leds[2] = CHSV(0,0,0);
     leds[3] = CHSV(0,0,0);
     leds[4] = CHSV(0,0,0);
     FastLED.show();
      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection; 
      
      } 
    }
  }
  }
  void pos3() {
  if (fadeDirection == 1){   
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(0,0,0);
     leds[1] = CHSV(0,0,0);
     leds[2] = CHSV(hue,sat,val);
     leds[3] = CHSV(0,0,0);
     leds[4] = CHSV(0,0,0);
     FastLED.show();
      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection; 
      
      } 
    }
  }
  }

  void pos4() {
  if (fadeDirection == 1){   
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(0,0,0);
     leds[1] = CHSV(0,0,0);
     leds[2] = CHSV(0,0,0);
     leds[3] = CHSV(hue,sat,val);
     leds[4] = CHSV(0,0,0);
     FastLED.show();
      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection; 
      
      } 
    }
  }
  } 
    void pos5() {
  if (fadeDirection == 1){   
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(255,0,val);
     leds[1] = CHSV(255,0,val);
     leds[2] = CHSV(255,0,val);
     leds[3] = CHSV(255,0,val);
     leds[4] = CHSV(255,0,val);
     FastLED.show();
      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection; 
      
      } 
    }
  }
  }

//this section is for the fade down but iv left that untill ive figured out the first part
/* if (fadeDirection == 0) {  //fade down
    EVERY_N_MILLISECONDS(9){
     //leds[0] = CHSV(hue,sat,val);
     //leds[1] = CHSV(hue,sat,val);
     //leds[2] = CHSV(hue,sat,val);
     //leds[3] = CHSV(hue,sat,val);
     leds[4] = CHSV(hue,sat,val);
     FastLED.show();
      val = val - 1;
      if (val == 0) {
        fadeDirection = !fadeDirection;  //reverse direction
      
      }
    }
  }*/

the only problem is "pos5" fades in and stays lit exactly how i want it to but then none of the other "pos's" light the leds even though the board is still reading the triggers.

my guess is that its something to do with this part of the code

      val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection;

iv tried fiddling with it but no luck. it just goes back to fading to full brightness continuously in a saw wave pattern.

thanks :slight_smile:

Thanks for using code tags it really helps.

val = val + 1;
      if (val == 255) {
        fadeDirection = !fadeDirection;

As this always adds 1 to the value irrespective of the fade direction it will get stuck at the top. Also using == means that val will exceed 255. The brightness values wrap round to 8 bits so that if you have 256 it is just like having 0, 257 is the same as 1, 258 is the same as 2 and so on.

So I would try using code like this:-

uint8_t  fadeDirection = 1; // remove the boolean fadeDirection = 1;
void pos1() {  
   EVERY_N_MILLISECONDS(3) {
     leds[0] = CHSV(hue,sat,val);
     leds[1] = CHSV(0,0,0);
     leds[2] = CHSV(0,0,0);
     leds[3] = CHSV(0,0,0);
     leds[4] = CHSV(0,0,0);
     FastLED.show();
      val = val + fadeDirection;
      if (val > 255) {
        fadeDirection = -fadeDirection;
        val = val + fadeDirection; // to keep val less than 255
      }

This will make the LEDs fade up and down when selected. If you want them to do something else it is simple to modify what happens at the top of the fade. Just ask if you don't understand anything.

hi mike

thanks for that its running much more smoothly now and triggering properly.

how would i go about holding the val at 255 after 1 fade in. would i be correct in thinking something needs to be added after the "if (val > 255)" statement?

would i be correct in thinking something needs to be added after the "if (val > 255)" statement?

Yes if you want to hold just stop incrementing.
It would be best to have some sort of flag that controls if it holds or not so first define a boolean variable called hold and initially set it to false.

Something like this:-

      if(! hold ) val = val + fadeDirection;
      if (val > 255) {
        fadeDirection = -fadeDirection;
        val = val + fadeDirection; // to keep val less than 256
        hold = true; // so don't increment anymore
      }

When setting a new sequence going be sure to set hold back to false as well as resetting the value of val.