Read Variables from serial port

As the title says i am trying to read a serial stream with the arduino. For Example

415, 112, 1024, 054, 10011101
786, 549, 0947, 622, 00111100

The above example shows 4 variables I want to use for my calculations. For example the voltage read by a sensor that i want to show remotely.

The last set of digits should make the arduino inputs high or low accordingly, for example, to turn a light on or off.

I know how to print these, but have no idea how to process them at the receiver. Can someone guide me on the right direction?

Many Thanks

Start by defining an array (static or global) that will store the each comma delimited value, and an index to keep track of where you are in it:

char buffer[10];
int buf_i= 0;

On each pass of the loop, check for available serial data and read the byte:

if (Serial.available() > 0)
  char inByte =;
  // do stuff with in byte

Now, you want to check for a comma to see if you are finished reading a number:

if (inByte == ',')
    // A complete value!

When you receive a complete value, you can send your buffer through atoi() to pull out the int represented by the string:

int num = atoi(buffer);

If it's not a comma, check for a new line:

else if (inByte == '\n')

If it's a new line, then you've finished with your last entry and you can traverse through it, checking if the value is '1' or '0' and do your actions accordingly.

If it's not a comma or new line, then it should be put in our buffer:

  buffer[buf_i] = inByte;
  bufer[buf_i] = '\0'; // null terminate to work with atoi()

In both the comma and new line case, you should reset your buffer:

memset(buffer, 0, 10);

The below code is for servo control, but it contains some methods that might be useful for your project (capture data from serial port, convert characters into numbers, identify which application the captured data is to be used for, etc.).

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

void setup() {

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c =;  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
          Serial.print("writing Microseconds: ");
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
          Serial.print("writing Angle: ");
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
         readString=""; //clears variable for new input
    else {     
      readString += c; //makes the string readString