Interpreting MIDI input... seems off

I put together a MIDI-in device according to the schematic on http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1187962258 on the old forum (and I read every post on said thread).

However, when I connect my midi keyboard and play on it, my arduino's output puzzles me (output and code below). For one thing, I don't get unique output from all keys. Can anyone offer insight as to why I might be encountering this difficulty?

Keying on, then off, for the top 12 notes on my MIDI keyboard outputs the following:

243 	 186 	 2 	 0 	 
243 	 250 	 0 	 
19 	 117 	 2 	 0 	 
19 	 245 	 0 	 
51 	 117 	 2 	 0 	 
51 	 245 	 0 	 
83 	 117 	 2 	 0 	 
83 	 245 	 0 	 
115 	 117 	 2 	 0 	 
115 	 245 	 0 	 
147 	 117 	 2 	 0 	 
147 	 245 	 0 	 
179 	 117 	 2 	 0 	 
179 	 245 	 0 	 
211 	 117 	 2 	 0 	 
211 	 245 	 0 	 
243 	 117 	 2 	 0 	 
243 	 245 	 0 	 
19 	 117 	 2 	 0 	 
19 	 245 	 0 	 
51 	 117 	 2 	 0 	 
51 	 245 	 0 	 
83 	 117 	 2 	 0 	 
83 	 245 	 0

This is all sorts of puzzling: after 8 semitones, the first byte repeats, and the following bytes remain constant for a while, so that two distinct notes may yield exact matches. For instance, line 3 (3rd B above middle C) matches line 19 (3rd D# above middle C).

Here's my code (implemented on an MEGA 2560:

void setup() {
  mySerial.begin(31250); // read midi-in
  Serial.begin(9600);  // output to monitor on pc
}

void loop() {
  if (mySerial.available()) {
    while (mySerial.available()) {
      byte in = mySerial.read();  Serial.print(in);  Serial.print(" \t ");
    }
    Serial.println();
  }
}

Do please help.

You have a timing mismatch between data coming in and data going on. MIDI data is arriving at 31250 bps, but you are printing out, as soon as a character is received, at 9600 bps. So it takes longer for you to print data to the screen than it does to arrive from your keyboard. This means you might be missing data coming from the keyboard.

Also note that SoftwareSerial has never had a good history of working well on a Mega. The Mega has several hardware serial ports, why not use one of them?

--
The Flexible MIDI Shield: MIDI IN/OUT, stacking headers, your choice of I/O pins

Thanks for the advice. I implemented some changes, per your advice, but my output is very similar to before (and still puzzling).

Revised code: doesn't use SoftwareSerial; reads all input into a buffer, then outputs buffer

void setup() {
  Serial1.begin(31250);
  Serial.begin(9600);
}

void loop() {
  if (Serial1.available()) {
    // push serial input onto buffer
    while (Serial1.available()) {
      byte in = Serial1.read();
      pushBuffer(in);
    }
    // output vector onto monitor
    if (bufferLen > 2) {
      for (byte i=0; i < bufferLen; i++) {
        Serial.print(buffer[i]); Serial.print(" \t ");
      }
      Serial.println();
      clearBuffer();
    }
  }
}

New output:

243 	 186 	 2 	 
243 	 250 	 0 	 
19 	 221 	 0 	 
19 	 253 	 0 	 
51 	 221 	 0 	 
51 	 253 	 0 	 
83 	 221 	 0 	 
83 	 253 	 0 	 
115 	 221 	 0 	 
115 	 253 	 0 	 
147 	 221 	 0 	 
147 	 253 	 0 	 
179 	 221 	 0 	 
179 	 253 	 0 	 
211 	 221 	 0 	 
211 	 253 	 0 	 
243 	 221 	 0 	 
243 	 253 	 0 	 
19 	 117 	 2 	 
19 	 245 	 0 	 
51 	 117 	 2 	 
51 	 245 	 0 	 
83 	 117 	 2 	 
83 	 245 	 0 	 
115 	 117 	 2 	 
115 	 245 	 0

This output doesn't show any exact repetition, but it does occur outside of this sample. For instance, 3rd A# above middle C matches 2nd F# above middle C (note on: 51 221 0; note off: 51 253 0).

Have I done something wrong in my code?

TMI: Here's the rest of the code in my arduino script, which I omitted above for the sake of brevity.

#define BUFFERLEN 4
byte bufferLen = 0;
byte buffer[BUFFERLEN];

void pushBuffer(byte data){
  buffer[bufferLen++] = data;
}

void clearBuffer(){
  for (byte i=0; i < BUFFERLEN; i++)
    buffer[i] = 0;
  bufferLen = 0;
}

Alright, let's take these one at a time. The first message is "243 186 2", which translates to 0xF3 186 (Song Select #186), and I don't know about the leftover 2. So right away there is some mystery.

I would expect a note-on event, as in this table:

which would be 0x9N NoteNumber Velocity, where N is the channel number. All of your data is entirely strange. How about you just play one note (middle C) and let's study that by itself.

--
The Rugged Audio Shield: Line In, Mic In, Headphone Out, microSD socket, potentiometer, play/record WAV files

FYI, your clearBuffer() function would be much faster as:

void clearBuffer(void) {
  memset(buffer, 0, sizeof(buffer));
  bufferLen=0;
}

--
The QuadRAM shield: add 512 kilobytes of external RAM to your Arduino Mega/Mega2560

Thanks for your patience. Here's middle C.

note on: 115 55 0
note off: 115 255 0

(Yea, it doesn't make sense to me. I can only imagine that I'm losing bits somehow. I'm using a Casio CTK-531 keyboard and am sorry for my inexperience.)

I observe that all of the first bytes in my note-on signals have the same last 5 bits: 10011, whose first four bits are the first four bits of the prescribed note-on signals. Any thoughts?

Hmmm...I can't explain it. Do you get the same output every time you press the key? I'm wondering if it's a hardware thing now. Or maybe Casio has a "MIDI-esque" output that doesn't meet the standard?

--
The Rugged Circuits Yellowjacket: 802.11 WiFi module with ATmega328P microcontroller, only 1.6" x 1.2", bootloader

Yes, I get the same output every time.

I've used the keyboard to supply MIDI into to my PC, so I believe that it does adhere to MIDI protocol. I guess that my MIDI in device is the problem, then (but I still can't guess how that might cause this problem).

Try a resistor value lower than 3.3k, like 1k or 470ohms and see if you get different results.

--
The Basic Motor Driver: simple, inexpensive motor driver for 1 stepper motor or 2 DC motors

My goodness! Thanks. I tried a couple of values, and the one that worked was 330 ohms (quite a lot less than 3.3K).