We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello guys here is my code. I have tried to draw 3 graphs from arduino. I am reading angle by meaning inegration gyro angle , accelerometer angle and their complementary filter. But i get the message "Error, disabling serialEvent() for COM5 null"
import processing.serial.*;
Serial port;
float currentAngle ;
float pitchacc ;
float pitch ;
int gyro = 1;
int accel = 2;
int xn = 0;
float yn1 = 250;
float yn2 = 250;
float yn3 = 250;
int xk = 0;
void setup ()
{
size (500,500);
background (0);
port = new Serial ( this, "COM5", 9600);
port.bufferUntil ('\n');
}
void draw ()
{
fill(255);
stroke(255);
line (xn, yn1, xk, currentAngle);
line (xn, yn2, xk, pitchacc);
line (xn, yn3, xk, pitch);
if (xk > 500) {
background(0);
xk=0;
}
xn=xk;
yn1 = currentAngle;
yn2 = pitchacc;
yn3 = pitch;
xk++;
}
void serialEvent (Serial port)
{
currentAngle = float(port.readStringUntil(gyro));
pitchacc = float(port.readStringUntil(accel));
pitch = float(port.readStringUntil('\n'));
}
Answers
https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text
https://forum.Processing.org/two/discussions/tagged?Tag=bufferuntil()
Here is my arduino code.
As I can guese I have something wrong with transforming data from string to float... in these 3 lines :
Modify the Arduino's source to separate those sent values w/
'\t'
:If I understand correctly, I can print each value from String using " [ ] ", can't I? But when I try to execute " println (vals[0]); " e.x., it says " NullPointerException'. For example if I need to build up 3 graphs I need to have 3 variables but I cannot use them separately.
For my example to work, you need to modify how the data is sent from the device.
For example, if you send 3
float
values:println("10.5" + "\t" + "-2.3" + "\t" + "1.0");
In Processing's end, it's expected:
vals[0] = 10.5, vals[1] = -2.3, vals[2] = 1.0
I am just really confused. I understand how splitTokens operates and the rest. I can send to processing all data, and if I use your sketch it gives me such numbers : (see picture "example") But if I for example want to print only one of them ( e.x. [0] or [1]) it says me "NullPointerException"... May be I didn't understand correctly how to modify arduino's code may be something else... Here my adruino's code
Arduino code seems correct. Maybe show us how you're trying to get each individual value from vals[].
Refering to splitToken reference explnation on Processing website, I want to get all the value independently and use them as y - coordinate for graphs building.
Until serialEvent() is finally called back for the 1st time, vals is still
null
!An easy fix is simply instantiating some
float[]
array at the same time vals is declared:Change:
float[] vals;
tofloat[] vals = new float[3];
Perfect! Now it is what I want! Appreciate it! One moment, could you please describe a bit how
float [] vals = new float[3];
works? I have read on website, but still it is not absoulutelly clear for me. Thanks!null
when it reachesprintln(vals[0]);
.vals[0]
crashes b/c we can't do anything w/null
but print it raw:println(vals);
new float[3]
, it's notnull
anymore.println(vals[0]);
. Although it's just gonna display0.0
at that 1st draw().float[]
array generated byfloat(splitTokens(s.readString()));
expression. :-bd