I don't know if this is an arduino issue or processing issue, so I'm starting in this forum.
You can think of my project as a weather station. It's not a weather station, but you can still visualize it that way. I have an Arduino Mega with three or four analog sensors that are displayed using Processing on a Win7 PC via serial communication. A very straight forward project configuration.
I would like Processing to request data from the Arduino, rather than the Arduino just sending data at some constant rate, but have discovered I can request data from the processing side only once per second. The Arduino doesn't want to read incoming requests faster than that no matter what "read" strategy I employ (read, readbytesuntil, serialevent, etc) on the Arduino side. I don't need super fast performance, just looking for 5-10 serial transfers per second.
Interestingly, if I pull the serial.write (to request data from the arduino) out of the draw() function and put it into a mouseClicked() function...I can use the mouse to send many requests per second and that works perfectly. The Arduino responds to each serial.write() and I'm easily sending 7-8 per second by just clicking the mouse button very quickly. So this appears to be an issue surrounding the draw() function.
Whether I use delay() or framerate() to throttle processing execution, processing to arduino serial seems to be limited to once per second requests. Data size is small in both cases. Processing is only sending a one byte command and new line to the Arduino. The Arduino is responding with about 96 bytes of data. Serial speed doesn't matter, I have tried 9600 - 115200 and the results are the same. I've also tested both Uno and Mega with the same results.
Any ideas? Anyone willing to test the code below to see what your results are? Just change the delay values in either script.
// ARDUINO CODE
char cin;
void setup()
{
Serial.begin(115200);
}
void loop()
{
char din[4];
if ( (Serial.available()) > 0 ) {
Serial.readBytesUntil('\n',din,4);
if ( din[0] == 'M' ) {
Serial.println(din[0]);
Serial.flush();
}
}
delay(100);
}
// PROCESSING CODE
import processing.serial.*;
Serial myPort;
char c;
int i=0;
void setup()
{
size(200,200);
String portName = Serial.list()[0];
myPort = new Serial(this,portName,115200);
}
void draw()
{
if ( myPort.available() > 0 ) {
i++;
c = myPort.readChar();
println(c + " " + i);
}
myPort.write("M\n");
delay(1000);
}