Setting up Processing for Arduino Communication

I'm starting to work with processing and I need to make the Serial Port COM 4, to interface with my arduino. How do I do this? Is it a piece of code?

Thanks a lot! -duemilanove

Look at the examples provided with Processing for how to determine the available ports. One of the ports in the array will be the one that the Arduino is attached to. Just use the correct index into the array to access that port.

Look at the examples provided with Processing for how to determine the available ports. One of the ports in the array will be the one that the Arduino is attached to. Just use the correct index into the array to access that port.

Thanks for the Reply. I’m really new to this and I still don’t know exactly what to look for.


This is the code that I have running on my Arduino:

//  Read data from the serial port and turn ON or OFF a light depending on the value

char val;         //Data received from the serial port
int ledPin = 13;  //Set value ledPin to pin 13

void setup(){
pinMode(ledPin, OUTPUT);  //Sets pin as OUTPUT
Serial.begin(9600);    //Start serial communication at 9600bps
}

void loop(){
if (Serial.available()){  //If data is available to read,
val = Serial.read();    //read it and store it as val
}
if (val == 'H'){              //If H is recieved
digitalWrite(ledPin, HIGH);  //turn ON light
} else {
digitalWrite(ledPin, LOW);  //If not leave light OFF
}
delay(25);
}

And here is the code for Processing:

//Check if the mouse is over a rectangle and write the status to the serial port

import processing.serial.*;
import cc.arduino.*;


Serial port;    //Create object from Serial class

void setup() {
size(200, 200);
noStroke();
frameRate(10);
//Open the port that the board is connected to and use the same speed (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}

void draw() {
background (255);
if (mouseOverRect() == true) {  //If mouse if over square
fill(242, 204, 47);                    //change color
port.write('H');              //send H to serial port
} else {            //If mouse is NOT over square
fill(0);          //change color
port.write('L');  //send L to serial port
}
rect(50, 50, 100, 100);  //Draws the square
}

boolean mouseOverRect() {    //Tests if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}

Do I need to change a setting in processing, change the arduino code, change the processing code, or what to make them communicate properly? These are sample codes from: http://dailyduino.com/archives/483

Thanks a lot!
-duemilanove

port = new Serial(this, Serial.list()[0], 9600);

This code creates a serial connection using the port named in the 0th position of the Serial.list() output. The Serial.list() function returns an array of port names. You could add some code to call that function first, then print the list. Run the program to see the output.

Then, you know what value to use as the index.

This is how I did it:

  // list all the available serial ports:
  println(Serial.list());

Add this to your program, and run it. On my computer, I see this:

Stable Library

Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 [0] "COM1" [1] "COM3"

Figure out which row corresponds to the port that the Arduino is connected to (the one you use to upload code to the Arduino). Use that index (1 in my case) in place of the 0 (if 0 is not the correct value).

Hey thanks a bunch! It all works now! :) I really appreciate the help!

-duemilanove