4

I'm attempting to fade a strip of ws2812 7 pixels in length. Below I am using a for loop to increment the color red. I can fade it to red (from blue) but it will do it one pixel at a time or it just stays blue, depending where I move my r for-loop to. I need it to fade from color to color all 7 LEDs at once.

I get no errors.

I don't understand the process of getting NUM_LEDS leds all into x then proceeding with my color changing. Seems like I'm grabbing one pixel, fading it to red, then another...

fastLeds 3.03 library, arduino 1.0.6

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

#define BRIGHTNESS 45
#define COLOR_ORDER GRB
#define g 50
#define b 100
int r;
CRGB leds[NUM_LEDS];

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

void loop(){
  for(int x = 0; x < NUM_LEDS; x++){
    delay(10);
    leds[x] = CRGB(r,g,b);
  }
  FastLED.show();
  delay(10); 

  //-------------------------

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

if NUM_LEDS is holding the number 7 why can't I just do it like this below: leds[NUM_LEDS]?

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

2 Answers 2

6

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); 
  }

}
3
  • Hi bigjosh. Thank you for taking the time to look over and edit my code. But it isnt working. It just lights up red. no blue. no fading. was I suppose to add anything or is it complete above?
    – user28025
    Commented Jul 4, 2015 at 4:15
  • Doh! Misplaced parenthesis. Edited- should work now. Sorry!
    – bigjosh
    Commented Jul 4, 2015 at 4:25
  • AHHH! Thank you bigjosh. Im embarrassed to say how long ive been trying to fade my led strip. Thanks again man, Youve been a big help.
    – user28025
    Commented Jul 4, 2015 at 4:39
0

Here is the initial 0-255 rainbow. I dunno if this is what you are trying to achieve but i took initiative from @bigjosh's answer. This just cycles thru each rgb from 0-255. @bigjosh does start red at 0-255 allowing for more for red colors.

void loop(){

//start from red
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

  int r = 255;
  int g = 0;
  int b = colorStep;

  // Now loop though each of the LEDs and set each one to the current color
  for(int x = 0; x < LED_COUNT; x++){
      leds[x] = CRGB(r,g,b);
  }

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

    //into blue
      for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = colorStep;
      int g = 0;
      int b = 255;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

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

//start from blue
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = 0;
      int g = colorStep;
      int b = 255; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

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

//into green
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 0;
      int g = 255;
      int b = colorStep; 

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }

//start from green
  for( int colorStep=0; colorStep <= 255; colorStep++ ) {

      int r = colorStep;
      int g = 255;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }

//into yellow
  for( int colorStep=255; colorStep >= 0; colorStep-- ) {

      int r = 255;
      int g = colorStep;
      int b = 0;

      // Now loop though each of the LEDs and set each one to the current color
      for(int x = 0; x < LED_COUNT; x++){
          leds[x] = CRGB(r,g,b);
      }

      // Display the colors we just set on the actual LEDs
  delay(wait_time); 
  LEDS.show();
  }


} //end main loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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