Hi! Im trying to display my sensor temperature thats hooked up with the arduino.
The project is basiclly to display temperature in little rectangles with the colors:RGB, but to do that I first have to split my String and then convert the second part of the String to a float while leaving the first part a String and giving it a new name.I have succesfully split my String and list[0] works fine (list[0] being the first part I was talking about), But for some reason whenever i try to use list[1] it keeps returning a ArrayIndexOutOfBoundsExeption: error. Could somebody help me with this? Here is the Code:
Processing code:
1 import processing.serial.*;
2 Serial myPort;
3 int lf = 10;
4 float temp =0.1;
5 String s =null
6 void setup() {
7 myPort = new Serial(this, “COM5”, 9600);
8 myPort.clear();
9 s = myPort.readStringUntil(lf);
}
10 void draw() {
11 while (myPort.available() > 0) {
12 s = myPort.readStringUntil(lf);
13 if (s != null) {
14 String[] list = split(s, “,”);
Since the error is being generated here 16 float temp = float(list[1]);
Then the problem is not caused by the conversion of a String to a float it is because the array length is less than 2 i.e. 0 or 1. It can’t be 0 because the exception would have been thrown by line 15. The only way I can see that happening is if the String s does not contain a comma.
Please show an example of s that generates this exception and the output from print list otherwise we are all working in the dark and having to guess possible causes.
import processing.serial.*;
Serial myPort;
int lf = 10;
float temp =0.1;
String s = null;
void setup()
{
// List all the available serial ports:
printArray(Serial.list());
myPort = new Serial(this, "COM6", 9600);
//myPort.clear();
s = myPort.readStringUntil(lf);
//println("s0:", s);
}
void draw()
{
while (myPort.available() > 0)
{
s = myPort.readStringUntil(lf);
//println("s1:", s);
if (s != null)
{
println("s2:", s);
String[] list = trim(split(s, ","));
//String[] list = split(s, ",");
//String sen1 = list[0];
//println("sen1: ", sen1);
//printArray(list);
if(list.length == 2)
{
printArray(list);
float temp = float(list[1]);
println("temp: ", temp);
}
//delay(600);
}
}
}
I wrote some test code on Arduino that I know the behavior of from the Arduino serial monitor.
I added some println() statements and other code to the Processing side to monitor incoming data.
trim() will removed the “linefeed” from the string; this may not be necessary if the float conversion can remove it. I still remove it in all code I write.
You can comment and uncomment as you desire to see what is going on when testing your code.
I also checked for an expected and valid array length before trying to read data from it.
My first pass at this with some tweaks to your code: