I plan to have the arduino control 2 water valves. Was planning on having it controlled by a computer so it would communicate through serial but I'm having a little problem trying to figure out how to implement that.
For example I would like the computer to send it a command like "valve1,2;valve2,2"
The first part is saying which valve "valve1" and the second part "2" would be how many seconds to leave the valve open. then the ";" would seperate if there is more then 1 valve in the command.
Only thing is im not sure how I would seperate "valve1,2" and "valve2,2". Is there any type of Split() function or what would be a better way to implement this?
Parsing serial string data is such a common request here, I'm surprised you haven't found anything by a simple search on the forum or using google. As such, I will leave it to you to find further examples, but I will ask why you think you need to have it all on one line? Have you thought about a possible structure of commands sent via serial like:
begin
valve1,2
valve2,2
end
go
There are many other strategies as well; this is but one possibility. Something also to think about: What happens if there is noise on the serial data line, and instead of "valve1,2", the Arduino sees "?alvxe1;j"? Have you thought about how you intend to deal with that?
Parsing is done in different ways, depending on whether you are parsing String objects or char arrays. Char arrays can be parsed using strtok. Strings can be parsed using indexOf and substring methods.
One way to avoid the complexities of parsing is to use Bitlash as the framework for your sketch. Bitlash is an interpreter for Arduino; you can get it at http://bitlash.net. You can organize your application as a set of small Bitlash macros and drive commands to run them over the serial port. The interpreter does the parsing.
Make life easy for yourself and make the command syntax use fixed length fields.
E.g.
v 1 05
spacespace
If duration is always less than 10 seconds, then just use one digit. You can use the ch - '0' trick to turn the char into an int.
You are in charge of what's sent, so no need to worry about trimming white space etc. Just make sure your PC app formulates the messages correctly. They only need to be human readable for debugging.
You don't need to capture the whole string in a buffer, just read a char at a time from Serial.