Split only returning first index
in
Core Library Questions
•
10 months ago
I am reading a serial feed from my arduino. I read in the entire line great and can print it, draw it, or do whatever. But when I use the split command to break it up by spaces or commas (I have tried both), I only get the first value. I can do whatever with that value, but I get an out of bounds exception when I try to read the second value in the data array. Below you can see the code and the output from running it. The string message is the list of numbers seperated by spaces wich I am trying to split. Any thoughts?
import processing.serial.*;
Serial myPort; // Create object from Serial class
short LF = 10; // ASCII linefeed
short portIndex = 4;
String message = "filler";
String T0="0";
String T1="0";
void setup()
{
size(1300, 800);
println(Serial.list());
println(" Connecting to -> " + Serial.list()[portIndex]);
myPort = new Serial(this,Serial.list()[portIndex], 9600);
myPort.bufferUntil(LF);
}
void draw()
{
//a bunch of drawing commands go here
//printing out temperatures
text(T0,575,600);
text(T1,650,600);
}
void serialEvent(Serial p)
{
message = myPort.readStringUntil(LF); // read serial data
if(message != null)
{
print(message);
String [] data = split(message, ' '); // Split the comma-separated message
T0 = data[0];
T1 = data[1]; //THIS IS THE LINE THAT CAUESS THE ERROR
println();
redraw();
}
}
1