I’ve coded a lot of arduinos and ESP8266 gizmos but now I seem to be up against the limits of my understanding of C++.
I’m playing with the neopixels and I have a series of arrays which define the various colors. They look like this:
int black[3] = { 0, 0, 0 };
int hotwhite[3] = { 80, 100, 100 };
int white[3] = { 50, 50, 50 };
int dimwhite[3] = { 10, 10, 10 };
int red[3] = { 80, 0, 0 };
int dimred[3] = { 10, 0, 0 };
// ...
I have a serial input which captures the names of colors like “red”, “blue”, “dimgreen”, etc. into a variable called, serialCommand.
If the code sees a value of “red” in the String variable, serialCommand, it runs a method which takes the name of the array element to set my neopixels to that color. These calls look like this:
if (serialCommand.equalsIgnoreCase("black")) {setLedColor(black); }
if (serialCommand.equalsIgnoreCase("white")) {setLedColor(white); }
if (serialCommand.equalsIgnoreCase("red")) {setLedColor(red);}
// ...
If I have 20 colors, then I write 20 individual lines to set the color. That seems wasteful and redundant.
What I would prefer is a single command that looks something like this:
setLedColor(<to the serialCommand value>);
The code I have works fine so this is not a critical situation. I’m just trying to see how others might do this in a more elegant fashion.
I found a reference to using a hashmap but it doesn’t seem to work with arrays and I’m not smart enough to extend that library.
Thanks for any tips,
Chris.