Adding more MIDI controls to pisound

Hey,

If you want to add more controls to your pisound audio projects, you can make your own MIDI controller using a couple of potentiometers, push buttons and any Arduino compatible board that uses ATmega32U4 microcontroller. Alternatively, it should be straightforward to adapt the example code to use DIN-5 MIDI ports, as the usbmidi API interface is compatible with Serial API.

See https://github.com/BlokasLabs/usbmidi/tree/master/arduino/libraries/usbmidi/examples/midictrl for full instructions.

4 Likes

Thanks for this, very useful! I’m building an effects unit and want to make an Arduino based MIDI controller with 4 pots :slight_smile:

1 Like

Hello again - question on this one!

I’ve added USBMIDI as a library, running the midictrl Arduino sketch gives me this error:

#error "Targets with both UART0 and CDC serial not supported"

I’ve taken a look but have to admit I’m stumped! Any help appreciated :slight_smile:

Dan

Full output:

arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/792560967 -hardware arduino-builder/hardware -hardware arduino-builder/packages/cores -tools arduino-builder/tools -tools arduino-builder/packages/tools -built-in-libraries arduino-builder/latest -libraries /tmp/612932493/pinned -libraries /tmp/612932493/custom -fqbn arduino:avr:diecimila:cpu=atmega328 -build-cache /tmp -verbose=false /tmp/612932493/midictrl

In file included from /tmp/612932493/custom/usbmidi/src/usbmidi.h:14:0,

from /tmp/612932493/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:235:2: error: #error "Targets with both UART0 and CDC serial not supported"

#error "Targets with both UART0 and CDC serial not supported"

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:233:0,

from /tmp/612932493/custom/usbmidi/src/usbmidi.h:14,

from /tmp/612932493/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:152:16: error: conflicting declaration 'Serial_ Serial'

extern Serial_ Serial;

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:232:0,

from /tmp/612932493/custom/usbmidi/src/usbmidi.h:14,

from /tmp/612932493/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/HardwareSerial.h:143:25: note: previous declaration as 'HardwareSerial Serial'

extern HardwareSerial Serial;

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:233:0,

from /tmp/612932493/custom/usbmidi/src/usbmidi.h:14,

from /tmp/612932493/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:52:32: error: 'EPTYPE1' was not declared in this scope

#define EP_TYPE_BULK_OUT (1<<EPTYPE1)

^

/tmp/612932493/custom/usbmidi/src/usbmidi.cpp:159:2: note: in expansion of macro 'EP_TYPE_BULK_OUT'

EP_TYPE_BULK_OUT,

^

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:51:33: error: 'EPTYPE1' was not declared in this scope

#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))

^

/tmp/612932493/custom/usbmidi/src/usbmidi.cpp:160:2: note: in expansion of macro 'EP_TYPE_BULK_IN'

EP_TYPE_BULK_IN,

^

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:51:48: error: 'EPDIR' was not declared in this scope

#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))

^

/tmp/612932493/custom/usbmidi/src/usbmidi.cpp:160:2: note: in expansion of macro 'EP_TYPE_BULK_IN'

EP_TYPE_BULK_IN,

^

exit status 1

Hey,

Which Arduino board are you using in particular? From the compiler invocation it seems it’s using atmega328 processor which does not have USB support (as atmega32u4 does), so that probably causes the error.

If you are using an Arduino which supports USB, make sure that the correct board is selected in Arduino IDE, Tools->Board menu.

If not, it’s totally fine to use atmega328 and serial MIDI ports rather than USB.

You can use the same code as in https://github.com/BlokasLabs/usbmidi/blob/master/arduino/libraries/usbmidi/examples/midictrl/midictrl.ino except for following changes:

  • Remove USBMIDI library includes
  • Replace USBMIDI with Serial
  • Add Serial.begin(31250); call in the setup() function

It was one of our goals when designing USBMIDI library to make it as compatible to Serial interface as possible :slight_smile: The sketch code should look something like this:

void sendCC(uint8_t channel, uint8_t control, uint8_t value) {
  Serial.write(0xB0 | (channel & 0xf));
  Serial.write(control & 0x7f);
  Serial.write(value & 0x7f);
}

void sendNote(uint8_t channel, uint8_t note, uint8_t velocity) {
  Serial.write((velocity != 0 ? 0x90 : 0x80) | (channel & 0xf));
  Serial.write(note & 0x7f);
  Serial.write(velocity &0x7f);
}

const int ANALOG_PIN_COUNT = 4;
const int BUTTON_PIN_COUNT = 2;

// Change the order of the pins to change the ctrl or note order.
int analogPins[ANALOG_PIN_COUNT] = { A3, A2, A1, A0 };
int buttonPins[BUTTON_PIN_COUNT] = { 9, 8 };

int ccValues[ANALOG_PIN_COUNT];
int buttonDown[BUTTON_PIN_COUNT];

int readCC(int pin) {
  // Convert from 10bit value to 7bit.
  return analogRead(pin) >> 3;
}

int isButtonDown(int pin) {
  return digitalRead(pin) == 0;
}

