Go Down

Topic: multiple arduinos, separate Processing scripts (Read 3561 times) previous topic - next topic


Jan 20, 2011, 02:55 am Last Edit: Jan 20, 2011, 02:55 am by BillyChildish Reason: 1
Howdy folks,
I have several Arduinos sending serial streams that I want to log in separate Processing scripts on the same PC (using PrintWriter objects).

I've tried to set the serial COM ports on each Processing script to log serialEvent data from the different Arduinos/COM ports, which works when each Processing script is activated independently (i.e. one logs data from one Arduino and the other logs data from the other board).

However when one is running previously, the second starts intercepting serial data from the first after it is run.

Not good... ("Don't cross the streams!" I keep hearing...)

Anyone have any hints or alternative approaches?



Not good... ("Don't cross the streams!" I keep hearing...)

Good one.  ;D

I don't speak Processing, but I'm pretty sure some processing ghost buster may show up to help. Offer them a cookie.



When Processing starts, it gets a list of serial ports that it can listen to. How are you making the Processing application choose which serial port to listen to? (Incorrectly, I'm guessing, or you wouldn't be posting here.)

Post your code.
The art of getting good answers lies in asking good questions.


Thanks for your reply!

Hmmm... I probably did make some mistake, but I thought I took care of port assignment when initializing the serial port in the setup with "myPort = new Serial(this, Serial.list()[3], 9600);"
And for the other Arduino board/Processing script I specify the serial port corresponding to the other Arduino.
Anyway, here's the basic code.

Code: [Select]

import processing.serial.*;    // import the Processing serial library
PrintWriter output;    // initialize the PrintWriter Java object

Serial myPort;    // the serial port

long time=0;    // initialize variable for the current time since program started
String trigTime;  // initialize variable for serial data from Arduino

void setup()
 // Create a new file in the sketch directory
 output = createWriter("testText.txt");
 myPort = new Serial(this, Serial.list()[3], 9600);

void draw() {      //draw() can be empty, everything's in SerialEvent

void serialEvent(Serial myPort) {  // use serialEvent() because of intermittent Arduino data
 time = millis();    // see how long it's been since program started

 if (time < 1200000)   // conditional for length of trial
   if (myPort.available() > 0) {  // when serial data is sent from Arduino

     // read serial data of trigger times (since start of this script) from Arduino
     trigTime = myPort.readStringUntil('\n');  // use readStringUntil() to make sure we get entire serial package

     // Write the Arduino data to a file
     output.print(trigTime);  // write the output to .txt file as string (will just be number in text)
     print(trigTime);  // print trigger times to this display

 else {
 output.flush(); // Write the remaining data
 output.close(); // Finish the file
 println("END TRIAL");
 exit(); // Stop the program

As stated above, the other Processing script is setup with a serial stream from the port corresponding to the other Arduino, and when each script is run alone, it reads from the correct Arduino. It is only when they are run together that the second one starts capturing the serial stream from both ports.

I've also tried naming the ports ("myPort1 vs myPort4") differently in the two scripts but this doesn't seem to help. Otherwise, all calls for information from serial in each script seems to refer back to the serial stream I defined in setup, so therefore I don't think I have to subsequently specify this (but maybe that's why this isn't working).

Thanks for your help!

Go Up