Processing and Arduino: seemingly no communication

Hi everyone,

My name is Jeroen, and I am new to this Arduino forum. I am trying to establish a serial communication between Arduino and Processing. I start by printing lines with int values from my Arduino (Serial.println(i)), and try to read these on Processing. Here is the code: Arduino

int i = 0;

void setup(){
  Serial.begin(9600);
}
void loop(){
  if(i > 3){
    i = 0;
  }
  Serial.println(i);
  i++;
  delay(1000);
}

Processing

import processing.serial.*;
Serial port;
int val=0;
void setup(){
  size(500,500);
  port = new Serial(this,"COM9",9600);
  port.bufferUntil('\n');
}
void draw(){
  background(50);
  if(val==0){
    fill(125);
  }
  if(val==1){
    fill(125,125,0);
  }
  else if(val==2){
    fill(125,0,0);
  }
  else if(val==3){
    fill(0,0,125);
  }
  rect(125,125,250,250);
}
void serialEvent(Serial port){
  val = int(port.readStringUntil('\n'));
}

When I check the Arduino on the Arduino serial monitor, everything works fine. The lines that are printed contain 1,2,3,0,1,2,3,0,...... The problem is, when I try to read the serial output from the Arduino, nothing happens.. It appears as if Processing does not communicate with the arduino. The TX-LED on the Arduino is flashing though.

I have the RXTX-2.1-7 Processing Library installed, that's what I found out when looking at the black console-box from Processing:

Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7

Could you guys help me out? I don't know what is wrong with my set-up. I checked the COM-port, which was the right port. I did not have the Arduino serial monitor running at the same time I had the Processing sketch running.

Thanks in advance, Jeroen

Are you sure the Arduino is actually connected to serial port "COM3" ?

There are examples of code for processing, which actually identify the serial ports it can connect to.

The first time I tried it, it was very difficult to get it to work.

May I suggest you try printing out val in serialEvent() to confirm whether it's ever being called?

-br

Thanks for your replies. I don't have my stuff over here, so I will give it another try tomorrow. @billroy: I did not think of testing the serialEvent() on its own, thanks! @michinyon: the Arduino program indicates COM9 (which I chose in the Serial()-function) as the port that the Arduino is connected to. This is the right port, as uploading the Arduino sketch worked.

Okay, so I tested the serialEvent()-function, and it is indeed triggered every time the Arduino prints to the serial port. I printed the val variable (which is by default 0, but changed by reading out Arduino output). But this variable turned out to remain 0, and not change to 1,2,3,0,1,2,3,0...

Then I reviewed the code, which demanded the output from the Arduino to be converted to integer. I removed that conversion, and changed the if-statements. So it turned out that the Arduino was communicating with Processing after all. But now the if-statements don't seem to work. I changed the values to test from integer to string, but the only if-statement that works is " if(val=="0") ", even though when I print val in the draw()-loop it is the correct value. Here is the new Processing code (Arduino code remains unchanged):

import processing.serial.*;
Serial port;
String val="0";

void setup(){
  size(500,500);
  port = new Serial(this,"COM9",9600);
  port.bufferUntil('\n');
  fill(255,14,120);
  background(50);
}
void draw(){
  background(50);
   if(val == "0"){
    fill(125,125,125);
  }
  if(val == "1"){
    fill(125,125,0);
  }
  if(val == "2"){
    fill(125,0,0);
  }
  if(val == "3"){
    fill(0,0,125);
  }

  rect(125,125,250,250);
  println("val: "+val);
  
  delay(1000);
  
}
void serialEvent(Serial port){
  val = port.readStringUntil('\n');
  //println("val: "+val);
}

Do you know what is going wrong with the code? Is there something wrong with the if-statements? I also tried if(val=="0").... else if(val=="1").... else if(val=="2").... else if(val=="3"), but that did not make any difference.

Why not convert the string to an int, and simply do integer comparison? What you are doing now is comparing the address where val is stored to the address where "0" or "1" or "2" are stored. Highly unlikely that the addresses will ever match.

You can only have one PC application talking to the serial port at a time.

This means, you can't have the Aruino IDE/Serial Monitor using the same serial port as Processing.

This is quite annoying, if you have to keep closing and re-starting the IDE.

@michinyon: that's what I found out the hard way, and indeed once I only used one program to talk to the serial, it worked.

@PaulS: In the previous code iteration I made an attempt to convert the serial output from Arduino to Processing into integer, but this resulted into val always being 0. Could you help me out how I could effectively convert the serial results into integer? I only used the read function port.readStringUntil('\n') and put that in an integer conversion function int(port.readStringUntil('\n')), but this resulted exclusively in val becoming 0.

I only used the read function port.readStringUntil(’\n’) and put that in an integer conversion function int(port.readStringUntil(’\n’)), but this resulted exclusively in val becoming 0.

Try doing it in two steps:

String stuff = port.readStringUntil('\n');
print("From the Arduino: [");
print(stuff);
println("]");
val = int(stuff);
print("val = ");
println(val);

Show us the output.

Okay, so PaulS' reply made me look into how I could convert the serial output into an integer. Turned out that I had to use a proper type conversion. So I found the following functions: - parseInt() - getInt() I used parseInt(), but found out that the "raw" extraction from the serial port wasn't a proper input for the function (read that over here). I found that a function called trim() was required to filter the additional newlines and newspace characters that were included in the output. Only when those are trimmed off, parseInt() could do it's job. And now it works! Thanks!