void setup() {
  Serial.begin(31250);
  // Initialize initial values.
  for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
    pinMode(analogPins[i], INPUT);
    ccValues[i] = readCC(analogPins[i]);
  }

  for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
    pinMode(buttonPins[i], INPUT);
    digitalWrite(buttonPins[i], HIGH);
    buttonDown[i] = isButtonDown(buttonPins[i]);
  }
}

void loop() {
  for (int i=0; i<ANALOG_PIN_COUNT; ++i) {
    int value = readCC(analogPins[i]);

    // Send CC only if th has changed.
    if (ccValues[i] != value) {
      sendCC(0, i, value);
      ccValues[i] = value;
    }
  }

  for (int i=0; i<BUTTON_PIN_COUNT; ++i) {
    int down = isButtonDown(buttonPins[i]);

    if (down != buttonDown[i]) {
      sendNote(0, 64 + i, down ? 127 : 0);
      buttonDown[i] = down;
    }
  }

  // Flush the output.
  Serial.flush();
}
1 Like

Hey - OK its been a busy summer so I’m just getting back to this!

I’m using a Arduino Duemilanove, so decided to use a midi cable, I’ve selected the board in the IDE. I cloned your usb midi repo and added it to my libraries, then called it at the start of the sketch using

// usbmidi - Version: Latest 
#include <midi_serialization.h>
#include <usbmidi.h>

The sketch above looks OK - I’m using A0 - A3 and pins 8&9. I’ve wired the midi plug according to these instructions

When I try to compile the sketch I get this error:

arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/015032148 -hardware arduino-builder/hardware -hardware arduino-builder/packages/cores -tools arduino-builder/tools -tools arduino-builder/packages/tools -built-in-libraries arduino-builder/latest -libraries /tmp/201504137/pinned -libraries /tmp/201504137/custom -fqbn arduino:avr:diecimila:cpu=atmega328 -build-cache /tmp -verbose=false /tmp/201504137/sketch_jul15b

In file included from /tmp/201504137/custom/usbmidi/src/usbmidi.h:14:0,

from /tmp/201504137/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:235:2: error: #error "Targets with both UART0 and CDC serial not supported"

#error "Targets with both UART0 and CDC serial not supported"

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:233:0,

from /tmp/201504137/custom/usbmidi/src/usbmidi.h:14,

from /tmp/201504137/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:152:16: error: conflicting declaration 'Serial_ Serial'

extern Serial_ Serial;

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:232:0,

from /tmp/201504137/custom/usbmidi/src/usbmidi.h:14,

from /tmp/201504137/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/HardwareSerial.h:143:25: note: previous declaration as 'HardwareSerial Serial'

extern HardwareSerial Serial;

^

In file included from /home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/Arduino.h:233:0,

from /tmp/201504137/custom/usbmidi/src/usbmidi.h:14,

from /tmp/201504137/custom/usbmidi/src/usbmidi.cpp:9:

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:52:32: error: 'EPTYPE1' was not declared in this scope

#define EP_TYPE_BULK_OUT (1<<EPTYPE1)

^

/tmp/201504137/custom/usbmidi/src/usbmidi.cpp:159:2: note: in expansion of macro 'EP_TYPE_BULK_OUT'

EP_TYPE_BULK_OUT,

^

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:51:33: error: 'EPTYPE1' was not declared in this scope

#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))

^

/tmp/201504137/custom/usbmidi/src/usbmidi.cpp:160:2: note: in expansion of macro 'EP_TYPE_BULK_IN'

EP_TYPE_BULK_IN,

^

/home/admin/builder/arduino-builder/packages/cores/arduino/avr/cores/arduino/USBAPI.h:51:48: error: 'EPDIR' was not declared in this scope

#define EP_TYPE_BULK_IN ((1<<EPTYPE1) | (1<<EPDIR))

^

/tmp/201504137/custom/usbmidi/src/usbmidi.cpp:160:2: note: in expansion of macro 'EP_TYPE_BULK_IN'

EP_TYPE_BULK_IN,

^

exit status 1

Any ideas? I’m fairly confident the Arduino is wired up correctly - it mirrors what I see in your diagram.

Thanks again :slight_smile:

Dan

Hey, if you’re not using USB for MIDI, then USBMIDI library is not necessary for you. You may use the code I posted here pretty much as is for Serial MIDI.

The errors are actually caused by including <usbmidi.h> on an Arduino board which does not support acting as a USB device.

Hi - thanks for the speedy response!

That’s still not working, the sketch uploads fine, but I’m not getting any control signals coming into Pd on the PiSound. The midi indicator led on the midi input on the PiSound isn’t flashing to show there’s data reaching it, so it may well be hardware or my wiring!

Thanks again

Dan

To verify the MIDI part, you may try modifying loop() to contain only a call to sendNote, so it’s always unconditionally sending some MIDI date. You should be able to see the blinking LED and see the data coming in RPi via call like amidi -phw:1,0 --dump. Here’s a possible loop() code:

void loop() {
  sendNote(0, 64, 100); // Note on
  sendNote(0, 64, 0);   // Note off
}