Unpacking serial data from arduino

Can someone help me understand what is going on here. I am receiving serial data in the format “angle,sonar distance,ir distance.”. My code works with the first 2 variables. I am trying to add the third. I do not understand the syntax for this code. Do I need an index2 or index1+1? what is the proper method?

///////////////////////////////////////////////////
  index1 = val.indexOf(","); // find the character ',' and puts it into the variable "index1"
  angle= val.substring(0, index1); // read the val from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
  sonar_distance= val.substring(index1+1, val.length()); // read the val from position "index1" to the end of the val pr thats the value of the distance
 //ir_distance= val.substring(index2+1, val.length());
  // converts the String variables into Integer
  iAngle = int(angle);
  isonar_distance = int(sonar_distance); 
}

1 Like

This is a way to split chunk of data based on the comma (,) as separator. You can revamp the code above by using the split() function as documented in the reference.

If you still have issues, make sure you print and you inspect the content of val and used debugging tools available with processing to understand what is going on. If the issue persists, provide a snippet on the arduino code as well as your updated Processing code.

Kf

1 Like
void loop()
{
  if (Serial.available() > 0) { // If data is available to read,
    val = Serial.read(); // read it and store it in val

    buttons(val);
    //delay(scanspeed);
    // Serial.println(scanspeed);
  }
  else {


    for (int i = minbrg; i <= maxbrg; i++)
    {
      val = Serial.read(); // read it and store it in val
      buttons(val);

      servo_1.write(i);
      servo_2.write(j);
      //Serial.println(scanspeed);
      delay(scanspeed);


      //delay(5);
      sonar_distance = calculatesonar_distance();
      Serial.print(i);
      Serial.print(",");
      Serial.print(sonar_distance);
     // Serial.print(",");
     // IR_Range();
     // Serial.println(ir_range);
      Serial.println(".");
    }

    for (int i = maxbrg; i > minbrg; i--)
    {
      val = Serial.read(); // read it and store it in val
      buttons(val);

      //int i = 180;
      servo_1.write(i);
      servo_2.write(j);
      delay(scanspeed);

      // delay(5);
      sonar_distance = calculatesonar_distance();
      Serial.print(i);
      Serial.print(",");
      Serial.print(sonar_distance);
     // Serial.print(",");
     // IR_Range();
     // Serial.println(ir_range);
      Serial.println(".");
    }




    // Serial.println("Hello, world!"); //send back a hello world
    //Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)

    delay(scanspeed);
    //  delay(10);

  }
}

so does this" index1 = val.indexOf(",");" find the first comma or all of them?

Yes, the code you provide manipulates the string by extracting the comma position and splitting the string. You can check any the docs for String indexOf to find a full explanation of this function.

sonar_distance= val.substring(index1+1, val.length()); is actually taking the value after the comma to the end of the string. Sonar distance will contain the last two values (out of three). If you want to use this algorithm, one approach is to detect the comma and split this value into sonar distance and ir distance.

I encourage you to use more flexible approaches as it is easier to read, easier to maintain, easier to update and in short, makes your life easier. If you want to do character manipulation, then I find it is easier to take pen and paper and figure out the algorithm you need and then implement the code.

String angle, sonarDista, irDista;
String[] rawData = val.split(',');

if(rawData.size()==3){  //Expecting three values
  angle = rawData[0];
  sonarDista = rawData[1];
  irDista = rawData[2];
}

Kf

3 Likes

possibly in case there is a spare last “,” better

if(rawData.length >= 3){

The method split(String) in the type String is not applicable for the arguments (char)

where is the argument declared char?

void serialEvent(Serial p) { //___________________ handle serial data
  String data = trim(p.readStringUntil('\n'));
  if (data != null) {

String[] datas = val.split(data, ",") ;

or

int[] datai = int(val.split(data, ",") );
    if ( datai.length >=3 ) {
//...
    }
  }
}

oh, but "," or ',' should be same but ' , ' does not work

2 Likes

ok, this is cleaner and easier to understand. but I am still getting an error.

Cannot invoke size() on the array type String

void serialEvent( Serial port) {

//the '.' is our end delimiter indicating the end of a complete packet

 String val = trim(port.readStringUntil('.'));

//make sure our data isn't empty before continuing

if (val != null) {

String i, sonar_distance, ir_distance;

String[] rawData = val.split(",");

if(rawData.size()>=3){  //Expecting three values//error=Cannot invoke size() on the array type String[]

  i = rawData[0];

  sonar_distance = rawData[1];

  ir_distance = rawData[2];

}

see my example:

datai.length >=3

ArrayList .size()
Array .length
String .length()

JAVA ? english ? :exploding_head:

1 Like

yup just found

if(rawData.length >=3){  //Expecting three values

that makes it happy thanks for your help

1 Like

Have a look on my simple Sketch.
Seriallisation is finished and I’ll sent a linefeed.
Don’t forget to add ReadSerial.pde to your sketch.

2 Likes