Problem with bilateral communication between Arduino and Processing

Hi,

I am quite new to Arduino and Processing. I'm working on a project where I want to control a depth camera and a 6DOF IMU with a Micro-Arduino. The problem that I have is that I don't know how to enable a bilateral communication between Arduino and Processing, that is, I want to be able to send information from Arduino to Processing to sketch the values of the angles given by the IMU, and at the same time I also want to send depth information obtained by the camera from Processing to Arduino, so that I can programm the micro-processor to turn an LED on when the distance is greater than X.

So far I have been able to send information from Arduino to Processing and vice versa following this link Connecting Arduino to Processing - learn.sparkfun.com, but as soon as I try to combine both I get an error stating that one of the ports is busy. Is it then even possible to send and receive information through the USB cable?

The code used in Processing lookslike this:

Serial myPortP; //Object created to send the string to Arduino
Serial myPort;  //Object created to receive the string from Arduino

void setup() {

   String portNameP = Serial.list()[1]; //Port
   myPortP = new Serial(this, portNameP, 9600); //Send data of depth to Arduino
   
   String portName = Serial.list()[1];
   myPort = new Serial(this, portName, 9600); //Receive data of angles from Arduino
}

void draw() {
... // I dont show all the code needed to get the Depth
myPortP.write(Depth); //To send the depth values to Arduino

}

void serialEvent(Serial myPort) {

  String myString = myPort.readStringUntil('\n');
  myString = trim(myString);
  float sensors[] = float(split(myString, ',')); //To separate the string of information that comes from Arduino

}

On the other hand, the code for Arduino looks like this:

char val; //Variable to wich the data from Processing will be asigned
...

void setup () {

Serial.begin(9600);
}

void loop()

Serial.print(angles[]); // To display all angles that will be sent as a string
Serial.print(",");
...

if (serial.avaliable())
{
val = Serial.read();
}
}

Any kind of answer will be appreciated, thanks a lot :slight_smile:

Serial myPortP; //Object created to send the string to Arduino
Serial myPort;  //Object created to receive the string from Arduino

There is only one serial port. Therefore, there should only be one instance of the class. The serial port IS bidirectional. Use Serial.print() or Serial.write() to write to it, and Serial.available() and Serial.read() to read from it, on the Arduino, and the corresponding methods in Processing to handle the other end of the port.