LED ws2812b how to random beetwen few colours

Hello
I`m try to random my leds only cover this colors
(255, 0, 0)); // Red
(0, 255, 0)); // Green
(0, 0, 255)); // Blue
(255, 255, 255)); //White

Could You advice how to change this code :

#include <Adafruit_NeoPixel.h>

#define PIN        3 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24 // Popular NeoPixel ring size


Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 200 // Time (in milliseconds) to pause between pixels

unsigned char x[2]={0, 255};
unsigned char y = x[random(0,2)];  //pick one

void setup() {

 pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
 pixels.show(); 
 pixels.setBrightness(25);
 
}

void loop() {
  
  
pixels.clear(); 

  
  for(int i=0; i<NUMPIXELS; i++) { // For each pixel...

    
    pixels.setPixelColor(i, pixels.Color(random(y),random(y),random(y)));

    pixels.show();   // Send the updated pixel colors to the hardware.

    delay(DELAYVAL); // Pause before next pass through loop
  }
}

Right now the color is pick up randomly but it`s to much other colors.
See file - Video .
I want to define the 4 color only to pick up them randomly.

Video

Regards

I want to define the 4 color only to pick up them randomly.

So you put the colours you want into an array, and then generate a random number for the array index when setting the colour.

Or if you are not up to doing that then use a switch statement on a random number and use each case to put in a different colour.

Could You advice how the code should be look`s like?

djmcg:
Could You advice how the code should be look`s like?

It should look like normal C code, you know with statements, declarations and function calls.

For example the switch statement should look like this:-
control structure - switch case

switch (random(0, 3)) {
  case 0:
    pixels.setPixelColor(i, pixels.Color(255, 0, 0));
    break;
  case 1:
    pixels.setPixelColor(i, pixels.Color(0, 255,  0));
    break;
  case 2:
    pixels.setPixelColor(i, pixels.Color(0, 0, 255));
    break;
  case 3:
    pixels.setPixelColor(i, pixels.Color(255, 255, 255));
    break;
}

Hi I put it in to code finally work but white is missing
Could You advice what to wrote in code to see serialprint "case" which was drawn .

#include <Adafruit_NeoPixel.h>

#define PIN        3 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24 // Popular NeoPixel ring size


Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 200 // Time (in milliseconds) to pause between pixels

//unsigned char x[2]={0, 255};
//unsigned char y = x[random(0,2)];  //pick one

void setup() {
  Serial.begin(9600);
 pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
 pixels.show(); 
 pixels.setBrightness(225);
 
}

void loop() {
  
    pixels.clear(); 
              for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
            {
                    switch (random(0, 3)) {
                case 0:
                  pixels.setPixelColor(i, pixels.Color(255, 0, 0));
                  break;
                case 1:
                  pixels.setPixelColor(i, pixels.Color(0, 255,  0));
                  break;
                case 2:
                  pixels.setPixelColor(i, pixels.Color(0, 0, 255));
                  break;
                case 3:
                  pixels.setPixelColor(i, pixels.Color(255, 255, 255));
                  break;
 
                                          }
          } 
    //pixels.setPixelColor(i, pixels.Color(random(y),random(y),random(y)));
    pixels.show();   // Send the updated pixel colors to the hardware.
    delay(DELAYVAL); // Pause before next pass through loop
    Serial.println(i); 
  }
}

Finally works
Thank You

#include <Adafruit_NeoPixel.h>

#define PIN        3 // On Trinket or Gemma, suggest changing this to 1

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 24 // Popular NeoPixel ring size


Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

#define DELAYVAL 500 // Time (in milliseconds) to pause between pixels

//unsigned char x[2]={0, 255};
//unsigned char y = x[random(0,2)];  //pick one

void setup() {
  Serial.begin(9600);
 pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
 pixels.show(); 
 pixels.setBrightness(25);
 
}

void loop() {
  
    pixels.clear(); 
              for(int i=0; i<NUMPIXELS; i++) { // For each pixel...
            {
                    switch (random(0, 4)) {
                case 0:
                  pixels.setPixelColor(i, pixels.Color(255, 0, 0));
                  Serial.println("RED");
                  break;
                case 1:
                  pixels.setPixelColor(i, pixels.Color(0, 255,  0));
                  Serial.println("GREEN");
                  break;
                case 2:
                  pixels.setPixelColor(i, pixels.Color(0, 0, 255));
                  Serial.println("BLUE");
                  break;
                case 3:
                  pixels.setPixelColor(i, pixels.Color(255, 255, 255));
                  Serial.println("WHITE");
                  break;
 
                                          }
          } 
    //pixels.setPixelColor(i, pixels.Color(random(y),random(y),random(y)));
    pixels.show();   // Send the updated pixel colors to the hardware.
    delay(DELAYVAL); // Pause before next pass through loop
    Serial.println(i); 
  }
}

Glad that it works, you might want to put a random seed in the setup function

randomSeed(analogRead(3));

Added.
Now when I restart Arduino the LEDs colors is random start . :slight_smile: