Go Down

Topic: talking between 2 Arduinos and 1 Processing (Read 1 time) previous topic - next topic


Hi!  For my project, I have two Arduinos connected to 1 computer (which is running processing) via two serial ports.  

In my setup there are 4 potentiometers and 4 LEDs connected to each Arduino and my goal is to get the potentiometers on one Arduino to fade in and out the LEDs on the other Arduino, as simultaneously as possible.  

I've actually gotten the communication to work using the processing-arduino library here:

and the Processing code here:

Code: [Select]
import processing.serial.*;
import cc.arduino.*;

Arduino arduino1, arduino2;
int ledPin1 = 3;
int ledPin2 = 6;
int ledPin3 = 10;
int ledPin4 = 11;

int bright;

void setup()
 arduino1 = new Arduino(this, Arduino.list()[1], 57600);
 arduino2 = new Arduino(this, Arduino.list()[2],57600);
 arduino1.pinMode(ledPin1, Arduino.OUTPUT);
 arduino1.pinMode(ledPin2, Arduino.OUTPUT);
 arduino1.pinMode(ledPin3, Arduino.OUTPUT);
 arduino1.pinMode(ledPin4, Arduino.OUTPUT);
 arduino2.pinMode(ledPin1, Arduino.OUTPUT);
 arduino2.pinMode(ledPin2, Arduino.OUTPUT);
 arduino2.pinMode(ledPin3, Arduino.OUTPUT);
 arduino2.pinMode(ledPin4, Arduino.OUTPUT);

void draw()
 bright = arduino2.analogRead(0)/4;
 arduino1.analogWrite(ledPin1, bright);
 bright = arduino2.analogRead(1)/4;
 arduino1.analogWrite(ledPin2, bright);
 bright = arduino2.analogRead(2)/4;
 arduino1.analogWrite(ledPin3, bright);
 bright = arduino2.analogRead(3)/4;
 arduino1.analogWrite(ledPin4, bright);
 bright = arduino1.analogRead(0)/4;
 arduino2.analogWrite(ledPin1, bright);
 bright = arduino1.analogRead(1)/4;
 arduino2.analogWrite(ledPin2, bright);
 bright = arduino1.analogRead(2)/4;
 arduino2.analogWrite(ledPin3, bright);
 bright = arduino1.analogRead(3)/4;
 arduino2.analogWrite(ledPin4, bright);

Then I just loaded onto each Arduino the Standard Firmata code given in the Arduino examples.

My problem is that while the communication works perfect, the connection is extremely extremely slow.  When I try to fade LEDs, the update speed is so slow that the LEDs pretty much blink from super dim to super bright!

As a result, I'm trying to program the Arduinos and Processing separately using the "handshake" method given in the communication example "SerialCallResponse."  While I can get this method to work for talking between one Arduino and processing, I'm completely lost as to how to connect two Arduinos.  

For example, in the Processing code, there is a setup

void serialEvent(Serial myPort)

I can only call this once and it specifies only one serial port (that I know of!) so how would I get it to talk to two serial ports?

Is there another way to get the Arduinos to communicate faster while still keeping the communication happily synced?  

Thanks so much for any advice!  


Feb 28, 2010, 10:04 pm Last Edit: Feb 28, 2010, 10:04 pm by MikMo Reason: 1
Do you need Processing for anything else than being the middleman?

If not then you can just connect the serial ports of the 2 Arduinos togeter
(dont forget to connect the grounds as well)


For example, in the Processing code, there is a setup

void serialEvent(Serial myPort)

I can only call this once and it specifies only one serial port (that I know of!) so how would I get it to talk to two serial ports?

The "Arduino.list()[1]" argument is defining the COM port that the Arduino is talking to. The Serial.list() function returns a list of available serial ports the same way.

Serial myFirstArduino = Serial.list()[1];
Serial myOtherArduino = Serial.list()[2];

Then, when serialEvent is called, compare the argument myPort to myFirstArduino. If they match, the data came from the first Arduino. Otherwise, it came from the other Arduino, or somewhere else.

But, as MikMo pointed out, Processing does not seem to be needed at all.
The art of getting good answers lies in asking good questions.


Hi MikMo,

My current setup is actually a simplified version of the end project:
Connect Arduino1 to computer1 via bluetooth, connect computer1 to computer2 using internet and connect computer2 to Arduino2 using bluetooth again.  As you can see, right now I'm still trying to connect Arduino1 and Arduino2 to the computer and have that conversation go smoothly.  Also, I may be using very complex controls (to control motors instead of fading LEDs) later on, so I thought it would be good to have much more memory to work with than the chips on the Arduinos.  

I also already have the two bluesmirf modules but I haven't gotten them to even be recognized by the internal bluetooth device on my computer (it only sees a "firefly" device).    

Actually, now I'm thinking perhaps I can talk directly between the Arduinos, as you mentioned, using bluetooth.  Without bluetooth, is this simply a matter of hooking up the tx and rx pins (tx of Arduino1 to rx of Arduino2 and vice versa)?


Hi PaulS,

Thanks!  I think I have a better grasp of what the serialEvent function does (apparently I was grossly misunderstanding it before).

I'm actually now focusing on getting the bluetooths to function (though I'm guessing this will just add further delays in communication?) :(

Go Up