So I've been trying to send MIDI messages with an Arduino Micro, starting with just trying repeatedly sending with MIDI.sendNoteOn() on the serial port. But no data can be read out of the board. My code is the following:
I've tried reading the serial port with both the serial monitor and xxd /dev/ttyACM0 (on OpenSUSE Linux) and nothing. I did try to communicate with my computer without the library (for instance, Serial.println('a') works as expected). So it's got to be a problem with MIDI specifically...
I tried the solution given by this thread (hence the MIDI_CREATE_INSTANCE(...) line), but to no effect unfortunately.
What's weird is that the built-in LED is always turned ON, which would mean that the problem is actually that the sendNoteOn() method is indefinitely stuck. Looking at the code of send() (which is called by sendNoteOn()`), I don't see why it would get stuck like this though, so it's a bit puzzling...
I'd greatly appreciate any help with that headache,
Thanks in advance !
/*
* MIDIUSB_test.ino
*
* Created: 4/6/2015 10:47:08 AM
* Author: gurbrinder grewal
* Modified by Arduino LLC (2015)
*/
#include "MIDIUSB.h"
// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).
void noteOn(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOn);
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void setup() {
Serial.begin(250000);
Serial1.begin(250000);
}
// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).
void controlChange(byte channel, byte control, byte value) {
midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
MidiUSB.sendMIDI(event);
}
void loop() {
int val;
val = random(20,100);
noteOn(0, val, 64);
MidiUSB.flush();
delay(100);
noteOff(0, val, 64);
MidiUSB.flush();
delay(800);
Serial1.println(val);
// controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}
The MIDI library works with the serial port but needs an adapter to MIDI port, it does not work by USB (it can be connected but changing the baudrate and you need a translator program like the old Hairless MIDI in the computer).
To send messages via USB you must use MIDIUSB as @Grumpy_Mike has shown you.
Sorry for the late reply, I've been testing other things. The MIDIUSB library seems to work indeed (thanks @Grumpy_Mike ), but I still can't figure out how I can read raw binary data from the serial port. Any idea how ?
The code I showed only writes data to the MIDI device. If you want to read data from it then look at the example of how to do this in the MIDIUSB.h library folder. It is called MIDIUSB_read.
I mean how to read on the computer side. I find it quite strange that there doesn't seems to be any option for the serial monitor, instead of reading like plain text... would be quite useful for debugging, outside of MIDI or anything
When using MIDIUSB the arduino acts as a USB Midi device, not as a serial device. The serial monitor doesn't even see that device.
You can use a MIDI software like MIDI-OX on the computer side to see the received midi commands.