I have an Arduino sending serial data. Its deeply embedded in a project of which I do not have the code.
If I use the serial monitor of my Arduino IDE I can see the data. Its real time and snappy.
I tried writing some code so I can use this data in Processing, but it lags behind by ~500ms and seems to come in chunks. It also appears to skip readings.
What am I doing wrong?
Here is my code:
import processing.serial.*; //import the Serial library
char end = '\n'; // /nl is the end of the message
String incomingString; // the string which is read
Serial port; // The serial port
String[] a;
void setup() {
port = new Serial(this, Serial.list()[1], 9600);
incomingString = port.readStringUntil(end);
port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
incomingString = null;
}
void draw() {
while (port.available () > 0) { //as long as there is data coming from serial port, read it and store it
incomingString = port.readStringUntil(end);
}
if (incomingString != null) { //if the string is not empty
a = split(incomingString, ", ");
//this is done in order to remap the incoming values so they are in the apropreate order
//print the values for debugging
for (int i = 0; i<8; i++) {
print(a[i]);
print(", ");
}
println();
}
}