Fading an APA102 Addressable LED Strip

Hello,

I have been attempting to write a sketch which will allow me to sequentially fade my APA102 strip from black to various colors using the FastLED library. My intention is to script these fades and synchronise them with a short piece of music as part of a cinematography experiment, however I am quite new to both the Arduino and C++ language.

I can not work out how to apply one color to the entire strip and what I have written thus far will fade the LEDs in but not out for some reason. If anybody could shed some light on these matters, I would be very grateful. :3

Here is my code.

#include <FastLED.h>
#define NUM_LEDS 60
#define CLOCK_PIN 10
#define DATA_PIN 11
CRGB leds[NUM_LEDS]; //Create array of 60 LEDs called 'leds'
 
void setup() {
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);
}
 
void loop() {
 whitefade(); //Fade first LED in and out
 delay(1000);
 redfade();  //Fade last LED in and out
 delay(1000);
}
 
 
void whitefade() {
    for(int brightness=0; brightness<255; brightness++){ //Increment 'brightness' from 0 to 255
      leds[0].setHSV(255, 0, brightness); //Brighten first LED using 'brightness' value
      FastLED.show(); //Update LED display
    }
   
    for(int brightness=255; brightness>=255; brightness--){ //Decrement 'brightness' from 255 to 0
      leds[0].setHSV(255, 0, brightness); //Dim first LED using 'brightness' value
      FastLED.show(); //Update LED display
    }
}
 
void redfade() {
    for(int brightness=0; brightness<255; brightness++){ //Increment 'brightness' from 0 to 255
      leds[59].setHSV(255, 255, brightness); //Brighten last LED using 'brightness' value
      FastLED.show(); //Update LED display
    }
   
    for(int brightness=255; brightness>=255; brightness--){ //Decrement 'brightness' from 255 to 0
      leds[59].setHSV(255, 255, brightness); //Dim last LED using 'brightness' value
      FastLED.show(); //Update LED display
    }
}

Thank you!

brightness>=255

This is why it won't fade back, should be brightness>=0 (I did this the other day as well :P)

To change the colour, you have to change the value of hue (the first variable in setHSV()).

Hope it helps!

Yes, it works now. Thank you very much.

Is there a way to assign a color to the entire array at once? I originally tried an if loop but not all of the LEDs illuminate simultaneously. If I specify the color of each LED individually, then they will all illuminate simultaneously but that is quite an inelegant solution.

I essentially just want to avoid having to do this. :stuck_out_tongue:

#include <FastLED.h>
#define NUM_LEDS 60
#define CLOCK_PIN 10
#define DATA_PIN 11
CRGB leds[NUM_LEDS];  //Create array of 60 LEDs called 'leds'
int outspeed = 0;     //Create integer for fade out speed
int HSV[2] = {0, 0};  //Create array for hue and saturation values

void setup() {
  FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);
}
 
