1

I have a strip of 7 leds. its ws2812. Im starting them on blue. Next I want them to do a slow fade into pink. So all Im trying right now is to increment r.

I can get them to work one by one. But I need them to all fade together from blue to pink.

Ive moved my r++ loop around till I was cross-eyed. Still it didnt fade all 7 leds to pink all at once.

sometimes it would stay blue and do nothing sometimes fade to pink one pixel at a time.

How do I get all 7 leds to fade together? Thank you.

#include <FastLED.h>
#define NUM_LEDS 7
#define DATA_PIN 6
#define BRIGHTNESS 45
#define COLOR_ORDER GRB
#define g 50
#define b 100

int r; //changed it to undefined

CRGB leds[NUM_LEDS];

void setup(){
FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);

 }
void loop(){

this lights all leds to blue

  for(int x = 0; x < NUM_LEDS; x++){

  leds[x] = CRGB(r,g,b);
 }
  FastLED.show();
  delay(10);

this code should grab all 7 leds in NUM_LEDS store it in x

  for(int x = 0; x < NUM_LEDS; x++){



  leds[x] = CRGB(r,g,b);

Heres the problem area. I get no errors but it doesnt fade all 7 leds at a time. Ive tried moving my code around but it either doesnt fade or fades to pink one led at a time. where should my 'for loop' for incrementing r be at? Should I be changing rgb all at once using array?

//take r and fade it up to 254


for(int r = 0; r < 254; r++) {


  }
  }
     FastLED.show();
 delay(100);    

}

migrated from electronics.stackexchange.com Jul 4 '15 at 5:56

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

1

If you want to fade all the LEDs form blue to red at the same time, I think you want code like this (explanatory comments embedded)...

#include <FastLED.h>
#define DATA_PIN 6
#define NUM_LEDS 7

#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

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

}

void loop(){

  // Let's take 256 steps to get from blue to red
  // (the most possible with an LED with 8 bit RGB values)

  for( int colorStep=0; colorStep<256; colorStep++ ) {

      int r = colorStep;  // Redness starts at zero and goes up to full
      int b = 255-colorStep;  // Blue starts at full and goes down to zero
      int g = 0;              // No green needed to go from blue to red

      // Now loop though each of the LEDs and set each one to the current color

      for(int x = 0; x < NUM_LEDS; x++){
          leds[x] = CRGB(r,g,b);
      }



      // Display the colors we just set on the actual LEDs
      FastLED.show();

      delay(10); 
  }

}
0

@bigjosh has provided code, so I will provide some plain text logic to explain what you should be doing.

To fade from one color to the next, in this case from Solid Blue (0,0,255) to Pink (Red + Blue, 255,0,255), you need to change the value a little, latch it, then wait before you change the value again. Pseudo code:

rgb = 0,0,255

write rgb

wait X milliseconds

rgb = r + 1,0,255

write rgb

wait X milliseconds

loop until r = 255

At each step, you need to push out the value to the leds before delaying and then starting again. The delay can and should be varied to your satisfaction. I find a fast fade better. At single steps of r + 1, even slow delays will seem mostly smooth.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.