Reading serial data from Arduino - big delays

Hi all first post here, and new to Processing, this is my first project.

I am trying to read a stream of serial data from an Arduino board, and have it set the angle of a pointer in processing on my screen.

I have followed the guidance here: Connecting Arduino to Processing - SparkFun Learn

and made this:

import processing.serial.*;
Serial myPort;
String val;

void setup()

  String portName = Serial.list()[0]; 
  myPort = new Serial(this, portName, 115200);

void draw()
  if ( myPort.available() > 0) 
  {  // If data is available,
  val = myPort.readStringUntil('\n');  

So, this does indeed print my value to the console, however, the values displayed are not quite as expected, like Processing is very delayed with displaying the numbers.

I have a shaft I’m rotating, and the Arduino is measuring the angle from 0 to 359.999999… degrees. This is being passed out of the Arduino over serial with this print command:


which is effectively the same as Serial.println in Arduino.

This works well in the Arduino IDE serial monitor and serial plotter, the numbers shown relate exactly to the shaft position.

But in the processing console, any shaft movement is hugely delayed between moving it and the change appearing in the console, so much so I’ve not been able to quantify it.

I’m wondering if there is a buffer of serial data in Processing that’s causing the delay? or maybe the console just can’t work that fast?

Any ideas?

PS: Processing 4.1.1 on Linux Mint 20.

Many thanks, Scott.

Hi @Scott, Welcome to the forum. I guess the Arduino is sending too often. It all buffers somewhere between the com port and Processing. Can’t be sure without seeing the Ard sketch. I think you should put a delay of 50ms in your Arduino loop(). See if that removes the laggy problem. Then think how fast you want and how fast PC+Processing will go.

There’s another issue with readStringUntil. After some chars have arrived, and before the \n has arrived, it will return nulls. You can test for this and only do action if not null. You’ll find examples of this in various posts in this section. You won’t see this problem while Ard is sending faster than PC can accept as there will always be a full line waiting.

1 Like

Hi @RichardDL, Thanks for that…

The Arduino IDE serial monitor and other software I use don’t have this buffering issue, so as you supposed, I also guess its something to do with the speed of a buffer within Processing itself. Your ideas on slowing down the data are great, and I shall implement something along these lines, thinking of only serial printing if there is a shaft angle change of 0.1 degrees.

Thanks for the advice, Scott

For some reason my method of only printing when an angle was greater than a setpoint didn’t’ work, so I just added the delay for testing purposes, and this worked well, thanks.