@PaulS
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.
////////////////START///////////////////////////
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");
println(Serial.list());
myPort = new Serial(this, Serial.list()[3], 9600);
myPort.bufferUntil('\n');
}
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
}
}
////////END//////////////
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!
eR