So, as part of my project with the Commodore 64 SID chip, I want to use MIDI input from a keyboard to drive the chip. I'm trying to figure out how to get MIDI in to play well with the Arduino. I used this Instructable to get the basic circuit down and connect it to the Arduino Uno. Since I am using Serial Rx to receive MIDI, I also have an LCD circuit wired in to monitor and debug. (From what I understand, I can't use the Serial monitor to debug in this case. Correct me if I'm misunderstanding.)
Here's the code I have for basic monitoring and debugging of MIDI messages:
#include <Wire.h>
#include <LiquidCrystal_SR.h>
byte incomingByte;
void setup(){
Serial.begin(31250);
lcd.begin(16,2); // initialize the lcd
lcd.clear();
lcd.print("hello");
}
void loop(){
checkMIDI();
}
void checkMIDI(){
if (Serial.available()) {
lcd.clear();
incomingByte=Serial.read();
if (incomingByte == 144) {
lcd.print(incomingByte);
lcd.print("Note=");
lcd.print(Serial.read());
lcd.setCursor(0,1);
lcd.print("Velocity=");
lcd.print(Serial.read());
}
else {
lcd.print("Command Byte =");
lcd.print(incomingByte);
lcd.setCursor(0,1);
lcd.print("Next Byte =");
lcd.print(Serial.read());
}
}
}
Anyhow, it appears to me that my circuit is wired correctly, and that the serial port is receiving data. When I hit middle C, and trigger a NoteOn command (incomingByte ==144) and hold it, my LCD displays: "144Note = 60, Velocity = (whatever my Velocity value is)". When I release the note, my LCD displays: "144Note = 60, Velocity = 0." (The "144" is the NoteOn Command and I have it reprinted a bit redundantly for feedback.)
However, I noticed that when I just hit a note quickly and release it, the 144 Note On Command seems to be dropped, and the LCD displays "Command Byte =" (the MIDI value of the note I hit) and "Next Byte = 0" (the Note Off Command.)
What is going on here? To clarify, if I hit a note hold and release, it seems I get this sequence:
144, 60, 64 (NoteOn, Middle C, 64 velocity)
144, 60 ,0 (NoteOn, Middle C, 0 velocity aka NoteOff)
But if I hit it staccato, it appears that my Arduino is picking up:
144, 60, 64
60, 0
(I just rewrote the code a little bit to fit all the data on the LCD screen without clearing it, and, indeed, if I hit the note quickly, the bytes reported are 144, 60 ,64, then 60, 0, with what appears to be a 144 being swallowed there.)
Am I missing something in understanding the MIDI specification? Why is the second 144 commandByte being dropped?