I am having problems connecting Processing to an Arduino

Hello, I need help connecting Processing to my Arduino.

I am new to Processing and Arduino.
I am running on a Mac and using an Arduino Uno.

I don’t know what I am doing…

I have used the following code and methods that I found online:
I am trying to read the Arduino serial out with Processing.
If I get this to work I will then work on writing to the Arduino.


Arduino code:
<void setup()
{
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}

void loop()
{
//send ‘Hello, world!’ over the serial port
Serial.println(“Hello, world!”);
//wait 100 milliseconds so we don’t drive ourselves crazy
delay(100);
}>


Processing code:
<import processing.serial.*;

Serial myPort; // Create object from Serial class
String val; // Data received from the serial port
void setup()
{
String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this, portName, 9600);
}
void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.readStringUntil(’\n’); // read it and store it in val
}
println(val); //print it out in the console
}>

When I run the Arduino code I get a series of "Hello Worlrt"s on it’s I/O
window, as expected.

For the Processing Code I was not sure which serial port to specify.
I tried 3 different methods to find out.

Method 1:
I looked at the Arduino IDE, Tools, Port dropdown.
It listed:
/dev/cu.BLTH
/dev/cu.Bluetooth-Incoming_Port
/dev/cu.UART
/dev/cu.usbmodem1412401
The forth one down is selected.
The forth one down looks correct, so it’s port3 ?

Method 2:
I started a new, separate Processing sketch with the following code:


Processing code:
< import processing.serial.*;
void setup() {
println(Serial.list());
}>

Running this resulted in a list of the same ports, in the same order, as
the Arduino IDE Tools Port list.
… although it printed the list out twice for some reason.

Method 3:
I tried all the port numbers in order.
The results are as follows:

Ports 1, 2, 5, 6:
Produced a vertical string of "null"s in the Processing console screen.

Ports 0, 3, 4, 7 produced the following error:
RuntimeException: Error opening serial port /dev/cu,BLTH: Port busy
Each error had the different name for each of the different devices.

I also got a small empty Processing I/O widow when I ran the
Processing sketch.

The end result is that nothing seems to be connecting.

Well, this was a lot of stuff.
I am obviously interested in getting this working.
I am trying to make a sort of Gui interface for a Arduino bench setup.

Does anyone have any ideas?
Thanks much for any help.

The following two sets of code for Arduino and Processing works on my Mac:

ARDUINO

void setup() {
//initialize serial communications at a 9600 baud rate
Serial.begin(9600);
}

void loop() {
//send ‘Hello, world!’ over the serial port
Serial.println("Hello, world!");
//wait 100 milliseconds so we don’t drive ourselves crazy
delay(100);
}

PROCESSING

import processing.serial.*;

Serial myPort; // Create object from Serial class
String val; // Data received from the serial port

void setup(){
 printArray(Serial.list());
 String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port
 println("portName = " + portName);
 myPort = new Serial(this, portName, 9600);
 println("port = " + myPort);
}

void draw(){
if ( myPort.available() > 0) { // If data is available,
// println("port is available: ");
 val = myPort.readStringUntil('\n'); // read it and store it in val
 println(val);
}

}

Make sure that you print the serial.list so that you can see that you selected the correct port. This port should match what Arduino IDE says it is using to send the data.

Thanks much for your help!

I changed to your code.
Now it is sort of working.
A step in the right direction.

The console now displays "Hello, world"s with randomly interspaced lines with
“null” on them. The “null” lines occur mostly every “Hello world” but not always.
The time between "Hello world"s can vary from about 1 sec to 20 sec or more.

Strange.
Any ideas?

Hello @jimh,

Take a look at this reference:

:)

The “null” lines occur mostly every “Hello world” but not always.

Welcome to serial communication. You would think that neat little packets of data are sequentially sent, but in reality there is a lot of slicing and dicing, and it falls upon us to unscramble the soup. I have also caught it sending invisible characters which are a lot of fun to find and eliminate.

Yes…
I am beginning to understand…
This is going to take some work.
But it is interesting!
It is such a coot thing to be able to do…
Ill get there,
Thanks for your help Jim