Software

Install UNTZtrument + Trellis Arduino Libraries

Follow the guide on how to setup the arduino IDE with the special UNTZtrument libraries that will allow you to program the Leonardo to perform like a native USB MIDI device.

Install Teensyduino to Mod Arduino IDE

You will need to install the Tennsyduino for your operating system. It's a special damon that updates your install of the Arduino IDE to have that special menu for uploading MIDI code to the Leonardo. Click the link below to get instructions on installing Teensyduino.
Ensure the TeeOnArdu + UNTZtrument library are installed!

Uploading Sketches to Leonardo MIDI

In certain cases, once the Arduino Leonardo has the MIDI uploaded, it won't be able to upload new code until the reset button is pressed. Keep this in mind when attempting to upload new sketches.

MIDI Loop

The code below uses the usbMIDI.sendNoteOn and usbMIDI.sendNoteOff calls for each button to change the state of the MIDI note. The note[i] variable is set in the table. 127 is the velocity of the midi note, being the highest/hardest. Channel is a reference that is defined in the top of the sketch.
Download: file
for(uint8_t i=0; i<16; i++) { // For each button...
        if(trellis.justPressed(i)) {
          usbMIDI.sendNoteOn(note[i], 127, CHANNEL);
          
          trellis.setLED(i);
        } else if(trellis.justReleased(i)) {
          usbMIDI.sendNoteOff(note[i], 0, CHANNEL);
          trellis.clrLED(i);
        }
      }

MIDI Note Mapping

This table lists the midi notes that are mapped the 16 buttons on the Trellis. The values are ordered just like the 4x4 grid on the Trellis. "60" is equal to C3, while "48" will trigger note C2. The full range of notes in this table are visually represented below. This map syncs perfectly with the TRG-16 performance pad in NanoStudio. For a list of the flu range of available notes, check out this page.
Download: file
uint8_t note[] = {
  60, 61, 62, 63,
  56, 57, 58, 59,
  52, 53, 54, 55,
  48, 49, 50, 51
};

Potentiometers MIDI CC

The 4 potentiometers are mapped to MIDI CC 1, 11, 12 and 13. The blocks of code below use the usbMIDI.sendControlChange call to define which potentiometer will be mapped to a MIDI CC. The analogRead(#) call refers to the analog input on the Arduino Leonardo.
Download: file
  mod = map(analogRead(0), 0, 1023, 0, 127);
  vel = map(analogRead(1), 0, 1023, 0, 127);
  fxc = map(analogRead(2), 0, 1023, 0, 127);
  rate = map(analogRead(3),0, 1023, 0, 127);
  usbMIDI.sendControlChange(1, mod, CHANNEL);
  usbMIDI.sendControlChange(11, vel, CHANNEL);
  usbMIDI.sendControlChange(12, fxc, CHANNEL);
  usbMIDI.sendControlChange(13, rate, CHANNEL);
pot = map(ArdiunoPin(#)), 0(lowest MIDI value), 1023(highest MIDI value);
usbMIDI.sencControlChange(Arduino Pin, pot, CHANNEL);
Download: file
uint8_t newModulation = map(analogRead(0), 0, 1023, 0, 127);
    if(mod != newModulation) {
      mod = newModulation;
      usbMIDI.sendControlChange(1, mod, CHANNEL);
    }

MINI UNTZtrument Sketch

Copy and paste the full code below into a new sketch. The 16 buttons on the Trellis are mapped to trigger LEDs+MIDI notes. 4 potentiometers are mapped to MIDI CCs.

Download: file
#include <Wire.h>
#include <Adafruit_Trellis.h>

#define LED     13 // Pin for heartbeat LED (shows code is working)
#define CHANNEL 1  // MIDI channel number

Adafruit_Trellis trellis;

uint8_t       heart        = 0;  // Heartbeat LED counter
unsigned long prevReadTime = 0L; // Keypad polling timer
uint8_t       mod;
uint8_t       vel;
uint8_t       fxc;
uint8_t       rate;  

uint8_t note[] = {
  60, 61, 62, 63,
  56, 57, 58, 59,
  52, 53, 54, 55,
  48, 49, 50, 51
};

void setup() {
  pinMode(LED, OUTPUT);
  trellis.begin(0x70); // Pass I2C address
#ifdef __AVR__
  // Default Arduino I2C speed is 100 KHz, but the HT16K33 supports
  // 400 KHz.  We can force this for faster read & refresh, but may
  // break compatibility with other I2C devices...so be prepared to
  // comment this out, or save & restore value as needed.
  TWBR = 12;
#endif
  trellis.clear();
  trellis.writeDisplay();
  mod = map(analogRead(0), 0, 1023, 0, 127);
  vel = map(analogRead(1), 0, 1023, 0, 127);
  fxc = map(analogRead(2), 0, 1023, 0, 127);
  rate = map(analogRead(3),0, 1023, 0, 127);
  usbMIDI.sendControlChange(1, mod, CHANNEL);
  usbMIDI.sendControlChange(11, vel, CHANNEL);
  usbMIDI.sendControlChange(12, fxc, CHANNEL);
  usbMIDI.sendControlChange(13, rate, CHANNEL);
}

void loop() {
  unsigned long t = millis();
  if((t - prevReadTime) >= 20L) { // 20ms = min Trellis poll time
    if(trellis.readSwitches()) {  // Button state change?
      
      for(uint8_t i=0; i<16; i++) { // For each button...
        if(trellis.justPressed(i)) {
          usbMIDI.sendNoteOn(note[i], 127, CHANNEL);
          
          trellis.setLED(i);
        } else if(trellis.justReleased(i)) {
          usbMIDI.sendNoteOff(note[i], 0, CHANNEL);
          trellis.clrLED(i);
        }
      }
      trellis.writeDisplay();
    }
    uint8_t newModulation = map(analogRead(0), 0, 1023, 0, 127);
    if(mod != newModulation) {
      mod = newModulation;
      usbMIDI.sendControlChange(1, mod, CHANNEL);
    }
    uint8_t newVelocity = map(analogRead(1), 0, 1023, 0, 127);
    if(vel != newVelocity) {
      vel = newVelocity;
      usbMIDI.sendControlChange(11, vel, CHANNEL);
    }
    uint8_t newEffect = map(analogRead(2), 0, 1023, 0, 127);
    if(fxc != newEffect) {
      fxc = newEffect;
      usbMIDI.sendControlChange(12, fxc, CHANNEL);
    }
    uint8_t newRate = map(analogRead(3), 0, 1023, 0, 127);
    if(rate !=newRate) {
      rate = newRate;
      usbMIDI.sendControlChange(13, rate, CHANNEL);
    }
    prevReadTime = t;
    digitalWrite(LED, ++heart & 32); // Blink = alive
  }
  while(usbMIDI.read()); // Discard incoming MIDI messages
}

Issues, Problems, Need Help?

If you encounter any technical problems with the Arduino software, please post up your issue, including photos of your wiring and any error/warning text from the Arduino IDE on the Adafruit Forums. Our support team will be able to help you there.

This guide was first published on Jul 30, 2014. It was last updated on Jul 30, 2014. This page (Software) was last updated on Dec 12, 2019.