Coding help required - create a array to store color value

i am using 3 ws2812 led which is taken from a full strip .

So am making a game which shows 3 different colors on the 3 leds for each level and the game will have about 10 levels which means i will have to store 30 values as a pair of 3 for the 3 leds so that i can reference them in my code.

The problem am having is I do not understand how to store these values as color objects. would it be better to store the values in an array as hex code or should i store them as rgb values.

Am weak in programming so an example would be easy for me to catch.

appreciate the help thanks

which library are you using for your ws2812 leds?

they usually offer a type for the Colors. for example in FastLed you have choice between 2 classes:

Classes

struct CHSV
Representation of an HSV pixel (hue, saturation, value (aka brightness)). More...

struct CRGB
Representation of an RGB pixel (Red, Green, Blue) More...

so you would need a 2D array of 30 x 3 elements of one of these types, for example

#include <FastLED.h>

CRGB colors[30][3] = {
  {{ 0,  0,  0}, {10, 10, 10}, {20, 20, 20}},
  {{30, 30, 30}, {40, 40, 40}, {50, 50, 50}},
  // ...
};

my mistake i should have mentioned that am using the fastled library and i think the example you gave will suit my need perfectly ,

so if i need to pull in the value of 2nd row and 3rd column and set it to an led , how would my line look.

colors[1][2]

Remember counting starts from 0.

something like that

#include <FastLED.h>
#define NUM_LEDS 3
#define DATA_PIN 3

const CRGB colors[30][3] = {
  {{ 0,  0,  0}, {10, 10, 10}, {20, 20, 20}},
  {{ 1,  0,  0}, {11, 10, 10}, {21, 20, 20}},
  {{ 2,  0,  0}, {12, 10, 10}, {22, 20, 20}},
  {{ 3,  0,  0}, {13, 10, 10}, {23, 20, 20}},
  {{ 4,  0,  0}, {14, 10, 10}, {24, 20, 20}},
  {{ 5,  0,  0}, {15, 10, 10}, {25, 20, 20}},
  // ...
};

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);  // GRB ordering is typical
  byte activeIndex = 1; // 2nd row of the array
  for (byte i = 0; i < 3; i++) {
    leds[i] = colors[activeIndex][i];
  }
  FastLED.show();
}

void loop() {}

thank you so much, I got what I needed

have fun!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.