system
October 5, 2014, 11:13am
1
Hi, I've only started working on working with Arduino and I have been wondering if it is possible to change a string with multiple variables to intergers. I'm trying to control the colors of a Neopixel strip and instead of creating multiple arrays for each of the RGB, I would like to have it in their sets. Is that possible? Below is what I want to do. I do understand that the code below returns with an error.
String colors[] = {"255,0,0"};
chase(strip.Color(colors[0])); // Red
you need to split the String in parts - the numbers are separated by comma's
every separate part can be converted to int by themselves.
system
October 5, 2014, 12:39pm
3
robtillaart:
you need to split the String in parts - the numbers are separated by comma's
every separate part can be converted to int by themselves.
In which case, storing the data as Strings that you need to convert to string to convert to ints makes NO sense.
KenF
October 5, 2014, 12:56pm
5
Why not just use an integer array in the first place
eg
int colours[]={255,0,0};
or you could even save several predefined colours such as
int colours[][3]={255,0,0, //red
0,255,0, //green
0,0,255, //blue
255,255,0, //yellow
128,128,128//grey
};
Then to find the RGB values for your chosen colour you'd use
red=colours[selectedColour][0];
green=colours[selectedColour][1];
blue=colours[selectedColour][2];
system
October 5, 2014, 6:27pm
7
KenF:
Why not just use an integer array in the first place
eg
int colours[]={255,0,0};
or you could even save several predefined colours such as
int colours[][3]={255,0,0, //red
0,255,0, //green
0,0,255, //blue
255,255,0, //yellow
128,128,128//grey
};
Then to find the RGB values for your chosen colour you'd use
red=colours[selectedColour][0];
green=colours[selectedColour][1];
blue=colours[selectedColour][2];
I've already considered this one, but I was thinking if there was any shorter way of doing it. I guess not.
Thanks for the reply.
Appreciate the help.
KenF
October 5, 2014, 6:59pm
8
There is but it gets slightly less readable. You could describe the array as follows
unsigned long colours[]={16711680ul, //red
65280ul, //green
255ul, //blue
16776960ul,//yellow
8421504,//grey
103745 //a bit like digestive biscuit
};
Then to get all of the RGB factors into a byte array, you simply use
byte *rgb=(byte *)&colours[selectedColor];
//rgb[1] now has red factor
//rgb[2] now has green factor
//rgb[3] now has blue factor
maybe hexadecimal notation?
0x00FF0000
0x0000FF00
system
October 5, 2014, 7:08pm
10
unfortunately I'm still green in the coding language so I can mostly do colors only through the basic method. Hexadecimal or any other is very alien to me, but I would love to learn it to simplify it.
an example to explain. (otherwise google is your friend.
255, 0, 0 => 255256 256 + 0 * 256 + 0 = 16711680uL == 0x00FF0000
0x00FF0000 == 00 FF 00 00 == 00 255 00 00