Led strip WS2812B - problem with colors

Hello.I have an arduino Uno and a led strip WS2812B (30 leds , 5 Volt , 3 pin (GND , 5V , Din) ).
I connected GND to arduino's GND , +5V to arduino's Vin and Din to pin 4 (of arduino).

Code:

#include <FastLED.h>

#define LED_STRIP_PIN 4
#define NUM_OF_LEDS 30

CRGB leds[NUM_OF_LEDS];
int brightness = 50;

void setup()
{
  FastLED.addLeds<WS2812B, LED_STRIP_PIN, GRB>(leds, NUM_OF_LEDS);
  FastLED.setBrightness(brightness);
}


void loop()
{
for(int i = 0; i < NUM_OF_LEDS; i++)
{ 
  leds[i] = CRGB::Red;
  FastLED.show();
  //delay(30);
}
}

The problem is that all leds don't get red , some are red others are yellow , green and mixed colors.Do you know what's going on?
If I enable only 5 leds, for instance:


  leds[0] = CRGB::Red;
  leds[1] = CRGB::Red;
  leds[2] = CRGB::Red;
  leds[3] = CRGB::Red;
  leds[4] = CRGB::Red;
  FastLED.show();
  //delay(30);

, I have no problem.

Thanks.

Do you have a series resistor on the data line?

How are you powering all this? If 5 LEDs work but 30 do not, you most likely do not have enough power for your project.

What kind of power source do you have? For Arduino vin recommended input is 7-12V.

I would recommend better power source or separate power source for each.

I had to put a resistor to make it work ,but now I do not have a lot of brightness as before.I changed the value from 50 to 255 but the brightness of leds don't change

Ok problem solved.I put 4 resistors in parallel

:face_with_raised_eyebrow:

Did you separate the power supply as recommended?

I only use the usb from my laptop

The resistors go in series between the Arduino board output pin and the first Din of the LED strip. The resistor should have no effect on the LED brightness. The resistor should be around 470Ω. Please show how you have connected the resistors and their values.

The Uno 5V regulator cannot provide the current for more than a few LEDs, period. You must have an external 5V supply for the LED strip that is capable of supplying the required current. WS2812 LEDs can draw up to 60 milliamps per pixel (white, full bright). 30 pixels will need 1.8A.

I do not have a 5V adaptor.I only have a 9V 1A adaptor , a 220 Ohm resistor in Din and something is wrong with colors.When I upload a scetch with blue color ,leds become red and when I remove the 9V adaptor and put it back again ,leds become blue.
I don't know what's wrong with it.

What's wrong is the power.
I'm sorry that you don't have the stuff on hand to fix it. The wait for parts can't feel very long.
The reality is that you still need the better power supply. That is the root of your issue and should fix you right up.
There is not an alternative with the equipment that you have on hand. I wish there was.

OK never mind I will buy one ,but what exactly do I need? 5V 1A or 5V 2A adaptor?

Good question.
I would go for at least 2A

I bought a 5V 2A adaptor and now it works very good.Now I have a question.
How can I save some colors in an array.For example something like this:

int array[3] = {CRGB::Red , CRGB::Blue , CRGB:White}; etc

I know it's not correct,but how can I do it?

Thanks.

Awesome!!!

CHSV your_color = CHSV(10, 20, 30);

There are probably other ways too.

Like this?

#include <FastLED.h>

#define NUM_LEDS 4

#define DATA_PIN 3

CRGB leds[NUM_LEDS];

void setup()
{
   FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical
   CRGB array[] = {CRGB::Red, CRGB::Yellow, CRGB::Green, CRGB::Blue};
   leds[0] = array[0];
   leds[1] = array[1];
   leds[2] = array[2];
   leds[3] = array[3];
   FastLED.show();
}

void loop()
{   
}

Thank you,I forgot that it is CRGB type