Problem with serial event and declaring a variable
in
Programming Questions
•
5 months ago
Hello,
i recently try to write a sketch for processing which uses the Arduino-Brain-Library as input source.
My Problem is that i cannot get any values from the serialEvent to use in the void draw loop.
If i let them println in the serialEvent they are correct. If i let them println in the void draw loop they turn zero.
Sorry for the clumsy english. Here is my code in the most simplest version i could think off:
import processing.serial.*;
Serial myPort; // The serial port
int packetCount = 0;
float what;
void setup () {
// set the window size:
size(400, 300, P3D);
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil(10);
background(0);
frameRate (25);
}
void draw () {
println (what); //this gives me zero
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String[] Values = split(myPort.readString(), ',');
// Verify that the packet looks legit
if (Values.length > 1) {
packetCount++;
// Wait till the third packet or so to start recording to avoid initialization garbage.
if (packetCount > 3) {
for (int i = 0; i < Values.length; i++) {
int newValue = Integer.parseInt(Values[i].trim());
//println (Values[4]); //this works
float what = float (Values[4]);
}
}
}
}
1