Connecting Processing 4 with Arduino - Serial Port Reading issue

Hello there,

I want to connect my Arduino UNO Board (of a Grove Kit) with Processing. I could not figure out a way to directly access the Arduino from Processing with the cc.arduino library. For me it seems like it is only usable with Processing versions before Processing 4. This is why I’m working with a serial port this way.

(Definitely feel free to inform me about any shorter way if there is one.)

What I did:

I connected the Arduino board to my MacBook, uploaded StandardFirmata and the following code to the board.

Following code is written inside Arduino IDE.

int analogPin = A0;
int val = 0;

void setup(){
  Serial.begin(115200);
}

void loop(){
  val = analogRead(analogPin);
  Serial.println(val);
  delay(350);
}

After that I added a Rotary Angle Sensor to the A0 Pin. I checked the serial monitor of the Arduino IDE to check if things are working — they were. Please note that I am working with a delay of 350.

Then I headed over to processing and worked with this code:

Following code is written in Processing.

import processing.serial.*;
float f;

Serial port;
String val; 

void setup() {
  port = new Serial(this, "/dev/cu.usbserial-144240", 115200);
  size(700,700);
  background(255);
}

void draw() {
  if ( port.available() > 0)
  { // If data is available,
    val = port.readStringUntil('\n'); 
    f = float(val);
  }
  
    noStroke();
    fill(0);
    rect(width/4,height/4,f,f);

  println(val);
}

For the sake of just trying it out, I wanted to have a black rectangle resize with the value given from the Rotary Angle sensor. Processing is reading the serial port like it should but only every 350 milliseconds (the delay I set). I would love to see it reading perfectly with a delay below 10 milliseconds or just no delay at all.
Deleting the delay inside of Arduino or setting it to something like 200 milliseconds will result with Processing giving out only one value the whole time.

I hope I was able to clarify my problem. If not, feel free to ask again.

(I found this topic in the forum as well, but it could not help me either: Issue with reading Arduino Port)

Thank you for your help!
gfs

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