I am new to the Arduino world. My first sketch was about sending MIDI data to the computer via USB, playing different scales, worked like a charm.
With my second sketch I have a problem I don't understand. My intention was to receive MIDI note commands via USB and then send the same commands back to the computer. I use Hairless MIDI for the MIDI message conversion, so please don't be surprised about the odd baud rate.
My problem is, that the Arduino only sends a status byte back to the computer and I can't tell why. The error message in Hairless MIDI reads: "Warning: got a status byte when we were expecting 2 more data bytes, sending possibly incomplete MIDI message 0xff"
The status bytes received by the Arduino read 90 or 80 (HEX) for NoteOn or NoteOff.
Can somebody put me in the right way, please? I'm trying to solve this problem for hours, but I can't get this to work.
byte commandByte;
byte noteByte;
byte velocityByte;
byte lastCommand;
byte lastNote;
byte lastVelocity;
void setup(){
Serial.begin(38400);
}
void loop(){
getMIDI();
sendMIDI(commandByte, noteByte, velocityByte);
}
void getMIDI(){
do{
if (Serial.available()){
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
}
}
while (Serial.available() > 2);//when at least three bytes available
}
void sendMIDI (byte comm, byte note, byte velo){
if(comm != lastCommand || note != lastNote || velo != lastVelocity){
Serial.write(comm);
Serial.write(note);
Serial.write(velo);
lastCommand = comm;
lastNote = note;
lastVelocity = velo;
}
}
if (Serial.available() >= 3)
{
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
}
Its also a great example of pointless comments. Comment things that are not obvious from the code in front of you. Intention, tricks and gotchas,
things that will trip people up if they aren't explained.
Many electronics/technology instructables are woeful, in my experience, as there appears
to be a complete lack of quality control. Lots of bad circuits
lacking decoupling and full of howlers...
Oh yes, look, just picked the first Arduino-related instructable I could find:
Still waiting for some electronic components to arrive and so I thought: let's learn some basic functions with the board only. I'll probably use the Arduino MIDI library for further experiments and hopefully circumvent direct reading from the serial port in the future.
The solution linked by Nick looks very smart to me, but at the moment I don't understand the most of it. Especially how to modify it to handle bytes instead of characters and process them after 3 bytes were received instead of after a line break was received. It might take a little while until I am able to use it.
As for doing something after three bytes, well you count bytes received and when you have three of them, you do something. Testing for a line break was just another way of making a decision.