How to assign predefined array of RGB values to setLED?

Hi there,

I’m wanting to sequence thru randomly (or in sequence) a number of predefined arrays of RGB values (I have orange and magenta - but will have more) but I’m not sure how to call them (arrays) randomly (or in sequence) and then assign them to the function “color” "setLED. Below is my code and the line comment “Not sure what to do here” is where I think I want to place the assignment?

Thanks in advance.

#include "Adafruit_TLC5947.h"

// How many boards do you have chained?
#define NUM_TLC5974 2

#define data   4
#define clock   5
#define latch   6
#define oe  -1  // set to -1 to not use the enable pin (its optional)
const int wait = 50;
const int cycledelay = 200;
const uint16_t orange[] = {1 , 2401 , 4095};
const uint16_t magenta[] = {1245 , 4095 , 1};
Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);

void setup() {
  Serial.begin(9600);
  Serial.println("TLC5974 test");
  tlc.begin();
  if (oe >= 0) {
    pinMode(oe, OUTPUT);
    digitalWrite(oe, LOW);
  }
  //initially turn all leds off
  for (uint16_t i = 0; i < 8 * NUM_TLC5974; i++) {
    tlc.setLED(i, 4095, 4095, 4095);
    tlc.write();
  }
}

void loop() {
  //randomize color choice
  randno = random(14);  // there are 15 RGB leds used in this circuit
  color = //NOT SURE WHAT TO DO HERE
  void color(uint16_t r, uint16_t g, uint16_t b) {
    tlc.setLED(randno, r, g, b);
    tlc.write();
    delay(wait);
  }
}

If you're not sure, I'd say "don't define a function inside another one, the compiler won't like it"

What you need is a struct. Google that.

thanks for that Delta_G...

but I don't think a struct does for me what I need to do. What I need to really do is have a predefined set of R,G,B values that are a struct or an array BUT i need to identify that struct or array numerically so that I can use Random or sequence count thru my various rgb values; - that is I don't need to use the array name orange or magenta - the array name wants to be a numerical value that I can then count or randomize etc. I don't think that I can declare an array by a number value like:

const uint16_t 03[] = {1 , 2401 , 4095}; // 03 represents the color orange

In other words, how can I have an array or struct of RGB values in that I can call on as a value between say 1 and 16 (the number of predefined rub values)?

Then you need an array of structs.

Anytime you're thinking you want to call a variable by number, that's an array that you need.

A struct allows you to put all three of the numbers that represent your color into one single variable.

so I understand that a struct is a way to make a “structure” of values. Is this what you mean?

void setup() {
// declare structure for rgb variable
  struct rgbcolor {
    byte r;    // red value 0 to 4095
    byte g;   // green value
    byte b;   // blue value
 }
// assign 17 predefined colors - not all listed here yet
rgbcolor 1 = { 1 , 2401 , 4095 }; // orange value
rgbcolor 2 = { 1 , 0 , 0 }; // red value
rgbcolor 3 = { 1245 , 4095 , 1 }; // magenta value
};

void loop() {
 ch = random(14);  // randomize led channel to assign color - there are 15 RGB leds used in this circuit
 randomcolor = random(16);
 color = rgbcolor (randomcolor);
  void color(uint16_t r, uint16_t g, uint16_t b) {
    tlc.setLED(ch, r, g, b);
    tlc.write();
    delay(wait);
  }
}

You can’t have numbers for variable names. You need an array of this struct objects.

You also can’t put a function definition in the middle of another function like that. Right now the color function is being defined inside loop.

byte r;    // red value 0 to 4095

You do know what the maximum value in a byte is don’t you? If not you might want to check that out before you go much further.

mbiasotti: so I understand that a struct is a way to make a "structure" of values. Is this what you mean?

void setup() {
// declare structure for rgb variable
  struct rgbcolor {
    byte r;    // red value 0 to 4095
    byte g;   // green value
    byte b;   // blue value
 }
// assign 17 predefined colors - not all listed here yet
rgbcolor 1 = { 1 , 2401 , 4095 }; // orange value
rgbcolor 2 = { 1 , 0 , 0 }; // red value
rgbcolor 3 = { 1245 , 4095 , 1 }; // magenta value
};

Almost.

// declare structure for rgb variable
struct rgbcolor {
  byte r;    // red value 0 to 4095
  byte g;   // green value
  byte b;   // blue value
}
someColors[] = {
 { 1 , 2401 , 4095 }, // orange value
 { 1 , 0 , 0 }, // red value
 { 1245 , 4095 , 1 } // magenta value
};

void loop() {
 int whichColor = random(sizeof(someColors)/sizeof(*someColors));  

}

Hi Paul,

So you are using the total byte size of the pre-assigned color struct to identify the particular color? If so doesn't that make pure red and pure green etc. equal to each other?

The size of the whole array divided by the size of the first element gives a count of how many elements are in the array.

What I need to really do is have a predefined set of R,G,B values that are a struct or an array BUT i need to identify that struct or array numerically

Isn't that a description of a 2 dimensional array?

okay - that makes sense - I'll give it a try. - thanks

not sure why I get an error compiling this sketch? (exit status 1)

/*
** led light sequencer
** written by mark biasotti
*/
#include "Adafruit_TLC5947.h"

// How many boards do you have chained?
#define NUM_TLC5974 2

#define data   4
#define clock   5
#define latch   6
#define oe  -1  // set to -1 to not use the enable pin (its optional)
unsigned long currentTime;
unsigned long loopTime;
int wait = 500;

Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);

