Reading and writing Serial at the same time with Arduino + Processing. possible?

Heya-

I'm working on something and keep running into errors with my serial bus. I'm wondering if what I am trying to do is even possible, and if there is some other way to do it?

Essentially, I built a little Arduino device that has LEDs that respond to Serial data coming from Processing that have been triggered by midi notes. That works fine on it's own.

Also on the device is an optical sensor that the Arduino then converts to Serial data, to send to Processing, to turn into MIDI data for other effects. That works fine on it's own.

But when combining the functionality of both, my processing sketch throws errors and disables the COM port ("error, disabling serialEvent() for //./COM3") which I'm assuming is because I am asking both pieces (the Arduino and the Processing sketch) to both read and write to COM3.

Is there some way for me to get this working?

Processing:

import rwmidi.*;
import processing.serial.*;

int kickNote = 36;
int snareNote = 38;
int hatNote = 44;

MidiInput input;
MidiOutput output;

String serialChunk = "";
int squeezeValue = 0;
int lastSqueeze = 0;
int photoValue = 0;
int lastPhoto = 0;
String[] serialArray;

Serial port;

void setup()
{
 size(400,400);
 input = RWMidi.getInputDevices()[0].createInput(this);
 output = RWMidi.getOutputDevices()[3].createOutput();

 port = new Serial(this, "COM3", 9600);
 port.bufferUntil('\n');
}
void noteOnReceived(Note note) {
  println("note on " + note.getPitch());
  if (note.getPitch() == kickNote){port.write(0);}
  if (note.getPitch() == snareNote){port.write(1);}
  if (note.getPitch() == hatNote){port.write(2);}
}
void noteOffReceived(Note note) {
  println("note OFF " + note.getPitch());
  if (note.getPitch() == kickNote){port.write(0);}
  if (note.getPitch() == snareNote){port.write(1);}
  if (note.getPitch() == hatNote){port.write(2);}
}
void draw(){
////////////////STUFF///////////////
}
void serialEvent (Serial port){
 serialChunk = port.readStringUntil('\n');
 serialChunk = trim(serialChunk);
 serialArray = split(serialChunk,",");
 squeezeValue = int(serialArray[0]);
 squeezeValue = int(map(squeezeValue,0,255,0,255));
 //println(serialArray[1]);
 photoValue = int(serialArray[1]); 
 photoValue = int(map(photoValue,0,255,0,255));
}

Arduino:

int photoPin = 0;
int photoValue;
int photoMapped;

int kickLed = 9;
int snareLed = 11;
int hatLed = 12;
boolean kickStatus = LOW;
boolean snareStatus = LOW;
boolean hatStatus = LOW;

int incomingByte=0;
//String readString;
//char readStringArray[2];

void setup() {
  Serial.begin(9600);
  pinMode(kickLed, OUTPUT);
  pinMode(snareLed, OUTPUT);
  pinMode(hatLed, OUTPUT);
    ///////// Startup Animation //////////
    delay(100);
    digitalWrite(kickLed, HIGH);
    delay(250);
    digitalWrite(kickLed, kickStatus);
    digitalWrite(snareLed, HIGH);
    delay(250);
    digitalWrite(snareLed, snareStatus);
    digitalWrite(hatLed, HIGH);
    delay(250);
    digitalWrite(hatLed, hatStatus);

}

void ledTrigger(int ledMessage){
  if (ledMessage == 0) { kickStatus = !kickStatus; digitalWrite(kickLed, kickStatus); }
  if (ledMessage == 1) { snareStatus = !snareStatus; digitalWrite(snareLed, snareStatus); }
  if (ledMessage == 2) { hatStatus = !hatStatus; digitalWrite(hatLed, hatStatus); }  
}

void loop() {
  delay(10);

if (Serial.available() > 0) { /////// READ Serial /////
    // read the incoming byte:
    incomingByte = Serial.read();
    ledTrigger(incomingByte);

 }

  photoValue = analogRead(photoPin); /////// WRITE Serial /////
  photoMapped = map(photoValue, 320,840,0,255);
  Serial.println(photoMapped);


} // end loop()

Well, you could post all your code. Please use the # icon on the editor's toolbar.

AWOL: Well, you could post all your code. Please use the # icon on the editor's toolbar.

Posted, apologies.

kickStatus = !kickStaus;

I see you found the typo in the previous post then :)

I don't see how the Arduino reading and writing to the USB can in itself cause the port to be closed, having the two talk to each other on the same port is normal. So I think it has to be in your Processing code. I can't see anything there either but I have limited Processing experience.


Rob