Need help with simple Trellis 4x4 buttons sending midi note on/off messages.

Hello,

I’m trying to set up my Trellis to send MIDI on/off messages. The hardware I’m using is a Trellis (original) and a Teensy 4.1, to send MIDI proper, but USBMIDI would be a plus as well.

The code is based off of the Trellis Test code that comes with the Trellis library, mixed with another code for a simple midi controller with Trellis. The Trellis works as intended with the Trellis Test code, but wouldn’t even respond with the MIDI controller code, so I figured entering the MIDI-specific data from the latter might work. Nope. The Trellis Test part of the code works, but MIDI Monitor 2 on my Macbook picks up no data at all. So yeah, lights up, no MIDI.

Help please. Here is the code

#include <Wire.h>
#include "Adafruit_Trellis.h"
#include <MIDI.h>

#define MOMENTARY 0
#define MODE MOMENTARY
#define NUMTRELLIS 1
#define numKeys (NUMTRELLIS * 16)
#define INTPIN 13

Adafruit_Trellis matrix0 = Adafruit_Trellis();
Adafruit_TrellisSet trellis = Adafruit_TrellisSet(&matrix0);

int velocity = 100;
int noteON = 144;
int noteOFF = 128;

void setup() {
    pinMode(INTPIN, INPUT);
  digitalWrite(INTPIN, HIGH);
}
void loop() {
  delay(30);
  if (MODE == MOMENTARY) {
  if (trellis.readSwitches()) {
      for (int8_t i=0; i<numKeys; i++) {
        if (trellis.justPressed(i)) {
          MIDImessage(noteON, 48+1, velocity);
          trellis.setLED(i);      
        }
        if (trellis.justReleased(i)) {
          MIDImessage(noteOFF, 48+i, velocity);
          trellis.clrLED(i);
        }
      }
      trellis.writeDisplay();
  }
 }
}

void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);
  Serial.write(MIDInote);
  Serial.write(MIDIvelocity);
}

Please note: I’m incredibly new to coding. Although I’ve sunk several hours into understanding Arduino basics (using Paul McWhorter’s YouTube channel), this really isn’t my specialty, and a lot of the concepts still go over my head. So if you look at this and think “Why the heck did you put this here?” My answer is probably “I don’t know, I just copied and pasted it.”

Thank you for your patience.

Your code sends MIDI data to the serial port. Your operating system's MIDI drivers don't pick that up. You need the Teensy to present itself as a USB MIDI device by selecting the appropriate USB option in the IDE's "Tools" menu, and use the "usbMidi" object to send MIDI, see Teensyduino: Using USB MIDI with Teensy on the Arduino IDE.

Pieter

Strange, I seem to remember a sketch using the standard MIDI library being recognized.

However, the board is set to MIDI, which is what is confusing me.

I’m sorry, I posted the wrong code originally, here is the correct one tat I have added MIDIUSB to:

 #include <Wire.h>
#include "Adafruit_Trellis.h"
#include <MIDI.h>
#include <MIDIUSB.h> 

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

#define MOMENTARY 0
#define LATCHING 1
// set the mode here
#define MODE MOMENTARY

int velocity = 100;
int noteON = 144;
int noteOFF = 128;
const int channel = 1;

Adafruit_Trellis matrix0 = Adafruit_Trellis();

Adafruit_TrellisSet trellis =  Adafruit_TrellisSet(&matrix0);

#define NUMTRELLIS 1
#define numKeys (NUMTRELLIS * 16)
#define INTPIN 13


void setup() {
  Serial.begin(9600);
  Serial.println("Trellis Demo");
MIDI.begin(MIDI_CHANNEL_OMNI);

  // INT pin requires a pullup
  pinMode(INTPIN, INPUT);
  digitalWrite(INTPIN, HIGH);

  trellis.begin(0x70);  // only one

  // light up all the LEDs in order
  for (uint8_t i=0; i<numKeys; i++) {
    trellis.setLED(i);
    trellis.writeDisplay();    
    delay(50);
  }
  // then turn them off
  for (uint8_t i=0; i<numKeys; i++) {
    trellis.clrLED(i);
    trellis.writeDisplay();    
    delay(50);
  }
}


