Skip to content

Commit

Permalink
updated the Arduino MIDI Test for working on both Uno and Leonardo
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgraf committed Jan 29, 2021
1 parent 4a17969 commit 0c8d928
Showing 1 changed file with 64 additions and 18 deletions.
82 changes: 64 additions & 18 deletions doc/ArduMidiTest.ino
Original file line number Diff line number Diff line change
@@ -1,49 +1,95 @@
// -----------------------------------------------------------------------------
// Arduino MIDI Test by H.R.Graf
// - received Note On/Off control built-in LED and are sent back on channel 2
// with slight change in pitch (to demonstrate active functionality)
// - using Baudrate of 115200 over USB
// - compatible to "Hairless MIDI to Serial Bridge"
// - compatible to my pizmidi/midiUartBridge (recommended!)
//
// Works out-of-the-box on Arduino Uno / Leonardo compatible boards
// over USB (No MIDI DIN circuit needed)
//
// Received Note On/Off control built-in LED and are sent back on channel 2
// with slight change in pitch (to demonstrate active functionality).
//
// On the Arduino Uno, the serial interface is used for the USB communication,
// which on the PC is accessible as virtual COM port. There, some additional
// SW is required to translate between UART and MIDI.
// The baud rate of 115200 is compatible to both
// - Hairless MIDI to Serial Bridge
// - my pizmidi/midiUartBridge (recommended!)
//
// On the Arduino Leonardo, the USB MIDI functionality is built-in and the
// serial interface is available for serial monitor / debug messages.
// -----------------------------------------------------------------------------

#include <MIDI.h>
#define BAUD_RATE 115200

struct CustomBaud : public midi::DefaultSettings{
static const long BaudRate = 115200; // e.g. Hairless MIDI to Serial Bridge
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, CustomBaud);
#if defined(ARDUINO_AVR_LEONARDO)
#define LED_BLINK LED_BUILTIN
//#define LED_BLINK 17 // RX LED on Pro Micro

#define DEBUG_INIT(x) Serial.begin(x)
#define DEBUG(x) Serial.print(x)

#include <USB-MIDI.h>
USBMIDI_CREATE_DEFAULT_INSTANCE();
//MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDICoreSerial);

#else // USB <-> UART <-> MIDI
#define LED_BLINK LED_BUILTIN

#define DEBUG_INIT(x) // not available
#define DEBUG(x)

#include <MIDI.h>

struct CustomBaud : public midi::DefaultSettings{
static const long BaudRate = BAUD_RATE; // e.g. Hairless MIDI to Serial Bridge
};
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, CustomBaud);
#endif

// -----------------------------------------------------------------------------

void handleNoteOn(byte channel, byte pitch, byte velocity)
void handleNoteOn(byte channel, byte key, byte velocity)
{
// Do whatever you want when a note is pressed.

digitalWrite(LED_BUILTIN, HIGH);
MIDI.sendNoteOn(pitch+1, velocity, 2);
digitalWrite(LED_BLINK, velocity ? HIGH : LOW);
MIDI.sendNoteOn(key+1, velocity, 2);
DEBUG("NoteOn: ");
DEBUG(key);
DEBUG(" @ ");
DEBUG(velocity);
DEBUG("\n");
}

void handleNoteOff(byte channel, byte pitch, byte velocity)
void handleNoteOff(byte channel, byte key, byte velocity)
{
// Do something when the note is released.
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.

digitalWrite(LED_BUILTIN, LOW);
MIDI.sendNoteOff(pitch+1, velocity, 2);
digitalWrite(LED_BLINK, LOW);
MIDI.sendNoteOff(key+1, velocity, 2);
DEBUG("NoteOff: ");
DEBUG(key);
DEBUG(" @ ");
DEBUG(velocity);
DEBUG("\n");
}

// -----------------------------------------------------------------------------

void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
DEBUG_INIT(BAUD_RATE);
DEBUG("ArduMidiTest");

pinMode(LED_BLINK, OUTPUT);
digitalWrite(LED_BLINK, LOW);

MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI);
MIDI.turnThruOff();

DEBUG("Ready");
}

void loop()
Expand Down

0 comments on commit 0c8d928

Please sign in to comment.