void loop() {
 outspeed = 0;   //Set fade out speed for next fade
 HSV[0] = 40;    //Set hue for next fade
 HSV[1] = 255;   //Set saturation for next fade
 fade();         //Begin fade
 delay(1000);
 outspeed = 0;
 HSV[0] = 200;
 HSV[1] = 255;
 fade();         
 delay(1000);
 outspeed = 0;
 HSV[0] = 100;
 HSV[1] = 255;
 fade();        
 delay(1000);
}

 
void fade() {
    for(int brightness=0; brightness<=255; brightness++){ //Increment 'brightness' from 0 to 255
leds[0].setHSV(HSV[0], HSV[1], brightness);
leds[1].setHSV(HSV[0], HSV[1], brightness);
leds[2].setHSV(HSV[0], HSV[1], brightness);
leds[3].setHSV(HSV[0], HSV[1], brightness);
leds[4].setHSV(HSV[0], HSV[1], brightness);
leds[5].setHSV(HSV[0], HSV[1], brightness);
leds[6].setHSV(HSV[0], HSV[1], brightness);
leds[7].setHSV(HSV[0], HSV[1], brightness);
leds[8].setHSV(HSV[0], HSV[1], brightness);
leds[9].setHSV(HSV[0], HSV[1], brightness);
leds[10].setHSV(HSV[0], HSV[1], brightness);
leds[11].setHSV(HSV[0], HSV[1], brightness);
leds[12].setHSV(HSV[0], HSV[1], brightness);
leds[13].setHSV(HSV[0], HSV[1], brightness);
leds[14].setHSV(HSV[0], HSV[1], brightness);
leds[15].setHSV(HSV[0], HSV[1], brightness);
leds[16].setHSV(HSV[0], HSV[1], brightness);
leds[17].setHSV(HSV[0], HSV[1], brightness);
leds[18].setHSV(HSV[0], HSV[1], brightness);
leds[19].setHSV(HSV[0], HSV[1], brightness);
leds[20].setHSV(HSV[0], HSV[1], brightness);
leds[21].setHSV(HSV[0], HSV[1], brightness);
leds[22].setHSV(HSV[0], HSV[1], brightness);
leds[23].setHSV(HSV[0], HSV[1], brightness);
leds[24].setHSV(HSV[0], HSV[1], brightness);
leds[25].setHSV(HSV[0], HSV[1], brightness);
leds[26].setHSV(HSV[0], HSV[1], brightness);
leds[27].setHSV(HSV[0], HSV[1], brightness);
leds[28].setHSV(HSV[0], HSV[1], brightness);
leds[29].setHSV(HSV[0], HSV[1], brightness);
leds[30].setHSV(HSV[0], HSV[1], brightness);
leds[31].setHSV(HSV[0], HSV[1], brightness);
leds[32].setHSV(HSV[0], HSV[1], brightness);
leds[33].setHSV(HSV[0], HSV[1], brightness);
leds[34].setHSV(HSV[0], HSV[1], brightness);
leds[35].setHSV(HSV[0], HSV[1], brightness);
leds[36].setHSV(HSV[0], HSV[1], brightness);
leds[37].setHSV(HSV[0], HSV[1], brightness);
leds[38].setHSV(HSV[0], HSV[1], brightness);
leds[39].setHSV(HSV[0], HSV[1], brightness);
leds[40].setHSV(HSV[0], HSV[1], brightness);
leds[41].setHSV(HSV[0], HSV[1], brightness);
leds[42].setHSV(HSV[0], HSV[1], brightness);
leds[43].setHSV(HSV[0], HSV[1], brightness);
leds[44].setHSV(HSV[0], HSV[1], brightness);
leds[45].setHSV(HSV[0], HSV[1], brightness);
leds[46].setHSV(HSV[0], HSV[1], brightness);
leds[47].setHSV(HSV[0], HSV[1], brightness);
leds[48].setHSV(HSV[0], HSV[1], brightness);
leds[49].setHSV(HSV[0], HSV[1], brightness);
leds[50].setHSV(HSV[0], HSV[1], brightness);
leds[51].setHSV(HSV[0], HSV[1], brightness);
leds[52].setHSV(HSV[0], HSV[1], brightness);
leds[53].setHSV(HSV[0], HSV[1], brightness);
leds[54].setHSV(HSV[0], HSV[1], brightness);
leds[55].setHSV(HSV[0], HSV[1], brightness);
leds[56].setHSV(HSV[0], HSV[1], brightness);
leds[57].setHSV(HSV[0], HSV[1], brightness);
leds[58].setHSV(HSV[0], HSV[1], brightness);
leds[59].setHSV(HSV[0], HSV[1], brightness);
      FastLED.show(); //Update LED display
    }
    
    for(int brightness=255; brightness>=0; brightness--){ //Decrement 'brightness' from 255 to 0
leds[0].setHSV(HSV[0], HSV[1], brightness);
leds[1].setHSV(HSV[0], HSV[1], brightness);
leds[2].setHSV(HSV[0], HSV[1], brightness);
leds[3].setHSV(HSV[0], HSV[1], brightness);
leds[4].setHSV(HSV[0], HSV[1], brightness);
leds[5].setHSV(HSV[0], HSV[1], brightness);
leds[6].setHSV(HSV[0], HSV[1], brightness);
leds[7].setHSV(HSV[0], HSV[1], brightness);
leds[8].setHSV(HSV[0], HSV[1], brightness);
leds[9].setHSV(HSV[0], HSV[1], brightness);
leds[10].setHSV(HSV[0], HSV[1], brightness);
leds[11].setHSV(HSV[0], HSV[1], brightness);
leds[12].setHSV(HSV[0], HSV[1], brightness);
leds[13].setHSV(HSV[0], HSV[1], brightness);
leds[14].setHSV(HSV[0], HSV[1], brightness);
leds[15].setHSV(HSV[0], HSV[1], brightness);
leds[16].setHSV(HSV[0], HSV[1], brightness);
leds[17].setHSV(HSV[0], HSV[1], brightness);
leds[18].setHSV(HSV[0], HSV[1], brightness);
leds[19].setHSV(HSV[0], HSV[1], brightness);
leds[20].setHSV(HSV[0], HSV[1], brightness);
leds[21].setHSV(HSV[0], HSV[1], brightness);
leds[22].setHSV(HSV[0], HSV[1], brightness);
leds[23].setHSV(HSV[0], HSV[1], brightness);
leds[24].setHSV(HSV[0], HSV[1], brightness);
leds[25].setHSV(HSV[0], HSV[1], brightness);
leds[26].setHSV(HSV[0], HSV[1], brightness);
leds[27].setHSV(HSV[0], HSV[1], brightness);
leds[28].setHSV(HSV[0], HSV[1], brightness);
leds[29].setHSV(HSV[0], HSV[1], brightness);
leds[30].setHSV(HSV[0], HSV[1], brightness);
leds[31].setHSV(HSV[0], HSV[1], brightness);
leds[32].setHSV(HSV[0], HSV[1], brightness);
leds[33].setHSV(HSV[0], HSV[1], brightness);
leds[34].setHSV(HSV[0], HSV[1], brightness);
leds[35].setHSV(HSV[0], HSV[1], brightness);
leds[36].setHSV(HSV[0], HSV[1], brightness);
leds[37].setHSV(HSV[0], HSV[1], brightness);
leds[38].setHSV(HSV[0], HSV[1], brightness);
leds[39].setHSV(HSV[0], HSV[1], brightness);
leds[40].setHSV(HSV[0], HSV[1], brightness);
leds[41].setHSV(HSV[0], HSV[1], brightness);
leds[42].setHSV(HSV[0], HSV[1], brightness);
leds[43].setHSV(HSV[0], HSV[1], brightness);
leds[44].setHSV(HSV[0], HSV[1], brightness);
leds[45].setHSV(HSV[0], HSV[1], brightness);
leds[46].setHSV(HSV[0], HSV[1], brightness);
leds[47].setHSV(HSV[0], HSV[1], brightness);
leds[48].setHSV(HSV[0], HSV[1], brightness);
leds[49].setHSV(HSV[0], HSV[1], brightness);
leds[50].setHSV(HSV[0], HSV[1], brightness);
leds[51].setHSV(HSV[0], HSV[1], brightness);
leds[52].setHSV(HSV[0], HSV[1], brightness);
leds[53].setHSV(HSV[0], HSV[1], brightness);
leds[54].setHSV(HSV[0], HSV[1], brightness);
leds[55].setHSV(HSV[0], HSV[1], brightness);
leds[56].setHSV(HSV[0], HSV[1], brightness);
leds[57].setHSV(HSV[0], HSV[1], brightness);
leds[58].setHSV(HSV[0], HSV[1], brightness);
leds[59].setHSV(HSV[0], HSV[1], brightness);
      FastLED.show(); //Update LED display
      delay(outspeed);
    }
}

Manually specifying each LED won’t be practical with larger strips.

    for(int brightness=0; brightness<=255; brightness++){ //Increment 'brightness' from 0 to 255
leds[0].setHSV(HSV[0], HSV[1], brightness);
leds[1].setHSV(HSV[0], HSV[1], brightness);
leds[2].setHSV(HSV[0], HSV[1], brightness);

Use a nested for loop instead:

for(int brightness=0; brightness<=255; brightness++){
  for (int index=0; index<=(nrOfLEDs-1); index++){
    leds[i].setHSV(HSV[0], HSV[1], brightness);
  }
  FastLED.show(); //Update LED display
}

for (int index=0; index<=(nrOfLEDs-1); index++)This is more usually written for (int index=0; index < nrOfLEDs; index++)

I'm planning on using the APA102 for my next project instead of the ws2812b. I'd like to control the lights using Artnet. Since both the wiz820io and APA102 uses SPI will this be a problem ?