Error arduino processing codes

Hello processing community, for my first program with processing i have an error when reading the datas from the IMU that i’m coding when i convert my String from the serial port into float values. The error is “ArrayIndexOutOfBoundsException” but not always at the same value. I think that comes from an overlap of the data but not sure that’s why i need your help! (it is not for a drone but to analyse animal behaviour).
Arduino code :

#include <SPI.h> 
#include <Wire.h>
#include <SFE_LSM9DS0.h>

#define LSM9DS0_XM  0x1D 
#define LSM9DS0_G   0x6B

#define PRINT_CALCULATED
#define PRINT_SPEED 1000

#define BT_SERIAL_TX_DIO 10 
#define BT_SERIAL_RX_DIO 11

LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);



void setup() {
  
  Serial.begin(9600);

  
  uint16_t status = dof.begin();
  //int compteur=0;
  /*Serial.print("LSM9DS0 WHO_AM_I's returned: 0x");
  Serial.println(status, HEX);
  Serial.println("Should be 0x49D4");
  Serial.println();*/
 /*Serial.println("Gx;Gy;Gz;Ax;Ay;Az;Mx;My;Mz;H;P;R;");*/
}

void loop() {
  printGyro();
  //Serial.print("\n");  
  printAccel(); 
 //Serial.print("\n"); 
  printMag();  
  //Serial.print("\n");


 


  printHeading((float) dof.mx, (float) dof.my);
  //Serial.print("\n"); 
  printOrientation(dof.calcAccel(dof.ax), dof.calcAccel(dof.ay), 
                   dof.calcAccel(dof.az));
 
 
  Serial.println();

  delay(5000);
}

void printGyro()
{
  dof.readGyro();

  /*Serial.print("G: ");*/
  
  #ifdef PRINT_CALCULATED
  
  Serial.print(dof.calcGyro(dof.gx), 2);
  Serial.print(";");
  Serial.print(dof.calcGyro(dof.gy), 2);
  Serial.print(";");
  Serial.print(dof.calcGyro(dof.gz), 2);
#elif defined PRINT_RAW

  Serial.print(dof.gx);
  Serial.print(", ");
  Serial.print(dof.gy);
  Serial.print(", ");
  Serial.println(dof.gz );
  
#endif
}

void printAccel()
{
  dof.readAccel();

  Serial.print(";");

  /*Serial.print(" A: ");*/
  
  #ifdef PRINT_CALCULATED
  
  Serial.print(dof.calcAccel(dof.ax), 2);
  Serial.print(";");
  Serial.print(dof.calcAccel(dof.ay), 2);
  Serial.print(";");
  Serial.print(dof.calcAccel(dof.az ), 2);
  
#elif defined PRINT_RAW 

  Serial.print(dof.ax);
  Serial.print(", ");
  Serial.print(dof.ay);
  Serial.print(", ");
  Serial.println(dof.az);
  
#endif

}

void printMag()
{
  dof.readMag();
  
Serial.print(";");

  /*Serial.print(" M: ");*/
  
  #ifdef PRINT_CALCULATED
 
  Serial.print(dof.calcMag(dof.mx), 2);
  Serial.print(";");
  Serial.print(dof.calcMag(dof.my), 2);
  Serial.print(";");
  Serial.print(dof.calcMag(dof.mz ), 2);
  
#elif defined PRINT_RAW

  Serial.print(dof.mx);
  Serial.print(",");
  Serial.print(dof.my);
  Serial.print(",");
  Serial.println(dof.mz);
  
  
#endif
}

void printHeading(float hx, float hy)
{
  float heading;

  if (hy > 0)
  {
    heading = 90 - (atan(hx / hy) * (180 / PI));
  }
  else if (hy < 0)
  {
    heading = - (atan(hx / hy) * (180 / PI));
  }
  else // hy = 0
  {
    if (hx < 0) heading = 180;
    else heading = 0;
  }

  Serial.print(";");
  /*Serial.print(" H: ");*/
  Serial.print( heading, 2);
}

void printOrientation(float x, float y, float z)
{
  float pitch, roll;

  pitch = atan2(x, sqrt(y * y) + (z * z));
  roll = atan2(y, sqrt(x * x) + (z * z));
  pitch *= 180.0 / PI;
  roll *= 180.0 / PI;

  Serial.print(";");

  /*Serial.print(" P, R: ");*/

  Serial.print( pitch, 2);
  Serial.print(";");
  Serial.print( roll, 2);
  Serial.print(";");


  /*Serial.print(";n;");*/
}

