Parsing serial data to int variables

Hey Guys,

Been toiling with this almost constantly for 48 hrs.
I've come a long way! But I'm going a bit mad and I would really appreciate a bit of help.

I'm using touch designer to send a string via serial to the arduino

The code in touch is

n=op('chopto1')
w=n[0,0]
x=n[0,1]
y=n[0,2]
z=n[0,3]
op('serial1').send("%d" %w,"%d" %x,"%d" %y, "%d" %z , terminator="\n")

This should deliver the string 32,73,18,99 to the arduino running

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

int start = 0;
int pad1 = 0;
int pad2 = 0;
int pad3 = 0;

String lights[4];

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    ParseSerialData();
    Serial.println(start);
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}

void ParseSerialData() {
int padNum = 0; 
int q = 0;
for (int i = 0; i < inputString.length(); i++){
if (inputString.substring(i, i+1) == ","){
lights[padNum] = inputString.substring(q,i);
padNum ++;
q = i + 1;
}

if (i == inputString.length() - 1 ){
  lights[padNum] = inputString.substring(q,i);
}

padNum = 0;
q = 0;
start = lights[0].toInt();
pad1 = lights[1].toInt();
pad2 = lights[2].toInt();
pad3 = lights[3].toInt();

}
}

Which as far as I can tell should return the first value in the inputString (32) to touch design via the Serial.println(start)

When I run the script in touch i get all 4 values printed back on separate lines, can anyone see the problem? Thanks

Alex

string_to_int_vars_parse.ino (1.54 KB)

Please read #7 below on how to post code:

http://forum.arduino.cc/index.php/topic,148850.0.html

ah, yeah my bad, thanks

I think you're over-thinking this. First, the String class does have a lot of convenience and power, but rarely do you need both. As a result, the String class bloats your program far above what it needs to be. Use char arrays as C strings instead. You code compiles to 4662 bytes. The code below does the same thing (I think):

char inputString[200];         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete
int pad[4];

char *lights[4];

void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
 // inputString.reserve(200);
}

void loop() {
  int charsRead;
  int i;
  char *ptr;
  
  // print the string when a newline arrives:

                                                            //  Test data: 32,73,18,99
  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', inputString, sizeof(inputString) - 1);
    inputString[charsRead] = '\0';
    i = 0;
    ptr = strtok(inputString, ",");
    while (ptr) {
      lights[i] = ptr;
      pad[i] = atoi(lights[i]);
      Serial.print(pad[i]);
      Serial.print("   ");
      i++;
      ptr = strtok(NULL, ","); 
    }
  }
 
}

and compiles to 2606 bytes. I used the test string shown in the code and pressed the Enter key on the Serial monitor to simulate the input data.

When I run the script in touch i get all 4 values printed back on separate lines, can anyone see the problem? Thanks

Notice the difference in the two lines of code below. In "println" the ln will add a cr/lf to the printing, making the prints appear on seperate lines. Remove the ln and the prints should be on the same line.

Serial.println(start);

Serial.print(start);

drkovorkian:
This should deliver the string 32,73,18,99 to the arduino running

Have a look at Serial Input Basics - especially the parse example.

...R