void loop() {
  delay(30); // 30ms delay is required, dont remove me!
  
  if (MODE == MOMENTARY) {
    if (trellis.readSwitches()) {
      for (uint8_t i=0; i<numKeys; i++) {
	        if (trellis.justPressed(i)) {{
          MIDI.sendNoteOn(48+1, velocity, 0);
          usbMIDI.sendNoteOn(48+1, velocity, 0);
          trellis.setLED(i);  
 }
	  Serial.print("v"); Serial.println(i);
	  trellis.setLED(i);
	} 
	// if it was released, turn it off
	if (trellis.justReleased(i)) {
	        MIDImessage(noteOFF, 48+i, velocity);
          trellis.clrLED(i);
	        Serial.print("^"); Serial.println(i);
	        trellis.clrLED(i);
	}
      }
      trellis.writeDisplay();
    }
  }

  if (MODE == LATCHING) {
    // If a button was just pressed or released...
    if (trellis.readSwitches()) {
      // go through every button
      for (uint8_t i=0; i<numKeys; i++) {
        // if it was pressed...
	if (trellis.justPressed(i)) 
	  Serial.print("v"); Serial.println(i);
	  // Alternate the LED
	  if (trellis.isLED(i))
	    trellis.clrLED(i);
	  else
	    trellis.setLED(i);
        } 
      }
      // tell the trellis to set the LEDs we requested
      trellis.writeDisplay();
    }
  }
  void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);
  Serial.write(MIDInote);
  Serial.write(MIDIvelocity);
}

You include two different MIDI libraries, and then don't use them. You don't need any MIDI libraries to send MIDI USB on Teensy, it's included in the Core. Please see the link I posted in my previous reply.

Thank you for your help. For context, this is my first microcontroller project and it has been entirely overwhelming and I have gone largely off of guess work without guidance.

I have followed Paul McWhorter's YouTube videos about Arduino, which are very helpful, but I haven't been able to connect the dots between his lessons, and setting up MIDI specifically.

I've visited the Teensy MIDI pages (both MIDI Library and MIDIUSB) several times to see if what I learned plugs into the information there, and I'm still having a hard time understanding the information. I'm sure it's hard for people to believe that I need any simpler directions than the ones found on those pages, nobody less so than myself.

Can you provide me with any tutorials to take me from where I am now (overwhelmed with patchy knowledge of how Arduino works in general) to where I want to be (has built a midi controller with the Trellis and a few pots). Thank you.

There are three main parts here: 1. read the buttons of the Trellis to see if any of them is pressed or released, 2. if that’s the case, send the appropriate MIDI message, and 3. turn on/off the LED for that button.

Parts 1 and 3 are already implemented in the code you posted, as far as I can see, you just need to update the sketch so it sends MIDI over USB.

To send a MIDI Note message, you use usbMIDI.sendNoteOn(note, velocity, channel); as explained on the page I linked to.

I stripped out the unnecessary parts of your code to make it easier to understand and added some comments:

#include <Adafruit_Trellis.h>

const uint8_t baseNote = 48;  // MIDI note C3 (one octave below middle C)
const uint8_t velocity = 127; // Velocity of MIDI notes (how hard you press the key)
const uint8_t channel = 1;    // MIDI channel

Adafruit_Trellis matrix0 = Adafruit_Trellis();
Adafruit_TrellisSet trellis =  Adafruit_TrellisSet(&matrix0);

const uint8_t numTrellis = 1;
const uint8_t numKeys = numTrellis * 16;

void setup() {
  trellis.begin(0x70); // Initialize the Trellis at the given I²C address 
}

void loop() {
  delay(30); // Lazy software switch debouncing

  if (trellis.readSwitches()) { // Reads all switches and returns true if any of them changed
    for (uint8_t i = 0; i < numKeys; i++) { // Iterate over all switches
      if (trellis.justPressed(i)) { // If the i-th switch was pressed
        usbMIDI.sendNoteOn(baseNote + i, velocity, channel); // Send a MIDI note on event
        trellis.setLED(i);                                   // and turn on the corresponding LED
      }
      if (trellis.justReleased(i)) { // If it was released
        usbMIDI.sendNoteOff(baseNote + i, velocity, channel); // Send a MIDI note off event
        trellis.clrLED(i);                                    // and turn off the corresponding LED
      }
    }
    trellis.writeDisplay(); // Actually update the states of the LEDs (they are buffered)
  }
}

It compiles for me, but I don’t own a Trellis to test it with.
Try to go through it line by line, and try to understand what each line does. If it’s not clear, you can ask concrete questions about it here.

It works! This is so great! Thank you so much! The only thing left for me to do now is study this intensely to wrap my mind around how it works.

And thank you for breaking it down as such. It's much clearer now. The plan from here is to continue learning about the Arduino environment in general so that the pieces come together into a whole picture.

I will also be adding potentiometers (without a multiplexer), and I will take this information and apply it to adding the pots. Thanks again.

Although I've sunk several hours into understanding Arduino basics (using Paul McWhorter's YouTube channel),

Watching videos is a very poor way to learn. Get used to reading words, then you can go at your own pace. Best of all if you don’t understand anything you can ask specific questions here.
Not many people are willing to sit through long videos looking for a phrase that is troubling you. Most videos I have seen on this subject are crap.

1 Like