processing code :

import processing.serial.*;

Serial myPort;  // Create object from Serial class

PrintWriter output;

boolean enregistrer;

/*String inString="";
float gx;
float gy;
float gz;
float ax;
float ay;
float az;
float mx;
float my;
float mz;
float heading;
float pitch;
float roll;*/

Table table;
Table valueTable;
//float valeurs[];

void setup() 
{
  size(800, 800);
  background(0);
  myPort = new Serial(this, "COM4", 9600);
  //frameRate(30);
  myPort.bufferUntil('\n');
  
  
  output = createWriter("valeurs.csv");
  table = new Table();
  table.addColumn("gx", Table.FLOAT);
  table.addColumn("gy", Table.FLOAT);
  table.addColumn("gz", Table.FLOAT);
  table.addColumn("ax", Table.FLOAT);
  table.addColumn("ay", Table.FLOAT);
  table.addColumn("az", Table.FLOAT);
  table.addColumn("mx", Table.FLOAT);
  table.addColumn("my", Table.FLOAT);
  table.addColumn("mz", Table.FLOAT);
  table.addColumn("heading", Table.FLOAT);
  table.addColumn("pitch", Table.FLOAT);
  table.addColumn("roll", Table.FLOAT);
  //table.addColumn("valeur n", Table.FLOAT);
  
  
}
  
 
  void draw ()
  {   
         delay(100);
         
         while (myPort.available()>0){
           String inString = myPort.readString();
           String[] newString = splitTokens(inString,";\n");
           
           float gx = float (newString[0]);
           float gy = float (newString[1]);
           float gz = float (newString[2]);
           
           print(gx,gy,gz);
             
           float ax = float (newString[3]);
           float ay = float (newString[4]);
           float az = float (newString[5]);
           
           println(ax,ay,az);
           
           float mx = float (newString[6]);
           float my = float (newString[7]);
           float mz = float (newString[8]);
           
           println(mx,my,mz);
           
           float heading = float (newString[9]);
           float pitch = float (newString[10]);
           float roll = float (newString[11]);
           
           println(heading,pitch,roll);
           
           table.setFloat(0, "gx", gx);
           table.setFloat(0, "gy", gy);
           table.setFloat(0, "gz", gz);
           
           table.setFloat(0, "ax", ax);
           table.setFloat(0, "ay", ay);
           table.setFloat(0, "az", az);
           
           table.setFloat(0, "mx", mx);
           table.setFloat(0, "my", my);
           table.setFloat(0, "mz", mz);
           
           table.setFloat(0, "heading", heading);
           table.setFloat(0, "pitch", pitch);
           table.setFloat(0, "roll", roll);        
           }
        
      }
          
      
   
  
  void keyPressed() 
{
   
   output.flush(); // Writes the remaining data to the file
   saveTable(table, "valeurs.csv");
   //output.close(); // Finishes the file
   enregistrer = true;


 exit(); // Stops the program
 }

Thank you :slight_smile:

Instead of this:

while (myPort.available()>0){
      String inString = myPort.readString(); 
      String[] newString = splitTokens(inString,";\n");

     ...
}

do this

while (myPort.available()>0){
      String inString = myPort.readString(); 
      println("REPORT: valid buffer=" + inString!=null?"Yes":"No");
      if(inString!=null){
          println("Received: "+inString);
      }
}

This will give you the opportunity to debug your code. One important point is that you should not assume that String[] newString = splitTokens(inString,";\n"); has all the data. Instead, if you are expecting 9 tokens or data values, you should check and ensure you have 9 values in your array. If you don’t then you can discard the data (easiest way) and try again. Another option, you could keep reading the input stream and add any new data to your input buffer (aka. inString). As soon as you get 9 tokens, you process those elements and then you could assign them to your g*, a* and m* fields.

This is not a solution but a suggestion how to approach your problem. Consider checking:

Processing with Arduino - void serialEvent - Processing 2.x and 3.x Forum
error with serial - Processing 2.x and 3.x Forum

Kf

Thank you for your answer i have sold my problem with your idea checking if i have all my values with only a comparator : String inString = myPort.readString();
String[] newString = split(inString,’;’);

       //print(newString);                      
       
       valeurs = newString.length;

       //println(newString.length);
       
       if (valeurs == 12){  
         
         float gx = float (newString[0]);
         float gy = float (newString[1]);
         float gz = float (newString[2]);..........

and it works