void setup()  {
  Serial.begin(9600);
  Serial.println("led light sequence version 1");
  tlc.begin();
  if (oe >= 0) {
    pinMode(oe, OUTPUT);
    digitalWrite(oe, LOW);
  }
  // turn all leds off
  for (uint16_t i = 0; i < 8 * NUM_TLC5974; i++) {
    tlc.setLED(i, 4095, 4095, 4095);
    tlc.write();
    delay(wait);
  }
  // declare structure for rgb variable
  struct rgbcolor {
    byte r;    // red value 0 to 4095
    byte g;   // green value
    byte b;   // blue value
  }
  someColors[] = {
    { 1 , 2401 , 4095 }, // orange value
    { 1 , 4095 , 4095 }, // red value
    { 1245 , 4095 , 1 }, // magenta value
    { 4095, 1, 4095 }     // green value
  };
}

void loop() {
  int whichColor = random(sizeof(someColors) / sizeof(*someColors));
  int whichChannel = random(14);
  tlc.setLED(whichChannel, whichColor);
  tlc.write();
  delay(wait);
}

exit status 1 means that the error caused compilation to terminate before it finished. What was the actual error message? You might have to scroll up in the console window to see it.

I’m going to stick my neck out and say it’s a scope issue with “someColors”

Delta_G - thanks for that. Didn't know that; new t this. The error says: "someColors" not declared in this scope

/*
** led light sequencer
** written by mark biasotti
*/
#include "Adafruit_TLC5947.h"

// How many boards do you have chained?
#define NUM_TLC5974 2

#define data   4
#define clock   5
#define latch   6
#define oe  -1  // set to -1 to not use the enable pin (its optional)
unsigned long currentTime;
unsigned long loopTime;
int wait = 500;

Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);

void setup()  {
  Serial.begin(9600);
  Serial.println("led light sequence version 1");
  tlc.begin();
  if (oe >= 0) {
    pinMode(oe, OUTPUT);
    digitalWrite(oe, LOW);
  }
  // turn all leds off
  for (uint16_t i = 0; i < 8 * NUM_TLC5974; i++) {
    tlc.setLED(i, 4095, 4095, 4095);
    tlc.write();
    delay(wait);
  }
}
// declare structure for rgb variable
struct rgbcolor {
  byte r;    // red value 0 to 4095
  byte g;   // green value
  byte b;   // blue value
} someColors[] = {
  { 1 , 2401 , 4095 }, // orange value
  { 1 , 4095 , 4095 }, // red value
  { 1245 , 4095 , 1 }, // magenta value
  { 4095, 1, 4095 }     // green value
};

void loop() {
  int whichColor = random(sizeof(someColors) / sizeof(*someColors));
  int whichChannel = random(14);
  tlc.setLED(whichChannel, whichColor);
  tlc.write();
  delay(wait);
}

(uncompiled, untested, but suspect missing an index now)

Okay when running this I now get this error when compiling:

C:\Users\mbiasotti\Documents\Arduino\libraries\Adafruit_TLC5947-master/Adafruit_TLC5947.h:31:8: note: candidate expects 4 arguments, 2 provided

exit status 1 no matching function for call to 'Adafruit_TLC5947::setLED(int&, int&)'

It looks like the line: tlc.setLED(whichChannel, whichColor) is not the correct syntax for the tlc.setLED to handle?

Mark

From one of your earlier posts tlc.setLED(randno, r, g, b);

Feel like I’m getting really close now. I’ve used my serial monitor to print out whichColor and whichChannel and I’m getting 0-3 for my whichColor which is correct (since I only have 4 defined so far) and whichChannel is showing 0-14. Now that I have the color identified by a value, all I need to do is somehow past that to tlc.setLED. Code below to verify random values are being sent.

/*
** led light sequencer
** written by mark biasotti
** rgb fully on is 0 fully off is 4095 - because I'm using common anode leds and pulling pin high.
**/ 
#include "Adafruit_TLC5947.h"

// How many boards do you have chained?
#define NUM_TLC5974 2

#define data   4
#define clock   5
#define latch   6
#define oe  -1  // set to -1 to not use the enable pin (its optional)
unsigned long currentTime;
unsigned long loopTime;
int wait = 500;

Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);

void setup()  {
  Serial.begin(9600);
  Serial.println("led light sequence version 1");
  tlc.begin();
  if (oe >= 0) {
    pinMode(oe, OUTPUT);
    digitalWrite(oe, LOW);
  }
  // turn all leds off
  for (uint16_t i = 0; i < 8 * NUM_TLC5974; i++) {
    tlc.setLED(i, 4095, 4095, 4095);
    tlc.write();
    delay(wait);
  }
}
// declare structure for rgb variable
struct rgbcolor {
  byte r;    // red value 0 to 4095
  byte g;   // green value
  byte b;   // blue value
} someColors[] = {
  { 801 , 1 , 3100 },   // white
  { 1 , 1 , 3600 },     // warm white
  { 1 , 4095 , 4095 },  // red
  { 4095 , 1 , 4095 },  // green
  { 4095 , 4095 , 1 },  // blue
  { 1 , 4095 , 4095 },  // magenta
  { 4095 , 1 , 2201 },  // cyan
  { 695, 1, 4095 },    // yellow
  { 1 , 2401 , 4095 },  // orange 
  { 4095 , 1 , 1951 },  // lt-blue
  { 4095 , 1 , 3301 },  // aqua
  { 1 , 4095 , 2601 }   // pink
};

void loop() {
  int whichColor = random(sizeof(someColors) / sizeof(*someColors));
  int whichChannel = random(15);

// tlc.setLED(whichChannel, rgbcolor(whichColor));
//tlc.write();
Serial.print(whichChannel);
Serial.print ("    ");
Serial.println (whichColor);
delay(wait);
}