I have code that is sending MIDI via the default USB plug in an Arduino Micro. I was using Control_Surface library which worked fine but was too big for the board plus a display. I only need a couple features; press buttons, send program changes. I have a HobbyTronics USB Host board that works great but I can't get the MIDI messages to go out via RX/TX.
I was hoping the change was simple but I guess that's why ppl use libraries! My working code follows below.
I tried using the transit library USB-MIDI.h a couple different ways but to no avail. Code compiles fine but nothing is sent to the hardware pins. I'm only posting the changed code for MIDI_CREATE_INSTANCE, all the button handling code is the same.
Thanks for any help!
USB MIDI HARDWARE CODE:
#include "OneButton.h"
#include <USB-MIDI.h>
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);
#define PIN_INPUT1 5
#define PIN_INPUT2 6
// tracking program change long press buttons
int counter = 0;
// for musicians this is channel 1 (all from Grumpy Mike!)
byte channel = 0;
// MIDI program change
void programChange(byte channel, byte patch) {
MIDI.sendProgramChange(patch, channel);
}
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button1(PIN_INPUT1, true);
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button2(PIN_INPUT2, true);
// setup code here, to run once:
void setup() {
MIDI.begin();
//... button function attachment code, same as below...
}
FULL CODE WORKS SENDING MIDI TO COMPUTER:
/*
This is a sample sketch to show how to use the OneButtonLibrary
to detect click events on 2 buttons in parallel.
The library internals are explained at
http://www.mathertel.de/Arduino/OneButtonLibrary.aspx
*/
#include "OneButton.h"
#include <MIDIUSB.h>
#define PIN_INPUT1 5
#define PIN_INPUT2 6
//MIDI CODE
int counter = 0;
byte channel = 0; // for musicians this is channel 1
// MIDI program change
void programChange(byte channel, byte patch) {
midiEventPacket_t event = {0x0C, 0xC0 | channel, patch};
MidiUSB.sendMIDI(event);
MidiUSB.flush();
//Serial.println("completes programChange()");
}
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button1(PIN_INPUT1, true);
// Setup a new OneButton on pin PIN_INPUT2.
OneButton button2(PIN_INPUT2, true);
// setup code here, to run once:
void setup() {
// Setup the Serial port. see http://arduino.cc/en/Serial/IfSerial
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting TwoButtons...");
// link the button 1 functions.
button1.attachClick(click1);
button1.attachDoubleClick(doubleclick1);
button1.attachLongPressStart(longPressStart1);
button1.attachLongPressStop(longPressStop1);
button1.attachDuringLongPress(longPress1);
// link the button 2 functions.
button2.attachClick(click2);
button2.attachDoubleClick(doubleclick2);
button2.attachLongPressStart(longPressStart2);
button2.attachLongPressStop(longPressStop2);
button2.attachDuringLongPress(longPress2);
} // setup
// main code here, to run repeatedly:
void loop() {
// keep watching the push buttons:
button1.tick();
button2.tick();
// You can implement other code in here or just wait a while
delay(10);
} // loop
// ----- button 1 callback functions
// This function will be called when the button1 was pressed 1 time (and no 2. button press followed).
void click1() {
Serial.println("Button 1 click.");
programChange(0, ++counter); // Increment preset
} // click1
// This function will be called when the button1 was pressed 2 times in a short timeframe.
void doubleclick1() {
Serial.println("Button 1 doubleclick.");
// Reset MIDI preset to zero
counter = 0;
programChange(0, counter);
} // doubleclick1
// This function will be called once, when the button1 is pressed for a long time.
void longPressStart1() {
Serial.println("Button 1 longPress start");
} // longPressStart1
// This function will be called often, while the button1 is pressed for a long time.
void longPress1() {
Serial.println("Button 1 longPress...");
programChange(0, ++counter); // Increment preset
delay(250);
} // longPress1
// This function will be called once, when the button1 is released after beeing pressed for a long time.
void longPressStop1() {
Serial.println("Button 1 longPress stop");
} // longPressStop1
// ... and the same for button 2:
void click2() {
Serial.println("Button 2 click.");
programChange(0, --counter); // Decrement preset
} // click2
void doubleclick2() {
Serial.println("Button 2 doubleclick.");
} // doubleclick2
void longPressStart2() {
Serial.println("Button 2 longPress start");
} // longPressStart2
void longPress2() {
Serial.println("Button 2 longPress...");
programChange(0, --counter); // Increment preset
delay(250);
} // longPress2
void longPressStop2() {
Serial.println("Button 2 longPress stop");
} // longPressStop2