Go Down

Topic: Spliting a string from the Arduino on Processing, Array error. (Read 459 times) previous topic - next topic

Ondelight

Apr 02, 2014, 04:02 pm Last Edit: Apr 02, 2014, 04:13 pm by Ondelight Reason: 1
Code: [Select]


import processing.serial.*;

int lf = 10;  
String myString = null;
Serial myPort;  

void setup() {

 println(Serial.list());

 myPort = new Serial(this, Serial.list()[0], 115200);
 myPort.clear();

 myString = myPort.readStringUntil(lf);
 myString = null;
}

void draw() {
 while (myPort.available() > 0) {
   myString = myPort.readStringUntil(lf);
   if (myString != null) {
     float[] ypr = float(splitTokens(myString, ","));
     println(ypr[1]);
   }
 }
}


Hello, I have a problem, I'm sending data from my arduino to Processing, the data is just 3 values : Yaw Pitch Roll
I'm sending them through Serial.print, and from Processing I split them back into 3 separate values. The problem is, when I try to print the value of ypr[1], the debugger gives me an "ArrayIndexOutOfBOunds:1" error, I get that it's talking about ypr[1] but i checked multiple times and ypr does have 3 entries, ypr[0] works fine but ypr[1] and ypr[2] give errors.
I tried 2 different ways to split the string, they both fail.

PaulS

Quote
I'm sending data from my arduino to Processing

How?

Code: [Select]
void draw() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil(lf);
    if (myString != null) {
      float[] ypr = float(splitTokens(myString, ","));
      println(ypr[1]);
    }
  }
}

Serial data handling does not belong in draw(). Use the serialEvent() method.

Printing what is actually in myString is the first step. Make an array of strings, and make sure that splitTokens() actually doing what you think it is.

Ondelight

#2
Apr 02, 2014, 09:50 pm Last Edit: Apr 02, 2014, 11:11 pm by Ondelight Reason: 1
Thank you for your help,
I'm sending the data in real time from the Arduino through a Serial.print wich looks like this : "Yaw,Pitch,Roll", in Processing I read thoses values from the serial input and I separate them into the ypr array.
I did check all the values before and after the split, myString contains exactly what is sent and println(ypr); gives me the 3 values separated into [zero] [1] and [2], but when I try the call ypr[1] or ypr[2] anywhere, it gives me the same error.
Sorry I'm new to Processing, draw() just seemed like the loop() of the Arduino so I just put it there but that was never suposed to be my final code anyway, I wanted to do stuff with this data afterwards, but I'm still stuck here. I'll put the code in serialEvent and see if that works better tomorrow, but that seems like a long shot.

PS : I also tried the ypr.length command and is says the array has a length of 3, wich is what its suposed to have

PaulS

Quote
I'm sending the data in real time from the Arduino through a Serial.print wich looks like this : "Yaw,Pitch,Roll"

Useless information! Post your CODE!

Quote
I did check all the values before and after the split, myString contains exactly what is sent and println(ypr); gives me the 3 values separated into [zero] [1] and [2],

But, you're not going to show that code or the actual output. Fine. Good luck.

Go Up