MIDI Elements Teensy Library

Written by Tomash on March 8th, 2013. Posted in Controllers, Technology

I have created a wrapper library to make programming MIDI controllers on the Teensy board dead easy and super fast. Simply instantiate object for each component like Buttons, Potentiometer, LED, rotary encoders, etc. and let the library handle all the dirty work of reading the raw values and deciding when to send MIDI signals. Enabling you to experiment and concentrate on your creative ideas and not waste too much time with the repetitive technical details and debugging code.

Just in a couple lines of code you can program a fully functioning MIDI controller.
Here’s an example of how simple things are:

boolean debug=false; // print to serial instead of midi
boolean secondary=true; // enable secondary midi messages
int midiChannel=1; // midi channel number

// declare all your components here
Button but(17,midiChannel,1,secondary,debug); // button 1 on pin 17
Button but2(16,midiChannel,2,secondary,debug); // button 2 on pin 16
Potentiometer pot(45,midiChannel,3,secondary,debug); // knob on pin 45 (A7)
Led led(15,midiChannel,3,true); // led on pin 15 with PWM enabled, triggered on the same number as pot

void setup(){
  usbMIDI.setHandleControlChange(OnControlChange); // set event handler for CC
}

void loop(){
  // add here all the input component reads
  pot.read(); // read knob and send midi messages
  but.read(); // read buttons and send midi messages
  but2.read();
  usbMIDI.read(); // read all the incoming midi messages
}
//====================================================================
// event handlers
void OnControlChange(byte channel, byte control, byte value){
  // add all your output component sets that will trigger with cc
  led.setOn(channel,control,value);
}

Currently only Buttons, Potentiometers and LEDs are supported. But I’m planning on adding RGB LED, rotary encoder, LED bars and ring and button pad classes.
Each component sends two sets of MIDI signals if enabled. For example Buttons send NoteOn, NoteOff, as well as ControlChange values so that they could be easily mapped in Toggle or Instant mode in software like Ableton Live. Also the Potentiometers send secondary NoteOns when values reach 0 or 127, à la MIDI Fighter Pro.

Download: github
To install extract the MIDIElements folder in your arduino/libraries directory

Here are the methods for each class:

Button:
Constructors

Button(byte pin);
Creates a button object with default channel 0, MIDI number 0 and debugging enabled
pin – pin number on Teensy

Button(byte pin, byte channel, byte number);
Creates a button object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent

Button(byte pin, byte channel, byte number, bool secondary);
Creates a button object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent
secondary – send secondary midi signals

Button(byte pin, byte channel, byte number, bool secondary, bool debugging);
Creates a button object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent
secondary – send secondary midi signals
debugging – print messages to serial port instead of midi

Functions
read();
Reads the button state and sends a midi note on if the button was pressed and note off if the button was unpressed on the channel and midi number specified in the constructor

readValue(bool &stateChanged);
Reads the button state, and sets the stateChanged if the state changed from the last time checked, also returns true if the button is pressed and false if not pressed. No MIDI signals are sent.
stateChanged – is set to true after the function exits if the button state changed state

setVelocity(byte velocity);
Changes the velocity value for note on messages, default velocity is 127.
velocity – note on velocity

Potentiometer:
Constructors

Potentiometer(byte pin);
Creates a potentiometer object with default channel 0, MIDI number 0 and debugging enabled
pin – pin number on Teensy

Potentiometer(byte pin, byte channel, byte number);
Creates a potentiometer object on specified channel and MIDI CC number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent

Potentiometer(byte pin, byte channel, byte number, bool secondary);
Creates a potentiometer object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent
secondary – send secondary midi signals

Potentiometer(byte pin, byte channel, byte number, bool secondary, bool debugging);
Creates a potentiometer object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which messages will be sent
number – midi value on which messages will be sent
secondary – send secondary midi signals
debugging – print messages to serial port instead of midi

Functions
read();
Reads the potentiometer value and sends a control changed if the value changed on the channel and midi number specified in the constructor

readValue(bool &stateChanged);
Reads the potentiometer value, and sets the stateChanged if the state changed from the last time checked, also returns the value of the potentiometer. No MIDI signals are sent.
stateChanged – is set to true after the function exits if the button state changed state

