Interpret serial communication w/ roborealm

Hi,

I am currently busy with programming an arduino in combination with roborealm.
Everything works except interpreting the values Roborealm sends serial to the Arduino.
The values have a range from -160 to 160. Below here information about our code, it’s a default code from the internet which I am trying to understand and eventually make better.

#include <Servo.h> // Include the servo library

Servo myServo; // Create a new servo object
char incomingData[4] = {
  0, 0, 0, 0}; // A buffer to store the ASCII value read in from the serial port
int distance = 0; // The distance of the object from the center of the screen
int currentAngle = 90; // The current angle of the servo
int i = 0; // counter

void setup(){
  Serial.begin(9600); // Open the serial port with a 9600 baud rate
  Serial.println("Serial port ready"); // Print on screen
  myServo.attach(11); // Attach the servo signal line to pin 9
  myServo.write(currentAngle); // Set the starting angle at 90 degrees
}

void loop(){
  // Wait for data to become available at the serial port
  if (Serial.available()){
    // Get the data coming through the serial port and store it in the buffer
    while (i < 4){
      incomingData[i] = Serial.read(); // Assign the input value to the incomingData buffer
      i++; // Increment the counter
    }

    distance = atoi(incomingData); // Convert ASCII to Int

    // If the distance is negative then add 5 to the currentAngle and update the servo, and vise versa
    if (distance < -15){
      currentAngle++;
      currentAngle = constrain(currentAngle, 0, 180); // Constrain the value of currentAngle to within 0-180 degrees
      myServo.write(currentAngle);
    }
    else if (distance > 15){
      currentAngle--;
      currentAngle = constrain(currentAngle, 0, 180); // Constrain the value of currentAngle to within 0-180 degrees
      myServo.write(currentAngle);
    }
  }

  i = 0; // Reset the counter
  //delay(50); // Delay 20ms to allow the servo to rotate to the position
}

I have searched al the top links in Google, searched the forum etc… I have seen people with the same problem and the topic was solved by a commenter. Unfortunately I don’t understand what the commenter means because of my lack of knowledge about Arduino.
Some answers were like; you need to escape the NULL ‘\0’???

The problem is: If I display the value of the integer distance I only see the last number… F.E -145 shows 5 or 32 shows 2.

How can I get the whole number in an integer/long/something similar with the serial communication as source.

Thanks in advance,

Yours Sincerly

~ Suddenbunny

if (Serial.available()){
    // Get the data coming through the serial port and store it in the buffer
    while (i < 4){
      incomingData[i] = Serial.read(); // Assign the input value to the incomingData buffer
      i++; // Increment the counter

The usual problem.
You check to see that you’ve got at least one character available, then you go ahead and read all four of them.

And then pass a non-NULL terminated array of chars to a function that expects a string (a NULL terminated array of chars).

You don't even have room in the array for the NULL.

Okay.. I've checked the raw values and I think I understand.. Sometimes I get -1 etc.

So how would my code look like if I want to check IF there is data available?

You already do check to see if there's data available.
You either need to do that for every character you read, or ou need to check that you've got all the data you expect before reading it.