Connecting Processing 4 with Arduino - Serial Port Reading issue

Hi @gfs, welcome. I hope we can help change things so you enjoy seeing things work.

I’ve only used Firmata briefly, and I think that you put the Firmata sketch into the Arduino (via the IDE). It worked when I last tried it, some years ago.

The alternative, as you are trying, is to write your own. There are lots of things that can go wrong.

In the Arduino you are using println. That will put cr+lf on the end, you only want the lf. This is why you have a blank line between printed values. You could change the code there, print(val);print("\n"), or trim() what’s received in Processing. In Processing look at the reference for readStringUntil and readBytesUntil. They can return null if the char you are looking at has not arrived yet, and you are not handling that. As I run your code it prints ‘null’ and sometimes stops with NullPointerException. See the help on ‘null’.

When you talk about ‘no delay if possible’ there are lots more things that can go wrong. You must not send faster than the PC can receive. If you do, the buffer in-between fills, and the PC is showing late data. Eventually data starts being lost. Either design it so the PC can keep up, or add in handshaking. (I’ve never used Mac, only Windows and RPi.)

Separate the issues of receiving the correct data from what’s on the GUI. Start by getting the expected values to print on the console. Try just print and print(" "); as a separator, it makes it easier to see more values.

I wrote a serial example, intended to be easy to get going, but might not have the performance that you seem to want.

2 Likes