help writing struct


I need help. I am trying to compare one list of 150 rgb values with other values coming from a sensor in order to find the closest match. I am having trouble figuring out a way to store the values.

Here is one approach that I tried:

byte A[] = {0,0,0}; byte B[] = {1,1,1}; byte C[] = {1,1,1};

byte r; byte g; byte b;

void setup() { Serial.begin(9600);


byte checkValues(byte r, byte g, byte b, byte address[]) {

byte y;


{ r = (address[y]); g = (address[y]+1); b = (address[y]+2);

return r, g, b; } }

void loop() {

checkValues(r,g,b,A); Serial.print (r); Serial.print (g); Serial.print (b); }

I don't think that this is the best approach because I need to try A, then B, then C etc. (up to 150) and I can't name the arrays 1, 2, 3. I also tried to make a struct, but it wasn't working because I don't know what I am doing.

What is the range of r, g and b values?

are the 150 rgb value related to each other, if so, how?

Can you provide a more precise definition of close - if you can work out how you want the actual comparison to work it may be easier to consider how to store the values

What is it you are you making?

The range of the values from the sensor are 0-60 for R, G and B, but there is also clear, which ranges between 0-110. The R, G and B in the set of 150 values are all between 1-9. The first values are being scaled like this:

R2 = ceil(3 * R * C (clear) )/2200.)

The comparison is done by mapping the rgb from the sensor to a point in 3d space and finding the distance to the closest of 150 values.

d = sqrt((R1-R2)^2 + (G1-G2)^2 + (B1-B2)^2)

There is no relationship between the 150 values and they actually don't really correspond to red green and blue. The color sensor is being used to choose different images to be displayed on an lcd screen.

If you want to make a struct, you'll need to click over on the far right edge of your Arduino editor window and create a new tab. Save it as an *.inc file, then #include "" at the beginning of your main Arduino code.

You have to do this because the Arduino preprocessor tries to prototype all your functions for you, and any function that uses a struct will be prototyped before the struct is ever defined.

You can use bitfields to hold the RGB values to minimize the RAM required. The following fragment needs two bytes per RGB value

typedef struct  {
  unsigned int r        :4 ; // 4 bit values that can range from 0  to 15
  unsigned int g        :4 ;
  unsigned int b        :4 ; 
   } RGB;
   RGB data[] = { {1,2,3}, {4,5,6}, {7,8,9}, { 10,11, 12}};
void setup(){
 Serial.begin (9600);
 for( int i=0; i < 4; i++){
    Serial.print(data[i].r); Serial.print(" ");  Serial.print(data[i].g); Serial.print(" ");  Serial.println(data[i].b); 

There is room for four more bits without increasing the data size so you can add additional fields or increase the rgb range to 5 bits if you want