-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArduMidiTest.ino
100 lines (80 loc) · 2.87 KB
/
ArduMidiTest.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// -----------------------------------------------------------------------------
// Arduino MIDI Test by H.R.Graf
//
// 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.
// -----------------------------------------------------------------------------
#define BAUD_RATE 115200
#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 key, byte velocity)
{
// Do whatever you want when a note is pressed.
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 key, byte velocity)
{
// Do something when the note is released.
// Note that NoteOn messages with 0 velocity are interpreted as NoteOffs.
digitalWrite(LED_BLINK, LOW);
MIDI.sendNoteOff(key+1, velocity, 2);
DEBUG("NoteOff: ");
DEBUG(key);
DEBUG(" @ ");
DEBUG(velocity);
DEBUG("\n");
}
// -----------------------------------------------------------------------------
void setup()
{
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()
{
MIDI.read();
}
// -----------------------------------------------------------------------------