Serial connection between Processing and Arduino doesn't work

I’m trying to connect an Arduino Nano Every and Processing via serial. I’m getting no errors on either side, and the arduino serial functionality works otherwise on the arduino IDE.

I’m connecting on the right port, and the port is busy while the processing code is running (as you would expect), however, the variable inside the arduino is not updating. In fact, the whole if serial.available statement is not executing.

Arduino:

int var;

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

void loop() {
  if (Serial.available()) {
    var = Serial.read();
  }
  Serial.println(var);
  delay(100);
}

Processing:

import processing.serial.*;

// The serial port
Serial myPort;

void setup() {
  // List all the available serial ports:
  printArray(Serial.list());
  // open port
  myPort = new Serial(this, Serial.list()[2], 9600);
}

void draw() {
  try {
    myPort.write(1);
  }
  catch(Exception e) {
    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
  }
  delay(50);
}

It is busy if another application is using the serial port such as :

  • Arduino SerialMonitor
  • Arduino Serial Plotter
  • And so on…

Your Processing code is sending data to the Arduino and the Arduino is sending the data back to Processing as a string.

Reference:
Serial.print() - Arduino Reference

Modified Processing code to receive the data to demonstrate that it is working:

import processing.serial.*;

// The serial port
Serial myPort;

byte b;

void setup() 
  {
  // List all the available serial ports:
  printArray(Serial.list());
  // open port
  myPort = new Serial(this, Serial.list()[0], 9600);
  delay(1000);
  }

void draw() 
  {
  try 
    {
    myPort.write(b++);  // Counts from 0 to 255 and repeats since it is a byte
    }
  catch(Exception e) 
    {
    // Print detailed error information to the console.
    System.err.println(e);
    e.printStackTrace();
    }
  //delay(50);
  
  if(myPort.available() > 0)
    {
    int incoming = myPort.read();
    println(incoming);
    }
  }

I also added a delay(1000) in Processing in the setup() to allow the Arduino to reboot before sending data and flooding the buffers.

Related topic:

I only modified your code to show what it is doing as is.
You still have some exploration to do to understand serial communications.

:)

Thank you very much for taking the time to respond, and you are most definitely correct - lots to learn! Thank you again for helping out.

Would you be able to suggest a good resource to get to grips with the basics of serial communication? I have basic/intermediate C++ and can only work with the arduino IDE and starting out with processing.

Hello,

There is a stuff out there!

Try a Google search for:

I have gleaned through a lot of this quickly to get some insight and then write my own code and routines.

Let us know what you find useful.

My background is in assembly and C and my understanding of serial communications is much different than someone new to this topic.

This is my world (from ATmega328P datasheet) and how I started programming serial ports:

This low level programming may not be useful to a beginner but has given me a much deeper insight into what is going on at a hardware level and I can better understand serial communications.

:)

That’s really cool, thank you for sharing! And for taking time to help out newbies on this forum.

1 Like

Hello,

Serial communications:
https://learn.sparkfun.com/tutorials/serial-communication/all

Connecting Arduino to Processing:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

Some good stuff here:
https://itp.nyu.edu/physcomp/ You will have to search through categories.

Processing Serial Library:
https://processing.org/reference/libraries/serial/index.html
This examples and documentation are simple but could use some updates and corrections for clarity.

As I said before do a search and find what best suits you.

There are plenty of topics in this forum as well.

Serial communications is simple but can be daunting at first… once you get over the initial learning curves it gets easier.

:)

Already started going through these resources, thank you for sharing them. Will post on the forum as things develop. Hope you have good holidays :slight_smile:

1 Like