Can you read from Serial and send values to Processing over Serial?

Hi all,

What I'm trying to do: I'm reading bytes from a midi keyboard. I want to print them over Serial, so that a Processing program I have will show which key is being pressed (A,B,C,D,E,F,G,~b,~#).

The Setup: Incoming bytes from the midi cable are fed to Rx.

What's wrong: a. It doesn't work. Beyond this pertinent observation, I suspect I can't read from Serial and print for Processing to display. b. I'm not sure the typecasting is right. I can't check whether I'm getting the right thing because the baud rate for MIDI is 31250, which I can't set on the Serial Monitor. Ideally I'd like to get a hex value so I can check against the key i'm pressing. I know the Processing code works if I'm merely reading a pot value from A0 and printing over Serial.

The code: in Arduino:

byte data1;
void setup() {
   Serial.begin(31250);
}

void loop(){
  if(Serial.available()>0){
    data1=Serial.read();
    Serial.println(data1);
  }
}

in Processing:

import processing.serial.*;

Serial myPort;        // The serial port
PFont font;

void setup () {
  // set the window size:
  size(800, 500);        

  // List all the available serial ports
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[1].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[1], 9600);
  // don't generate a serialEvent() unless you get a newline character:
  myPort.bufferUntil('\n');  
  
  font=loadFont("AgencyFB-Reg-200.vlw");
  textFont(font, 200);
}
void draw () {
  // everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // convert to an int and map to the screen height:
    int inByte = int(inString); 
    
    background(0,0,0);
    fill(46, 209, 2);
    text(inByte, 300, 400);
  }
}

I'd really like to get some feedback on this. Any suggestions? Thanks in advance!

Nicolas

 myPort = new Serial(this, Serial.list()[1], 9600);

You need to set the same baud rate in processing that you do in the arduino, not 9600 baud.

As an alternative you could have a full MIDI interface on the arduino and on the PC, then use processing to receive MIDI data.

You can one application at a time on the other end of a serial port. If that application is the midi application, you can't talk to Processing, because it is not the application at the other end of the serial port.

If Processing is the application at the other end of the serial port, you can't read midi messages, because the midi application is not at the other end of the serial port.

Incoming bytes from the midi cable are fed to Rx.

They should be fed to some other pin, and NewSoftSerial (pre 1.0) or SoftwareSerial (1.0+) used to read that pin as part of a software serial port. Then, the hardware serial port can talk to Processing.