I'm currently working on a project that uses a LED and photoresisters to compare colors with. I have where you type in the color you want to the arduino to interpret. After typing it in, the program checks the letters to see what parameters there are for the colors.
My question is: is it possible to be able to input three different colors into the serial moniter(example: Red, Green, Blue) and the program do those in order?
How would I go about doing this? I'm still a novice at programming so any advice would be great
What you want to do is called parsing: breaking a string of data into meaningful groups. Hit the search box at the top of this page and search for "parse serial data". You'll find lots of discussion.
Basically, you want to keep reading serial characters in from the serial port, combining them into a string. Keep reading until you get a newline character to signal the end of the input. Then search for the commas, break the string apart, and convert each of the sub-strings to numbers.
Another way is with a state machine: keep reading characters and adding them to a string until you see a comma: convert that string to a number and save it as the red value. Then clear out your string and start reading more characters into it until the next comma: convert that to a number and store as green. Again, clear out the string, start filling it again, and when you get a comma or end of line, convert it to a number and store as blue. Finally, update your LED using the saved red, green, and blue values.
zwood14:
What if it is with actual words instead of numbers?
Then you do a series of if statements with string comparisons to each of your possible commands.
// assume serial command has been read into a String named command, and converted to all lower case
if (command == "run")
Run();
else if (command == "stop")
Stop();
else if (command == ...