Yeah i was wondering about that, i mean that is the example ? When i saw it i was already confused. The example i kept for myself was this.
#include "MIDIUSB.h"
#define LED 3
// 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);
MidiUSB.flush();
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
MidiUSB.flush();
}
void setup() {
//Serial.begin(115200);
pinMode(LED, OUTPUT);
analogWrite(LED, 20);
delay(1000);
analogWrite(LED, 0);
}
// 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);
MidiUSB.flush();
}
void loop() {
static uint32_t moment = millis();
static bool noteon = true;
midiEventPacket_t rx;
if (millis() - moment > 2000) {
moment = millis();
if (noteon) noteOn(0, 48, 100); // pin C2
else noteOff(0, 48, 64);
noteon = !noteon;
}
do {
rx = MidiUSB.read();
if (rx.header != 0) {
uint8_t type = rx.byte1 >> 4;
uint8_t channel = rx.byte1 & 0xF;
uint8_t value1 = rx.byte2;
uint8_t value2 = rx.byte3;
if (type == 0x9) analogWrite(LED, value2 * 2);
if (type == 0x8) analogWrite(LED, 0);
}
} while (rx.header != 0);
// controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}
I puit a LED + resistor on pin 3 and used it's PWM ability.
Did some sending in the opposite way just to confirm. Of course the Serial.port is used for the MIDI, and setting the baud rate and writing BS data (as in not MIDI) is probably not such a great idea.
Mind you the Leonardo has Serial1 connected to it's 0 & 1 pins i think just like the micro. You could connect to that if you really want debug information.
I do recommend you look into the Control-Surface library. It is really great and well supported.
Just to keep going on the current path will also get your where you want i think.