Go Down

Topic: Placing arrays into an array (Read 343 times) previous topic - next topic

mrExplore

Sep 16, 2018, 07:46 pm Last Edit: Sep 16, 2018, 08:58 pm by mrExplore
Hi all,

I'm working with arrays to store RGB color values.


I would like to place all color arrays into one array from wich a color can be selected.
Currently I have this array of colors set up like this:

Code: [Select]
// all colors in one array
int arrColors[10][3]={
 
  {255,156,66},     //white
  {255,0,0},            //red
  {0,255,0},            //green
  {0,0,255},            //blue
  {255,110,0},        //yellow
  {255,0,160},        //purple
  {255,255,255},    //lightblue
  {0,255,255},        //turquoise
  {255,43,148},     //pink
  {255,25,0}          //orange
  
};



However, i would like to create arrays with subsets too.
A sub set would be two or three colors to combine in an effect.

Yes I could define every subset array as above but there must be a better way.
I was thinking of something like:


Code: [Select]

// arrays for usable/nice colors
int arrWhite[3] = {255,156,66};
int arrRed[3] = {255,0,0};
int arrGreen[3] = {0,255,0};
.....


// all colors in one array
int arrColors[9][3]={
 
  arrWhite,      
  arrRed,      
  arrGreen,
  arrBlue,
  arrYellow,
  arrPurple,
  arrLightblue,
  arrTurquiose,
  arrPink
  
};

// colors for red white blue flags
int arrRedWhiteBlue[3][3]={
 
  arrRed,      
  arrWhite,  
  arrBlue
  
};

// colors for red yellow green flags
int arrRedYelloGreen[3][3]={
 
  arrRed,      
  arrYellow,  
  arrGreen
  
};



I can't find a way to get this to work :(
Does anyone have an idea on how to go about this ?

Tanks !


LOL, I made a typo, I said Tanks!


arduino_new

You can have an array of struct

Code: [Select]


struct color {
    byte r;
    byte g;
    byte b;
};

color all_colors[] = {{0,0,0}, {0, 1, 2}, {0,1,2}};
int color_size = sizeof(all_colors) / sizeof(all_colors[0]); //size of color


mrExplore

Ehm ok,

I'm not sure if I get this right.

What I would like is easily referable color sets for my code.
I'd like an effect function to accept a set of colors to work with.

This code seems to compile just fine:

Code: [Select]
struct color {
    byte r;
    byte g;
    byte b;
};

color clrRed[] = {255, 0, 0};
color clrGreen[] = {0, 255, 0};
color clrBlue[] = {0, 0, 255};

int arrRGB[3] = {clrRed, clrGreen, clrBlue};


My next question would be how to access the values of R, G and B for elements (color) of arrRGB ?

Code: [Select]
arrRGB[0][1]
arrRGB[0].r

?



Thanks






UKHeliBob

Does this do what you want ?
Code: [Select]

struct color
{
  byte r;
  byte g;
  byte b;
};

color clrRed = {255, 0, 0};
color clrGreen = {0, 255, 0};
color clrBlue = {0, 0, 255};

color arrRGB[3] = {clrRed, clrGreen, clrBlue};

void setup()
{
  Serial.begin(115200);
  Serial.println(arrRGB[0].r);
  Serial.println(arrRGB[1].g);
  Serial.println(arrRGB[2].b);
}

void loop()
{
}
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

arduino_new

#4
Sep 17, 2018, 01:40 am Last Edit: Sep 17, 2018, 02:29 am by arduino_new
Ehm ok,

I'm not sure if I get this right.

What I would like is easily referable color sets for my code.
I'd like an effect function to accept a set of colors to work with.

This code seems to compile just fine:

Code: [Select]
struct color {
    byte r;
    byte g;
    byte b;
};

color clrRed[] = {255, 0, 0};
color clrGreen[] = {0, 255, 0};
color clrBlue[] = {0, 0, 255};

int arrRGB[3] = {clrRed, clrGreen, clrBlue};


My next question would be how to access the values of R, G and B for elements (color) of arrRGB ?

Code: [Select]
arrRGB[0][1]
arrRGB[0].r

?



Thanks


That's certainly not easier, since you can set your struct array at once, you should do it.

Code: [Select]

color all_colors[] = {{255,0,0}, //red
{0, 255, 0}, //green
{0, 0, 255}, // blue
... //more here

};


So all_colors[0] will have the values for red color (255,0,0). And
all_colors[0].r is the red value
all_color[0].b is the blue value
all_color[0].g is the green value

If you declared all_colors as global variable, you can just use it without passing anything to functions.

[/code]



mrExplore

#5
Sep 17, 2018, 04:45 pm Last Edit: Sep 17, 2018, 06:43 pm by mrExplore
Thanks for your replies, this does what I need :)

Also got me reading on structs and typedef, here's a nice intro:
https://www.norwegiancreations.com/2017/10/getting-started-with-programming-part-8-typedef-and-structs/




mrExplore

Now for the next challenge:

What I'm doing is:
Code: [Select]


struct color
{
 int R;
 int G;
 int B;
};

// Define colors
color clrRed = {255,0,0};
color clrGreen = {0,255,0};
color clrBlue = {0,0,255};

// Make an array of colors
color arrRedGreenBlue[] = {clrRed, clrGreen, clrBlue};
int intLengtharrRedGreenBlue = sizeof(arrRedGreenBlue) / sizeof(arrRedGreenBlue[0]);

// Make an array to copy colorsets to
// Colorsets wil be placed into this array, from this array random colors wil be chosen for the effects
color arrCurrentColorset[10];




Now I would like to copy the contents from arrRedGreenBlue into arrCurrentColorset.
And this is where my plan fails :(

Code: [Select]

memcpy( arrCurrentColorset, arrRedGreenBlue, intLengtharrRedGreenBlue );
intLengtharrCurrentColorset = intLengtharrRedGreenBlue;


Does anyone have any idea why ?

Thanks  !
(don't worry, no tanks this time :))

PaulS

Code: [Select]
int intLengtharrRedGreenBlue = sizeof(arrRedGreenBlue) / sizeof(arrRedGreenBlue[0]);

That value is NOT the length of the array. It is the number of elements in the array.

Code: [Select]
memcpy( arrCurrentColorset, arrRedGreenBlue, intLengtharrRedGreenBlue );
That function wants the number of bytes to copy (the size of the array), NOT the number of elements in the array.
The art of getting good answers lies in asking good questions.

mrExplore

#8
Sep 18, 2018, 08:15 pm Last Edit: Sep 18, 2018, 08:16 pm by mrExplore
Ok, I'm just a little bit confused now.

So you say:

length of an array IS NOT number of elements
AND
size of an array IS NOT number of elements

My guess is that you are saying that length and size are both "the number of bytes to copy" ?

So would this work:
Code: [Select]
memcpy( arrCurrentColorset, arrRedGreenBlue, sizeof(intLengtharrRedGreenBlue) );

PaulS

The size of the array, in bytes, is sizeof(array). The number of elements in the array is sizeof(array) / sizeof(array[0]).

The memcpy() function wants the number of bytes to copy, not the number of elements to copy.
The art of getting good answers lies in asking good questions.

mrExplore

Ok, thanks for clearing that up.


Go Up