USB Midi library

Hi ich bin noch kompletter Neuling in der Materie und brauche ein wenig Hilfe. Ich habe mir die letzten Wochen ein einen Controller zusammengelötet für Ableton. Insgesamt sind es 12 Potis die über zwei 4051 Multiplexer laufen. Als Chip habe ich einen Leonardo Klon. Das Ding ist der Code den ich unten angefügt habe funktioniert insoweit das ich über die Hairless Bridge den Controller mit meiner DAW verbinden kann. Allerdings spinnt das auch manchmal und wird dann wieder nicht erkannt. Ich wollte jetzt mit der MIDIusb library dafür sorgen, dass ich die Hairless Bridge nicht mehr brauche aber das übersteigt mein Verständnis bei weitem. Ich finde nur leider keine geeignete Anleitung. Kann mir jemand erklären wie ich die Bibliothek verwende und ob das mit meinem Code überhaupt kompatibel ist. Freue mich auf eure Antworten. Danke. Leo


int controlChange = 176; //MIDI Kanal 1
int controllerNummer[] = {20,21,22,23,24,25,26,27,28,29,30,31,32};
int potiWert[12];
int controllerWert[12];
int controllerWertAlt[12];

int i = 0;

int bit1 = 0;
int bit2 = 0;
int bit3 = 0;

void setup() {
  //Select Pins 4051
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);

  Serial.begin(115200);
}

void loop() {
  for (i = 0; i <= 5; i++) {
    bit1 = bitRead(i,0);
    bit2 = bitRead(i,1);
    bit3 = bitRead(i,2);

    digitalWrite(2, bit1);
    digitalWrite(3, bit2);
    digitalWrite(4, bit3);

    potisAbfragen(i,A0);
    potisAbfragen(i+6,A1);
    }
}

void potisAbfragen(int zaehler, int analogPin) {
  potiWert[zaehler] = 0.2 * potiWert[zaehler] + 0.8 * analogRead(analogPin);
  controllerWert[zaehler] = map(potiWert[zaehler],0,1023,0,127);
  if (controllerWert[zaehler] != controllerWertAlt[zaehler]) {
    sendeMIDI(controlChange, controllerNummer[zaehler], controllerWert[zaehler]);
    controllerWertAlt[zaehler] = controllerWert[zaehler];
    }
  }

void sendeMIDI(int statusByte, int dataByte1, int dataByte2) {
  Serial.write(statusByte);
  Serial.write(dataByte1);
  Serial.write(dataByte2);
  }

What exactly are you struggling with? Did you see the examples that come with the MIDIUSB library, e.g. https://github.com/arduino-libraries/MIDIUSB/blob/master/examples/MIDIUSB_write/MIDIUSB_write.ino?

The MIDIUSB library only handles the USB communication, it leaves constructing and parsing MIDI messages to the user, which might not be what you want. There are libraries such as the Control Surface library I maintain that provide easier to use higher-level abstractions, that allow you to write midi.sendControlChange(controller, value).

It also supports multiplexers and potentiometers out of the box:

#include <Control_Surface.h> // https://github.com/tttapa/Control-Surface
 
USBMIDI_Interface midi; // Instantiate a MIDI Interface to use
 
// Instantiate two analog multiplexers
CD74HC4051 muxA { A0, {2, 3, 4} };
CD74HC4051 muxB { A1, {2, 3, 4} };
 
// Create an array of CCPotentiometer objects that send
// out MIDI Control Change messages when you turn the
// potentiometers connected to the pins of the multiplexers
CCPotentiometer potentiometers[] {
  {muxA.pin(0), 20},
  {muxA.pin(1), 21},
  {muxA.pin(2), 22},
  {muxA.pin(3), 23},
  {muxA.pin(4), 24},
  {muxA.pin(5), 25},
  {muxB.pin(0), 26},
  {muxB.pin(1), 27},
  {muxB.pin(2), 28},
  {muxB.pin(3), 29},
  {muxB.pin(4), 30},
  {muxB.pin(5), 31},
};
 
// Initialize the Control Surface
void setup() { Control_Surface.begin(); }
 
// Update the Control Surface
void loop() { Control_Surface.loop(); }

For more details, see Control Surface: Getting Started.

If you just want to add USB MIDI functionality to your current code, you could do something like this:

#include <Control_Surface.h>

USBMIDI_Interface midi;

// ... 

void setup() {
  midi.begin();
  // ...
}

void loop() {
  midi.update();
  // ...
}

// ... 

void potisAbfragen(int zaehler, int analogPin) {
  // ...
  if (controllerWert[zaehler] != controllerWertAlt[zaehler]) {
    midi.sendControlChange(controllerNummer[zaehler], controllerWert[zaehler]);
    controllerWertAlt[zaehler] = controllerWert[zaehler];
  }
}

More information in Control Surface: MIDI Tutorial.

Hey PieterP,
thank you for answering. i´ve build a controller with 12 potis and wrote this code for it. It works but only with the Hairless Midi bridge. Now I want to add something which allows that Ableton can recognize my controller as a MIDI usb controller and send the data without the hairless midi bridge. I´ve tried your second example but it doesnt work. I got this error: 'class CS::USBMIDI_Interface' has no member named 'sendcontrolChange'

I don´t know much about programming and I dont understand whats going on hahah

Maybe it's easier for you to use the USBMIDI lib. This lib can be used nearly just the same way as Serial, but it registers as a midi device on the USB. It should be easy to change your sketch from Serial to USBMIDI.
The lib can be installed by means of the library manager.

Hello again!
I wrote a new Code with the Control Surface library and now it works how I want. Thank you for your help. Do you know if I´m able to change the name of the Board which is showed in my Midi settings in the daw?

I show you the new code if your intrested in.

#include <Control_Surface.h>
#include <Def/MIDIAddress.hpp>

USBMIDI_Interface midi;

CD74HC4051 mux1 {A0, {2, 3, 4}};
CD74HC4051 mux2 {A1, {2, 3, 4}};


CCPotentiometer multiplexerA[] {
  { mux1.pin(0), {21, CHANNEL_1}},
  { mux1.pin(1), {22, CHANNEL_1}},
  { mux1.pin(2), {23, CHANNEL_1}},
  { mux1.pin(3), {24, CHANNEL_1}},
  { mux1.pin(4), {25, CHANNEL_1}},
  { mux1.pin(5), {26, CHANNEL_1}},
  };

CCPotentiometer multiplexerB[] {
  { mux2.pin(0), {27, CHANNEL_1}},
  { mux2.pin(1), {28, CHANNEL_1}},
  { mux2.pin(2), {29, CHANNEL_1}},
  { mux2.pin(3), {30, CHANNEL_1}},
  { mux2.pin(4), {31, CHANNEL_1}},
  { mux2.pin(5), {32, CHANNEL_1}},
  
  };


void setup() {
  Control_Surface.begin();
}

void loop() {

 Control_Surface.loop();
}

My bad, I should have more explicit about that, sendControlChange is the name of the function in the current master version of the library, if you installed version 1.2.0, you have to use sendCC instead (this also works in the master version for backwards compatibility).

Glad to hear it works now!

See this thread: How to change de MIDI-USB device name. · Issue #37 · arduino-libraries/MIDIUSB · GitHub

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.