Reading MIDI and Serial Monitor behavior.

Hi, everyone!
My first post here! :slight_smile:

I'm trying to make a sketch to read midi messages from my MIDI keyboard. So I made a circuit and tested it with code below. Everything works fine, but here is an issue - The Serial Monitor displays "?" symbols, the Arduino transmits something all the time, TX led is on.

Could you explain me, why the Arduino is sending messages over serial while midi keyboard is connected to RX? And how get rid of it. Thanks!

#include <MIDI.h> // Add Midi Library

#define LED 13 // Arduino Board LED is on Pin 13

//Create an instance of the library with default name, serial port and settings
MIDI_CREATE_DEFAULT_INSTANCE();

void setup() {
pinMode (LED, OUTPUT); // Set Arduino board pin 13 to output
MIDI.begin(MIDI_CHANNEL_OMNI); // Initialize the Midi Library.
// OMNI sets it to listen to all channels.. MIDI.begin(2) would set it
// to respond to notes on channel 2 only.
MIDI.setHandleNoteOn(MyHandleNoteOn); // This is important!! This command
// tells the Midi Library which function you want to call when a NOTE ON command
// is received. In this case it's "MyHandleNoteOn".
MIDI.setHandleNoteOff(MyHandleNoteOff); // This command tells the Midi Library
// to call "MyHandleNoteOff" when a NOTE OFF command is received.
}

void loop() { // Main loop
MIDI.read(); // Continuously check if Midi data has been received.
}

// MyHandleNoteON is the function that will be called by the Midi Library
// when a MIDI NOTE ON message is received.
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
digitalWrite(LED,HIGH); //Turn LED on
}

// MyHandleNoteOFF is the function that will be called by the Midi Library
// when a MIDI NOTE OFF message is received.
// * A NOTE ON message with Velocity = 0 will be treated as a NOTE OFF message *
// It will be passed bytes for Channel, Pitch, and Velocity
void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
digitalWrite(LED,LOW); //Turn LED off
}

You forgot to mention what board you're using, so I'll assume an Arduino Uno.

The first thing to keep in mind is that the ATmega328P chip on an Arduino Uno only has a single UART (Serial port). This port is connected to the TX/RX pins on the IO header, and to the on-board Serial-to-USB converter.
If you send anything using the TX pin, it'll also be sent over USB. Similarly, you can only receive input from one source at a time, either the RX pin, or the USB connection.

Second, the MIDI library you're using automatically mirrors all MIDI messages it receives on the input to the output. That was a strange design decision in my opinion, but it's something they're stuck with now for the sake of backwards compatibility. You can disable it using MIDI.turnThruOff() in your setup.

Finally, the reason you're seeing question marks in the Serial monitor is because the Serial-to-USB converter runs at a baud rate other than the MIDI baud rate, so all it sees is gibberish. Even if you would be able to select the correct baud rate in the Serial monitor, you would still get some strange results, because MIDI is a binary protocol, and the Serial monitor only displays ASCII (UTF-8?) text.

Pieter

Second, the MIDI library you're using automatically mirrors all MIDI messages it receives on the input to the output. That was a strange design decision in my opinion, but it's something they're stuck with now for the sake of backwards compatibility. You can disable it using MIDI.turnThruOff() in your setup.

I should have guessed that it can be mirrored! Thanks, it helps.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.