Random color changing with WS2812b, could the code be any better?

#include <FastLED.h>                     // needed for WS2812 LEDs
#define LED_PIN     3                    // where it's connected to
#define NUM_LEDS    8                    // how many LEDs total
#define BRIGHTNESS  128                  // not too bright, not too dim
#define LED_TYPE    WS2811               // change if needed
#define COLOR_ORDER GRB                  // change if needed
CRGB leds[NUM_LEDS];
byte b[24] = {};

void setup() {
  randomSeed(analogRead(0));
  delay( 300 );                            // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );
  for (int i = 0; i < 24; i++) {
    b[i] = 0;
  }
}

void loop()
{
  byte x = random(24);
  b[x] = b[x] ^ 255;
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = CRGB(b[i * 3], b[i * 3 + 1], b[i * 3 + 2]);
  }
  FastLED.show();
  delay(100);
}

Plan to use em on a few small tree, the code basically turns on or off one of the 3 color LEDs to get 8 basic colors (red, yellow, green, cyan, blue, magenta, white, and off ) Using XOR 255 flips the stored value either 0 or 255.

But I think it could be better since I am refreshing all of LED after changing just one color. Maybe a 2D array, select array LED, flip one of the 3 color, and push out only that one? I’ve had problem getting 2D array working before.

The current code would top out at about 630 LEDs (*3 colors each) before I nearly run out of RAM and risk operation issues.

update: got the arrays working

#include <FastLED.h>                     // needed for WS2812 LEDs
#define LED_PIN     3                    // where it's connected to
#define NUM_LEDS    8                    // how many LEDs total
#define BRIGHTNESS  128                  // not too bright, not too dim
#define LED_TYPE    WS2811               // change if needed
#define COLOR_ORDER GRB                  // change if needed
CRGB leds[NUM_LEDS];
byte b[NUM_LEDS][3] = {};

void setup() {
  randomSeed(analogRead(0));
  delay( 300 );                            // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness(  BRIGHTNESS );
  for (int i = 0; i < NUM_LEDS; i++) {
    for (int j = 0; j < 3; j++) {
      b[i][j] = 0;
    }
  }
}

void loop()
{
  int i = random(8);
  byte j = random(3);
  b[i][j] = b[i][j] ^ 255;
    leds[i] = CRGB(b[i][0], b[i][1], b[i][2]);
  FastLED.show();
  delay(100);
}

Seems maybe a tad faster? Since it now only pushes update to one LED not whole strip. However 2D array uses more memory, I risk memory error after 300 LEDs.

When you do a .show, all LEDs are updated with the data in the array.

Try

  int i = random(8);
  byte j = random(3);
  leds[i].raw[j] ^= 255;
  FastLED.show();

If it works, your “b” array is no longer needed.