title: Issues interfacing Arduino sensor MPU6050 and Processing
body: <p>I have an MPU6050 gyroscope and accelerometer connected with Arduino through the I2C protocol. These sensor send a continuos stream of data into the Serial port with these instructions (in the arduino IDE):</p>
  
  <pre><code>Serial.print(euler[0] * 180/M_PI);
  Serial.print(":");
  Serial.print(euler[1] * 180/M_PI);
  Serial.print(":");
  Serial.println(euler[2] * 180/M_PI);
  </code></pre>
  
  <p>This come from an example sketch included in the library of the sensor and it just sends the value of yaw / pitch / roll to the serial port, separated by the colon. </p>
  
  <p>Now there is the interesting part. I've always been fascinated about visualizing data, and so i want to build a sort of graph of these data coming from serial in Processing(this is part of a larger project that include a ultrasonic sensor, like a sort of radar).</p>
  
  <p>So I wrote a short sketch on processing to catch that data in order to analyze and visualize it. This is the sketch:</p>
  
  <pre><code>import processing.serial.*;
  
  Serial myPort;
  
  String data; //Angle values
  
  String[] splitted; //Array containing splitted data
  
  float yaw, pitch , roll;
  
  
  
  void setup()
  {
    myPort = new Serial (this, Serial.list()[0], 115200);
  
  }
  
  void draw()
  {
  
    while (myPort.available() &gt; 0) //data arrived fromm serial
    {
  
      data = myPort.readStringUntil('\n');
  
      //Data Parsing
      splitted = data.split(":");
  
      yaw = float(splitted[0]);
      pitch = float(splitted[1]);
      roll = float(splitted[2]);
  
      println(yaw + "  " + pitch + "  " + roll);
  
  
    }
  
  
  
  }
  </code></pre>
  
  <p>This code doesn't work. There are 2 errors that alternate. One of them is:</p>
  
  <pre><code>ArrayIndexOutOfBondsException
  </code></pre>
  
  <p>And the other one:</p>
  
  <pre><code>NullPointerException
  </code></pre>
  
  <p>That points to the "splitted" array.</p>
  
  <p>I think i got the problem. In a previous version of the Processing sketch i was using the:</p>
  
  <pre><code>readString() function
  </code></pre>
  
  <p>I think that, since data are sent to the Serial port in the Arduino sketch one at a time, the processing sketch sometimes catch only one, or two of the yaw, pitch, roll values, causing the array index to crash or to a nullPointerexception when no value is added to the array. I then changed the '''readString''' to the '''readStringUntil('\n')''', because, maybe the first packet will be lost but the other one coming next will always be cathed without breaking them (i catch the entire line). But there are the same errors, so I think my small experience can't help solving the problem anymore. I need your help.</p>
  
  <p>Excuse me for my bad English, and thanks for any help.</p>