Teensy Arduino USB Tap Tempo for Axe-FX

Here’s a simple hardware project that would be a good first-time project for anyone interested in creating their own USB MIDI device.

For this project I used a small and inexpensive USB-based microcontroller board called the Teensy 2.0. This tiny board is packed with features: 25 I/O pins, 12 analog inputs, 7 PWM outputs, 16MHz processor, USB 2.0 full-speed/low-speed, UART, SPI, and I2C.

Teensy 2.0 development board

I used the free Arduino software development platform for this project. The Arduino language is similar to C, so anyone familiar with C will feel right at home. The company that makes the Teensy provides an extension to Arduino called TeensyDuino, which adds support for the USB 2.0 port. Making a USB device could not be easier.

I actually RTFM, and followed the setup instructions for the Arduino and Teensy software. Setup was quick and easy, and in 10 minutes I was downloading HEX files to the Teensy board with the Teensy Loader. I like the way the compiler automatically launches the Teensy Loader program and programs the device after a successful compile.

With everything working, I started a new Arduino project. Under the ‘Tools’ menu I made the following selections:

  • Board: “Teensy 2.0”
  • USB Type: “MIDI”
  • CPU Speed: “16 MHz”

Now it gets amazing:  Simply add empty setup() and loop() functions as shown below, select compile from the menu, and the attached Teensy becomes a USB MIDI device. Simple as that, a MIDI USB device.

void setup(){
}
void loop(){
}

Of course, the actual source code for this project is somewhat more complicated. The full source code is at the bottom of this post.

The code does a few things:

  • Configures an input pin for the switch, and an output pin for LED
  • Calls the usbMIDI.read() function every few milliseconds, which returns TRUE when a new MIDI message is received from the computer
  • Tests the message to see if it is the tempo ‘pulse’  SYSEX message from an Axe-FX
  • Flashes the LED when a matching message is received
  • Debounces the switch input
  • Detects a switch-down event
  • On switch down, sends MIDI CC message for Axe-FX tap tempo (channel 1, cc# 14, value 0) to the computer

Here’s the source code.

/***************************************
Teensy Tap Tempo for Axe-FX
By D. Sorlien, Sept. 2011
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
http://creativecommons.org/licenses/by/3.0/
***************************************/
byte *  pSysExArray;
int ledPin = 11;
int switchPin = 0;
int ledTimer = 0;
int switchTimer = 0;
boolean lastswitch = 1;
boolean newswitch = 1;
boolean switchState = 1;

void setup() {
  pinMode(ledPin, OUTPUT);     // blinky lights
  pinMode(switchPin, INPUT_PULLUP);   // use the foot, Luke
  digitalWrite(ledPin, LOW);
}

void loop() {
  delay(5);
  // Detect & process incoming sysex
  if (usbMIDI.read()) {
    if (usbMIDI.getData1() == 7) {
      pSysExArray = usbMIDI.getSysExArray();      // get pointer to data
      if(  (pSysExArray[0] == 240) &&             // correct message?
           (pSysExArray[1] == 0) &&
           (pSysExArray[2] == 1) &&
           (pSysExArray[3] == 116) &&
           (pSysExArray[5] == 16) &&
           (pSysExArray[6] == 247)  )
      {
        digitalWrite(ledPin, HIGH);    // LED on
        ledTimer = 10;                 // for approx 10 x 5ms
      }
    }
  } //  if (usbMIDI.read())

  // Handle switch, transmit Tap Tempo CC message on switchdown
  newswitch = digitalRead(switchPin); // read pin state
  if(lastswitch != newswitch) switchTimer = 10; // restart 50ms debounce on pin change
  if(switchTimer == 0) {
    if(switchState && !newswitch) { // detect switch down event
      usbMIDI.sendControlChange(14, 0, 1); // Send tap tempo CC message
    }
    switchState = newswitch;   
  }
  if(switchTimer > 0) switchTimer--;
  lastswitch = newswitch;

  // Handle LED
  if(ledTimer > 0) ledTimer--;
  if(ledTimer == 0) digitalWrite(ledPin, LOW); // LED off
}
//***************************************
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: