Let me share my code, which i know doesn't hang Ableton or Guitar Rig
#include "MIDIUSB.h"
#define LED 3 // that pin is PWM capable and has no extra function
#define POT A2
#define CHANNEL (7 - 1) // midi channel 6 equals 5 in this system.
// 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, (uint8_t) (0x90 | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOn);
MidiUSB.flush();
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, (uint8_t) (0x80B | channel), pitch, velocity};
MidiUSB.sendMIDI(noteOff);
MidiUSB.flush();
}
void setup() {
pinMode(LED, OUTPUT);
analogWrite(LED, 120);
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, (uint8_t) (0xB0 | channel), control, value};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
}
void loop() {
static uint32_t moment = millis();
//static bool noteon = true;
static uint8_t volume = 0;
uint8_t analog = analogRead(POT) >> 3;
if ((volume != analog) && (millis() - moment > 20)) {
moment = millis();
volume = analog;
controlChange(CHANNEL, 7, volume);
}
/*if (millis() - moment > 2000) {
moment = millis();
if (noteon) noteOn(0, 48, 100); // pin C2
else noteOff(0, 48, 64);
noteon = !noteon;
}*/
midiEventPacket_t rx = MidiUSB.read();
while (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);
rx = MidiUSB.read();
}
//controlChange(0, 10, 65); // Set the value of controller 10 on channel 0 to 65
}
It is rather similar to yours, so i don't see any issue with your code.. really.
Other possibility is the OS of course, but i am also on Windows (10, i am never upgrading a device with a new OS again, it is just asking for trouble..) I am connecting it through a hub, but i don't think it makes much difference. So i can not re-create your issue, which to means it doesn't really exist. Have you tried several USB cables ? Removing other USB devices etc ? Do you have any other software running that can interfere with the COM ports , like the IDE ? Anyway, I hope it helps, this started out as a test sketch and i now use it as a simple volume button though i am intending to expand it a bit.