Led:
Constructors

Led(byte pin);
Creates a Led object with default channel 0, MIDI number 0 and debugging enabled
pin – pin number on Teensy

Led(byte pin, byte channel, byte number);
Creates a Led object on specified channel and MIDI CC number
pin – pin number on Teensy
channel – midi channel on which led will be triggered
number – midi value on which led will be triggered

Ledr(byte pin, byte channel, byte number, bool pwm);
Creates a Led object on specified channel and MIDI number
pin – pin number on Teensy
channel – midi channel on which led will be triggered
number – midi value on which led will be triggered
pwm – enable pulse width modulation

Functions
setOn(byte channel, byte number, byte velocity);
Call from within Note On or Control Change event handler and pass on the parameters. And the LED will turn on if the messages received correspond to those specified in the constructor.
channel – midi channel of the received message
number – midi number or control change of the received message
velocity – midi velocity or control change value of the received message

setOff(byte channel, byte number, byte velocity);
Call from within Note Off event handler and pass on the parameters. And the LED will turn off if the messages received correspond to those specified in the constructor.
channel – midi channel of the received message
number – midi number or control change of the received message
velocity – midi velocity or control change value of the received message

Tags: , , , , ,

Trackback from your site.

Comments (15)

  • j
    April 2, 2013 at 6:36 pm |

    WOW! this looks awesome!
    I really thank you for sharing this library.
    I’m planning on making my own kind of “spectra midi fighter pro” which includes faders, knobs, arcade buttons and RGB leds, so it’d be great if you updated the library with support for RGB leds.

    Great work!

  • ryan
    June 9, 2013 at 3:52 pm |

    Tomas, this is godsend for me. Ive recently been moving forward to create the midi controller of my dreams but have been stuck at coding. Great thanks for the code. One request, can you add accelerometer blocks? Or/and gyrometers?

  • Ryan
    June 14, 2013 at 4:10 pm |

    any chance you could add sections for an accelerometer?

  • Walter Hoch
    July 2, 2013 at 4:02 pm |

    Does this library you’ve re-writed work on Arduino Mega? Im getting a hard time to make a controller of it.

    • Tomash
      July 2, 2013 at 4:47 pm |

      Its written with the Teensy MIDI library, if you change the basic MIDI commands of each class you could make it work with Mega

  • Cap
    July 24, 2013 at 3:03 am |

    Nice! This is an excellent library and help for my own endeavors. Thank you so much, can’t wait for those updates too! By LED bars and rings you mean something like this? ( http://compare.ebay.com/like/271202265082?var=lv&ltyp=AllFixedPriceItemTypes&var=sbar ) I’ve been wanting to incorporate this into my own Midi controller for a while now. Any idea how to get the knob value from Ableton Live back to the Midi Controller? Some kind of MidiRead function? Thanks again Tomash!

  • July 26, 2013 at 12:24 am |

    Where did you get the case from? or did you make it?

    Wicked project btw!

  • October 30, 2013 at 6:25 am |

    Hey Tomash, awesome work! I got myself a teensy years ago but gave up after I couldn’t figure how to make it generate proper midi. Now I moved to livid brain jr. but I hate the lack of customization.
    Is there any chance of adding FSR sensors to your library?

    Thank you in advance.

    I haven’t met someone who inspired me like you did for a LONG time.

  • Alex
    November 30, 2013 at 8:22 pm |

    This is SO perfect ! With RGB strips support it would be SO SO CRAZY !

  • Alex M
    February 21, 2014 at 4:44 pm |

    great work. can you add encoder support please ?

  • alex m
    March 3, 2014 at 1:10 pm |

    hi there. i have a problem on teensy 3.0 with your code using a 10k pot …. the signal is very noisy at a range of 100 from 127 … ans at this value is sending continuous control change to PC. i am using traktor pro 2. can you help me please ?

  • Cameron
    March 4, 2014 at 11:11 pm |

    This is an awesome resource! Thank you very much for doing this! Any chance you have added Encoders to your MidiElements library?

  • Cap
    April 28, 2014 at 2:42 am |

    BUMP. Any addition of Encoders or Led Rings to the library?

Leave a comment