# Problem i face in serial communication

**URL:** <https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [January 11, 2020, 2:34pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023 "2020-01-11T14:34:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyjian](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@Cyjian](https://discourse.processing.org/u/Cyjian)\
**Post date:** [January 11, 2020, 2:34pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/1 "2020-01-11T14:34:08Z")

</div>

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

```auto
#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

```auto
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?

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a945d2dd19508b39b3e5c6b9fa21cb7cfe299c2e.jpeg)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2020, 3:35pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/2 "2020-01-11T15:35:17Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b9b172a53e78ef1673e42d36421bcb8b0265be9e.png)

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

🙂

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![Cyjian](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@Cyjian](https://discourse.processing.org/u/Cyjian)\
**Post date:** [January 11, 2020, 4:44pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/3 "2020-01-11T16:44:40Z")

</div>

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?

```auto
String dis_and_deg = myPort.readStringUntil('\n');

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2020, 5:29pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/4 "2020-01-11T17:29:14Z")

</div>

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.

```auto
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);

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2894d0c69a28f80a23c456a813a1b16946b1df80.png)

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

🙂

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 11, 2020, 6:46pm UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/5 "2020-01-11T18:46:21Z")

</div>

Further examination of this:

```auto
//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

```

🙂

---

<div class="post-metadata">

**Author:** ![Cyjian](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@Cyjian](https://discourse.processing.org/u/Cyjian)\
**Post date:** [January 12, 2020, 3:27am UTC](https://discourse.processing.org/t/problem-i-face-in-serial-communication/17023/6 "2020-01-12T03:27:59Z")

</div>

Thank you very much for helping me 😂 😂 😂
