Variable array name, followed by a fixed index

For my project, I need to have values from different arrays written to leds. The problem is that the array names have to be variable, but the array index can be a fixed number. What I have now looks like this.

String presetColor = "red";

int red[3] = {255, 0, 0};
int gre[3] = {0, 255, 0};
int blu[3] = {0, 0, 255};

---

analogWrite(redLed, (presetColor)[0]);
analogWrite(greenLed, (presetColor)[1]);
analogWrite(blueLed, (presetColor)[2]);

This means I would like to display the separate values of red in this case, but the presetColor can change. For some reason, this doesn't work. I don't get any error messages, but in my case only the blue led is on, whatever the variable is set to.

Another thing in the same code makes it even harder. It looks like this at this moment:

int red[3] = {255, 0, 0};
String pre1[3] = {"red", "ora", "yel"}; // This array holds the names of the above mentioned colors
presetArray = 0;

analogWrite(redLed, (pre1[presetArray])[0]); // at some point in time, I want to display the color red

This means I need to first select the variable which holds this color, then select the index, and take the index of that array, so in the end a have the first value of red[3] which is 255.

I need all this to be variable because in the end it will make things much easier.

Thanks in advance!

In C/C++ you cannot select variables by name while your program is running. The names only exist for the compiler which replaces them with memory addresses.

What you could do is make a 2 dimensional array

byte colours[3][3] = {{255, 0, 0},{0, 255, 0},{0, 0, 255}};

and a series of variables

byte red = 0;
byte gre = 1;
byte blu = 1;

and then your program could refer to the red colour with colours[red];

I hope I have all the syntax details correct.

...R

Robin2:

byte red = 0;

byte gre = 1;
byte blu = 1;




and then your program could refer to the red colour with `colours[red];`

I guess the "colours [ r e d ]" should look like this? Thanks anyway, will definitely try this out!

Or use struct syntax, like colours.red colours.blue etc...

notBramKorsten:
What I have now looks like this.

Totally strange.

“String” is the death of every non-trivial program: Wastes RAM, fragments RAM, uses runtime for nothing.

You’d better NOT use keyword ‘String’ for data type in any case with any 8-bit Atmega and limited RAM!

But back to your problem.

In C/C++ you can use pointers. Such like pointers to integers or pointers to integer arrays:

int red[3] = {255, 0, 0};
int gre[3] = {0, 255, 0};
int blu[3] = {0, 0, 255};
int *presetColor = red;  // declare a pointer and make it point to the 'red' array initially.

You then can access the elements of the array such like for printing on Serial:

for (int i=0;i<3;i++) Serial.println(presetColor[i]);

BTW: Did I already mention: “String” is the worst data type for 8-bit Atmega controllers, better do not use!

jurs:
“String” is the death of every non-trivial program: Wastes RAM, fragments RAM, uses runtime for nothing.

You’re absolutely right, will try to remove these asap.

jurs:
In C/C++ you can use pointers. Such like pointers to integers or pointers to integer arrays:

int red[3] = {255, 0, 0};

int gre[3] = {0, 255, 0};
int blu[3] = {0, 0, 255};
int *presetColor = red;  // declare a pointer and make it point to the ‘red’ array initially.




You then can access the elements of the array such like for printing on Serial:


for (int i=0;i<3;i++) Serial.println(presetColor[i]);

Thank you!
I did indeed fix the problem by using pointers. The code works fine now. This also means I removed that String which seems good to me :slight_smile:

jurs:
BTW: Did I already mention: “String” is the worst data type for 8-bit Atmega controllers, better do not use!

Yes you did :wink:

Hello and welcome,

Here is an example using a struct, as suggested by aarg

struct Color { uint8_t r, g, b; };

Color
	red   = { 255,   0,   0 },
	green = {   0, 255,   0 },
	blue  = {   0,   0, 255 }
;

Color presetColor = red;

analogWrite( redLed,   presetColor.r );
analogWrite( greenLed, presetColor.g );
analogWrite( blueLed,  presetColor.b );

notBramKorsten:
I guess the "colours [ r e d ]" should look like this? Thanks anyway, will definitely try this out!

I'm not sure what you mean.

The way it appears in Reply #1 is how I intended it to appear.

Have I made a mistake?

...R

After rereading your post I've decided to delete this response for now.

Robin2:
I’m not sure what you mean.

The way it appears in Reply #1 is how I intended it to appear.

Have I made a mistake?

…R

My fault, when I saw it your post was not using code blocks yet, so it turned [ r e d ] to the color red so it was invisible.

Thanks for the help, the program seems to work now. the only thing i’m struggling with is to set the pointer to another color somewhere in the code. Does anyone know how to do this?

Post your current code and we'd be in a better position to do so