Help with WS2812B led strip code

Hi i am new to arduino, i am using a ws2812b led strip of 10 leds and the code from the fastled firstlight exemple, which i am trying to change so i can light 2 leds at a time instead of one to get a 5 flash loop in white, was looking for a more of a flux capasiter look, if anyone can help me with this thanks

here is my code

#include <FastLED.h>

///////////////////////////////////////////////////////////////////////////////////////////
//
// Move a white dot along the strip of leds.  This program simply shows how to configure the leds,
// and then how to turn a single pixel white and then off, moving down the line of pixels.
// 

// How many leds are in the strip?
#define NUM_LEDS 10

// Data pin that led data will be written out over
#define DATA_PIN 3

// Clock pin only needed for SPI based chipsets when not using hardware SPI
//#define CLOCK_PIN 8

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];

// This function sets up the ledsand tells the controller about them
void setup() {
	// sanity check delay - allows reprogramming if accidently blowing power w/leds
   	delay(2000);

      
      FastLED.addLeds<WS2811, DATA_PIN, RGB>(leds, NUM_LEDS);
      // FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
      // FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
      
}

// This function runs over and over, and is where you do the magic to light
// your leds.
void loop() {
   // Move a single white led 
   for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
      // Turn our current led on to white, then show the leds
      leds[whiteLed] = CRGB::White;

      // Show the leds (only one of which is set to white, from above)
      FastLED.show();

      // Wait a little bit
      delay(100);

      // Turn our current led back to black for the next loop around
      leds[whiteLed] = CRGB::Black;
   }
}

Your code will display 10 leds. Where do you think the code says 5?

BTW, in a for loop the last arg is normally whiteLed++ or ++whiteLed

What is happening now and what do You want to happen?

This is not a real thing. Be descriptive.

The WS2811 light three LEDs at one address. It is not the same as WS2812B.

did any LED´s light´s up?
is the strip dark?
did you use the right Voltage?

and im seeing one thing in xour discription you use an WS2812b
in your setup you use the ws2811
change it with the line below and try again

`FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical`

Hi , yes the leds light up, they individually light up in a white led chaser effect, of 10 leds , need help with the code so I can keep the same effect but have 2 leds light up simultaneously in the chaser instead of one

Thanks..

Okay, perfect. The LEDs get their signals from the Arduino. There are two different types of strips: one runs on 5V, where every LED can be controlled individually, and the other runs on 12V, where three LEDs share the same signal and light up together.

For me, the Adafruit NeoPixel library is much easier to use — you can give that one a try.

End the loop early.
Add one more line addressing the next LED.

   for(int whiteLed = 0; whiteLed < NUM_LEDS - 1; whiteLed = whiteLed + 1)
.
.
leds[whiteLed] = CRGB::White;
leds[whiteLed + 1] = CRGB::White;

And adjust the loop condition...

for(int whiteLed = 0; whiteLed <( NUM_LEDS - 1)/2; whiteLed = whiteLed + 2

That loop

for (int whiteLed = 0; whiteLed <( NUM_LEDS - 1)/2; whiteLed = whiteLed + 2) {

does 0, 2, 0, 2 forever.

(NUM_LEDS - 1) / 2 = 9 / 2 = 4.

So whatever you were going for is not happening.

But why are you even trying to reduce the number of steps by half? A pixel takes ten steps to cross the strip. A two-pixel train will still take ten steps…

@xfpd was close, and had he tested the code the obvious flaw woukd have an obvious solution- you gotta turn off the things you turn on:

     // Turn our current leds back to black for the next loop around
     leds[whiteLed] = CRGB::Black;
     leds[whiteLed + 1] = CRGB::Black;

Which plays out over nine steps thusly:

XX........
.XX.......
..XX......
...XX.....
....XX....
.....XX...
......XX..
.......XX.
........XX
XX........
.XX.......
..XX......
...XX.....
....XX....

Which is pretty. When trains get longer or sometimes even with just two, you can write it so the train enters from the left and exits to the right, like

..........
X.........
XX........
.XX.......
..XX......
...XX.....
....XX....
.....XX...
......XX..
.......XX.
........XX
.........X
..........
..........

Where I've added a few beats of no lights at all between passes.

These tiny things can get bit complicated. I don't think FastLED does bounds checking, so you can't just write to pixels that have no memory allocated. This is one advantage of Adafruit's Neopixel library… you can write a simpler loop and just place the train anywhere, into negative indices or beyond the last real pixel. Bounds check will mean when the train is off screen, so to speak, it does no harm.

I draw things like this out as I have done above. This informs crafting the loops, or in the case of non-blocking implementation, writing the maths that make each step.

a7

2 Likes

This code moves a train along a strip. It moves one step per iteration of the loop() function.

# include <FastLED.h>

// Move a train of pixels along a strip of leds.

# define NUM_LEDS 27  // size of strip
# define DATA_PIN 3

CRGB leds[NUM_LEDS];

# define TRAIN 5 // length of train

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

void loop() {
 static int caboose = -TRAIN; // train caboose location

// move the caboose pixel to be the new engine pixel
// turn off the caboose
 if (caboose > -1 && caboose < NUM_LEDS)
   leds[caboose] = CRGB::Black;

// turn on what will be the new engine
 int newEngine = caboose + TRAIN;
 if (newEngine > -1 && newEngine < NUM_LEDS)
   leds[newEngine] = CRGB::Red;

 FastLED.show();
 delay(100);  // sue me

 caboose++;   // move train one step down the strip, reset if done
 if (caboose == NUM_LEDS) {
   delay(1300);
   caboose = -TRAIN;
 }
}

The timing is done with delays, but there are no for *loops*. It's more than half way to being non-blocking.

a7

Ok , thanks for all the help, much appreciated :+1:

hi thanks for you code, this is the effect i was looking for, is running in green at the moment, would you be able to help me with the code in changing the color, would like it to run in white then maybe i can learn from the from you code to change to other colors

thanks, ic

You can just… read that same code and find out yourself how to do so…

// [...]

 if (newEngine > -1 && newEngine < NUM_LEDS)
   leds[newEngine] = CRGB::Red; // change "Red" to "White"!
 FastLED.show();

// [...]

There are also other ways to change LED’s color which are documented in the FastLED docs.

Hmm, are you using alto’s exact same code? If that’s so, watch out for this (specifically the part that says “RGB“):

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

You should change “RGB” to “GRB”; otherwise, if you are going to, say, change the color to red, you’ll actually be getting green instead. “GRB” is the correct colour order for your LED strip. See more here:

RGB was used in the original code. It's hard to see errors when you make white pixels!

wokwi seems to have GRB strips.

a7

1 Like

Yup. I just wanted to make sure OP knew that not all models have the same RGB order (OP mentioned that the LEDs light up green instead of the expected red from your code, although they didn't complain about it).

Sorry if I wasn’t clear :smile:

You changed your user name.