3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.


A Framework For Making Affordable & Stylish Modular Controllers (USB to MIDI, HID, or Serial)

Step 11All About The Code

All About The Code

I did my absolute best to make the code user-friendly. The code includes many comments to help you understand what is going on. You are not required to write any code, more-so, just fill in the blanks. Easy. Only if you want to add some LED features will you need to append some basic code. There is an LED example provided within the code to get you started.   

I was never trained as a coder, but I do consider this code to be pretty damn good! Thanks to my brother Neil for help with some of the more difficult sections.

The code is broken up into five sections: 
  1. Edit - The section where you are required to make the appropriate edits related to your controller using a fill-in-the-blanks method.
  2. Variables and functions - Holds the variables and functions used by the code. 
  3. Setup - This is where we define out pin configuration, informing the Teensy which pin in an input and which is an output.
  4. Loop - The brains of our whole operation. This function loops over and over while the Teensy is running. 
  5. Communication functions - These functions handle our messages.
Within these five sections you will often find the subsections for pushbuttons, LEDs, analog-in Teensy, analog-in multiplexer, and encoders.

If you are not using Traktor software, you will likely have to edit the rotary encoder MIDI messages. Check your software's documentation to understand the expected message format. When you figure it out please message me so I can add it to this Instructable. 

Although it is not imperative to understand all the code, I just want to explain one snippet of code which will help you understand how the control messages work. The code below sends analog messages for a potentiometer. There are three modes (debug, I2C, and MIDI) which you turn on/off in the edits. If debug is enabled, messages will be sent through USB to the serial monitor to help with debugging. 'serialDebugOut' is a very basic function I wrote which sends these serial messages and it can be viewed at the bottom of the code. If I2C is enabled, messages will be sent through I2C to the master module which will then forward the messages through USB. If MIDI is enabled, then this controller will send MIDI messages directly through USB to the computer. 'usbMIDI.' is a specific message used by Teensy++ for MIDI messages. Read more about Teensy MIDI here. Keep in mind you can edit the code to send many kinds of messages: Teensy USB Serial, Teensy USB Keyboard, Teensy USB Mouse, Teensy USB Joystick, Teensy USB MIDI, and Arduino Serial.

        if(enableDebug==1){ //SERIAL debug is on
          serialDebugOut("Analog",i,tempAnalogInMap);
        }
        else if(enableI2C==1){ //I2C
          serialI2COut('a',i,tempAnalogInMap,channelNumber);
        }
        else{ //MIDI
          usbMIDI.sendControlChange(i,tempAnalogInMap,channelNumber);  
        }

Read the code/comments over and over, it make take a few iterations to wrap your head around. You can learn a lot from the code. Constantly reference the Arduino library for sections you don't understand. 

The rotary encoder code is a nasty beast. If you are feeling ambitious you can read more about it here

**Likely there will be some improvements made to the code over time so continue to check back for the latest version.**

All code can be found here at GitHub. For this section use the code titled 'Controller Code'. Click 'ZIP' in the top-left to download. 



« Previous StepDownload PDFView All StepsNext Step »
4 comments
Jan 22, 2012. 3:02 PMXTL says:
Hope you don't mind. I made some minor edits here for clarity (to me).
I also tried to separate the *edit* sections a little more from the "please do not edit" sections :-) You have one codeblock to edit - the LED section - the rest is pin connections, and modes of behaviour.
Still have question about nature of SHIFT key and what it does in this context.

Very nice Instructable. super useful. thanks
changes-xtl
Jan 30, 2012. 6:28 AMjkinney says:
I'm working on the code now for my own board, do i have to enable the shift button in both the pushbutton category and the specific shift category?
Jan 30, 2012. 11:35 AMjkinney says:
sorry about that. i'm looking at the changed version of the code that XTL posted. Thanks for the help :D

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
66
Followers
2
Author:Fuzzy-Wobble
I am a human from planet earth. I use my brain to make things. fuzzywobble.com