Problem i face in serial communication

Hi, i am learning the serial communication by using the Arduino collect the data and use the processing to do a GUI to display the data. Here is my Arduino code and Processing code.

Arduino

#include <Servo.h>
Servo myservo;

const int trig = 11;
const int echo = 12;
float duration, distance;

void setup() {
  myservo.attach(9);
  
  pinMode(trig,OUTPUT);
  pinMode(echo,INPUT);
  
  Serial.begin(9600);
}

void loop() {
  for (int i=0; i<=180;i++){
    myservo.write(i);

    digitalWrite(trig,LOW);
    delay(2);
    digitalWrite(trig,HIGH);
    delay(20);
    digitalWrite(trig,LOW);
    duration = pulseIn(echo,HIGH);
    distance = (duration*0.034)/2;

    Serial.print(distance,2);
    Serial.print(",");
    Serial.print(myservo.read());
    Serial.print("\n"); 
    delay(30);
  }

 for (int i=180;i>=0;i--){
    myservo.write(i);

    digitalWrite(trig,LOW);
    delay(2);
    digitalWrite(trig,HIGH);
    delay(20);
    digitalWrite(trig,LOW);
    duration = pulseIn(echo,HIGH);
    distance = (duration*0.034)/2;

    Serial.print(distance,2);
    Serial.print(",");
    Serial.print(myservo.read());
    Serial.print("\n");
    delay(30);
  }


  
}

Processing

import processing.serial.*;

Serial myPort;
float distance,degree;

void setup(){
  //fullScreen();
  myPort = new Serial(this,"COM4",9600);
}

void draw(){
  if (myPort.available() > 0){
    String dis_and_deg = myPort.readStringUntil('\n');
    //print(dis_and_deg);
    String[] list = split(dis_and_deg,',')
    distance = float(list[0]);
    degree = float(list[1]);
    print(distance + " cm " + degree + " degree \n"); 
  }
 

}

I cant understand why i change my processing code like below and then the degree will became 0 can someone help me to explain how it happen?

import processing.serial.*;

Serial myPort;
float distance;
int degree;

void setup(){
  //fullScreen();
  myPort = new Serial(this,"COM4",9600);
}

void draw(){
  if (myPort.available() > 0){
    String dis_and_deg = myPort.readStringUntil('\n'); // cant use readString() because once used readString(), the parameter of split(value,delimiter) got 2 delimiter ("," and "\n") since there is two delimieter therefore error.
    //print(dis_and_deg); // Check the string is correct or not.
    String[] list = split(dis_and_deg,',');  // also can try substring() to split the string.
    distance = float(list[0]);
    degree = int(list[1]);
    print(distance + " cm " + degree + " degree \n"); // Check the variable distance and degree correct or not.
  }
 

}

Capture

1 Like

Hello,

You receive a string which includes a newline character ( ‘\n’ )

You need to remove the newline character ( ‘\n’ ) that is in the received string before splitting it so you can convert to an int.

Example:

https://processing.org/reference/trim_.html
https://processing.org/reference/String.html (discusses newline character and escape sequences such as \n)

:slight_smile:

String s1 = "123,456,890\n";
String [] list1 = split(s1, ',');

printArray(list1);
println(int(list1[2]));
println();

String s2 = "123,456,890\n";
s2 = trim(s2);

String [] list2 = split(s2, ',');

printArray(list2);
println(int(list2[2]));
println();

3 Likes

Thank!!! It is useful!! But i still got some question on it.

  1. Since i receive a string include ‘\n’, why the program run normal if the data type of my program is
    float?
  2. When i change it became readString() it became error, i don know why, can you help me explain
    about it?
String dis_and_deg = myPort.readStringUntil('\n');

Hello,

Yes, it does seem to work work with a float followed by a \n and does not require a trim().

I would have to look at source code to see why… another day perhaps.

String data = "1.1,2.1\n";
//data = trim(data); 

String[] list = split(data,',');  

float dis1 = float(list[0]);
float deg1 = float(list[1]);

println(dis1, deg1);

//********************

String data2 = "1.1,2.1\n";
data2 = trim(data2); 

String[] list2 = split(data2,',');

float dis2 = float(list2[0]);
int deg2 = int(list2[1]);

println(dis2, deg2);

//********************

String data3 = "11,22\n";
//data3 = trim(data3);          //Trim is needed otherwise it displays 0!

String[] list3 = split(data3,',');  

int dis3 = int(list3[0]);
int deg3 = int(list3[1]);

println(dis3, deg3);

//********************

String data4 = "11,22";

String[] list4 = split(data4,',');  

int dis4 = int(list4[0]);
int deg4 = int(list4[1]);

println(dis4, deg4);

Interesting… requires further exploration.
In the meantime, you should trim() String as good practice before converting to int or float.

:slight_smile:

1 Like

Further examination of this:

//https://www.baeldung.com/java-check-string-number

println("123");
println(int("123"));
println(int("123"));
println(Integer.parseInt("123")); 
println(int("3\n"));
println(parseInt("123\n"));
//println(Integer.parseInt("123\n")); 
// This gives error:
// NumberFormatException: For input string: "123
// "

println("3.14");
println(float("3.14"));
println(float("3.14\n"));
println(parseFloat("3.14\n"));
println(Float.parseFloat("3.14\n"));

Output:
123
123
123
123
0
0
3.14
3.14
3.14
3.14
3.14

:slight_smile:

2 Likes

Thank you very much for helping me :joy: :joy: :joy:

1 Like