We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello everyone..... im new to processing and found first problem..... sorry if i ask simple question but i cant understand this...
i got this code:
import processing.serial.*;
Serial myPort;
String PORT;
String result[];
void setup()
{
size(800, 200);
myPort = new Serial(this, Serial.list()[3], 9600);
//COM that your arduino is on. Mine is on COM22
myPort.bufferUntil('\n');
}
void serialEvent (Serial myPort) //when data is received
{
PORT = (myPort.readStringUntil('\n')); //read to the end of the line
result = split(PORT, ',');
}
void draw()
{
background(0,0,40);
fill(255);
textSize(20);
textAlign(RIGHT);
text("Valore Riserva: ", 200, height/2);
text(result[0], 220, height/2);
text("Valore Medio: ", 450, height/2);
//text(result[1], 470, height/2);
text("Valore Pieno: ", 700, height/2);
//text(result[2], 720, height/2);
for ( int i = 0; i < result.length; i = i+1){
print("index ", i+" : ");
println(result[i]);
}
printArray(result);
}
and i got this result on console:
index 0 : 1
index 1 : 1
index 2 : 1
index 3 :
[0] "1"
[1] "1"
[2] "1"
[3] ""
everything seems to work but when i call an index like this way:
print(result[1]);
it returns me this error:
ArrayIndexOutOfBoundsException:1
like i was calling an unexisting array element......
am i doing something wrong?
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
Initialize result[]:
String[] result = new String[10];
So when draw() is executed, result isn't
null
until serialEvent() is run for the 1st time.https://forum.Processing.org/two/discussion/16618/processing-with-arduino-void-serialevent#Item_1
https://forum.Processing.org/two/discussion/14988/drawing-of-graphs-from-i2c-imu#Item_3
tnx... i wasnt thinking before serialEvent() :D noob error im sorry but thank you guys