Pages: [1]   Go Down
Author Topic: Spliting a string from the Arduino on Processing, Array error.  (Read 329 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:

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.
« Last Edit: April 02, 2014, 09:13:37 am by Ondelight » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm sending data from my arduino to Processing
How?

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: April 02, 2014, 04:11:23 pm by Ondelight » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: