Teensyduino

Teensyduino

So here’s the deal,

I had been running my midi controller on the arduino using code from Arcade Button MIDI Controller : 10 Steps (with Pictures) - Instructables and it was awesome. However, I had the arduino on the side and all ugly looking so i got a teensy++. With this there is more expandability and usb to midi instead of serial, but also a little confusion. I’m not new to arduino like programing but for some reason midi has got me stumped.

My big question is if anyone out there has used the teensyduino for usb midi, i’m not looking for someone to write the code for me just to help explain how to set it all up.

Here is what the makers of teensyduino give to run with:

with no examples.

This is what i have for reading one of the buttons, i still need that channel, but i’m not sure what that is lol.

if (digitalRead(PIN_D3)) {

} else {
usbMIDI.sendNoteOn(60, 127,
}

oh and the buttons are on pull-up resistors (low is pressed, high is released)

Thanks a lot of any help!

The channel is just the midi channel you want to send the message on… Channels 1-16 are available…

well don’t i feel stupid lol.

Thanks, do you know about any of the things that need to be included in the setup, right now i just have defining all the inputs as pull-ups

void setup()
{
pinMode(PIN_D3, INPUT_PULLUP);
pinMode(PIN_D4, INPUT_PULLUP);
pinMode(PIN_D5, INPUT_PULLUP);
pinMode(PIN_D6, INPUT_PULLUP);
pinMode(PIN_D7, INPUT_PULLUP);
pinMode(PIN_E0, INPUT_PULLUP);
pinMode(PIN_E1, INPUT_PULLUP);
pinMode(PIN_C0, INPUT_PULLUP);
pinMode(PIN_C1, INPUT_PULLUP);
pinMode(PIN_C2, INPUT_PULLUP);
pinMode(PIN_C3, INPUT_PULLUP);
pinMode(PIN_C4, INPUT_PULLUP);
}

If your code works on an arduino… it should work perfectly on the teensy… just substitute the old midi code with the new usbmidi code…

honestly, the code i’ve been using confuses the hell out of me.

like i said i got it off instructables, so i’ve never really had to fiddle with it. However i did try just using the teensy as serial with this program and it still wouldn’t work correctly. for example analog wouldn’t work at all and pin 6 wouldn’t work, i think it has something to do with teensys led on that pin.

I wouldn’t mind if i still had to use serial, as long as i can get it to work.

if you have any insight i would greatly appricate it.

The code is a download on the link if you wouldn’t mind looking.

I’ll look in a bit… i’m currently recording (or attempting to record) a 10 min mix… but for now try getting your previous code to work as is on the teensy… assuming that is possible, then it should be a simple matter of swapping out the code that deals with sending midi messages.

thanks a lot.

any ideas why, some of the pins aren’t working right?

and good luck getting that mix done!

Upon first glance, that code is very well written… its certainly a good starting point…

That it is, I’ve been trying to break it up so I can understand it and understand why the teensy is having trouble with it.

what problems are you facing exactly? It may be as simple as changing the pin assignments…

Some of the pins are sending 2 different notes one when I press it down and a different when I release. While pin 6 isn’t working at all, but I think I just need to add an external pull down cause if the led. Also I’m having a hard time getting analog to work. If it helps I have the buttons hooked up to pins 3-14 for the digital inputs.

Without knowing any of the potential differences between an arduino and a teensy, the following should be all you need to make the code work with the usbmidi of the teensy (side note, I am LOVING how he deals with the analog inputs… very clever, I’m certainly going to poke around to fully understand things)…

I cannot see any reason why the code wouldn’t work as is with the teensy… did you make sure you have all the pin assignments correct?

void noteOn(int channel, int pitch, int velocity)
{
// 0x90 is the first of 16 note on channels. Subtract one to go from MIDI's 1-16 channels to 0-15
channel += 0x90 - 1;

// Ensure we're between channels 1 and 16 for a note on message
if (channel >= 0x90 && channel <= 0x9F)
{
#ifdef DEBUG
Serial.print("Button pressed: ");
Serial.println(pitch);
#else
usbMIDI.sendNoteOn(pitch, velocity, channel)
#endif
}
}

// Send a MIDI note off message
void noteOff(int channel, int pitch)
{
// 0x80 is the first of 16 note off channels. Subtract one to go from MIDI's 1-16 channels to 0-15
channel += 0x80 - 1;

// Ensure we're between channels 1 and 16 for a note off message
if (channel >= 0x80 && channel <= 0x8F)
{
#ifdef DEBUG
Serial.print("Button released: ");
Serial.println(pitch);
#else
usbMIDI.sendNoteOff(pitch, 0x00, channel)
#endif
}
}

// Send a MIDI control change message
void controlChange(int channel, int control, int value)
{
// 0xB0 is the first of 16 control change channels. Subtract one to go from MIDI's 1-16 channels to 0-15
channel += 0xB0 - 1;

// Ensure we're between channels 1 and 16 for a CC message
if (channel >= 0xB0 && channel <= 0xBF)
{
#ifdef DEBUG
Serial.print(control - MIDI_CC);
Serial.print(": ");
Serial.println(value);
#else
usbMIDI.sendControlChange(control, value, channel)
#endif
}
}

Important question: Are you using the code in midifighter mode? In other words, what is the current state of the following code

// Uncomment this line to enable outputs corresponding to the MIDI Fighter so MF mappings can be used in Traktor.
//#define MIDI_FIGHTER

Thanks for the code I’ll have to give it a shot in the morning. Where would that go in the existing code?

I believe that all of my pin assignments are correct. I only found 2 spots where I had to change the pin numbers in the code. Is there somthing I should change for the analog?

I left that commeted I’m not using it in midi fighter mode

Ok… so… from what I can see, you need to refer to the teensy pins by their constants (PIN_D0, PIN_C1, etc…) arduino just uses the numbers, 2, 3, 4, 5, etc… so at the top of the code you will have to change the DIGITAL_PIN_ORDER define to the correct values…

Which you can find here: Digital Input/Output on Teensy with Arduino functions, digitalWrite, digitalRead, pinMode

Pay attention to the midi fighter section, if you want to use it, make sure you wire the buttons right, OR specify the buttons in the correct order for the MF part to send the right notes (it should work regardless, it’ll just be sending the wrong notes compared to a MF)

For the internal pullups, make sure you have the INPUT_PULLUP teensy extension is installed (no clue what the heck that is… never used a teensy before)..

As for analogue, I’m not sure… I haven’t read the analogue section of the teensy docs yet…

You’d replace the existing code block with what I posted… or to learn what I did, just compare the differences… All I did was add one line, and remove 3…

Could you post a complete copy of YOUR code (make sure to use the # button to wrap it in [ code ] tags)

From what I can see of the analog part… there doesn’t seem to be any difference between arduino and teensy… so just make sure your pin assignments are correct and it should work… if not, try to explain what exactly is happening with the analog part…

But for now, leave them completely disconnected, focus on getting the buttons to work properly first..

// Number of digital inputs. Can be anywhere from 0 to 18.
#define NUM_DI 12
// Number of analogue inputs. Can be anywhere from 0 to 6.
#define NUM_AI 6

// Number of digital inputs. Can be anywhere from 0 to 68.
//#define NUM_DI 52
// Number of analogue inputs. Can be anywhere from 0 to 16.
//#define NUM_AI 16

#ifdef MIDI_FIGHTER
#define MIDI_CHANNEL 3
// First note, starting from lower left button
#define NOTE NOTE_C2
// When mapping to a MIDI Fighter we need to skip a row of buttons. Set this from 0-3 to define which row to skip.
// Rows are ordered from bottom to top (same as the MIDI Fighter's button layout).
#define SKIP_ROW 2
// This pin order corresponds to the bottom left button being zero, increasing by one as we move from left to right, bottom to top
// 8  9 10 11
// 4  5  6  7
// 0  1  2  3
// This array size must match NUM_DI above.
#define DIGITAL_PIN_ORDER 10, 11, 12, 13, 6, 7, 8, 9, 14, 3, 4, 5
#else
#define MIDI_CHANNEL 1
// First note, starting from upper left button
#define NOTE NOTE_C0
// This pin order corresponds to the top left button being zero, increasing by one as we move from left to right, top to bottom
// 0  1  2  3
// 4  5  6  7
// 8  9  10 11
// This array size must match NUM_DI above.
#define DIGITAL_PIN_ORDER 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
//  #define DIGITAL_PIN_ORDER 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53
#endif

#define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5
//#define ANALOGUE_PIN_ORDER A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15
#define MIDI_CC MIDI_CC_GENERAL1

This is the only section that i changed (it wouldn’t let me post the entire thing)

not sure how to redefine the analog inputs in the code, but according to teensys website you can call the inputs PIN_D3 or pin 3 so theoretically it should work lol Teensyduino - Add-on for Arduino IDE to use Teensy USB development board

however i just noticed that i may have analog wrong does it look like 28-35 is the analog, or 38-45 are the analog

Change: #define DIGITAL_PIN_ORDER 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14

To:

#define DIGITAL_PIN_ORDER PIN_D0
PIN_D1, PIN_D2, PIN_D3, PIN_D4, PIN_D5, PIN_D6, PIN_D7, PIN_E0, PIN_E1, PIN_C0, PIN_C1, PIN_C2, PIN_C3, PIN_C4

Re: analog

Teensy seems to use the same method for pin-naming as the arduino, so you would use the pin numbers you wired them up to as per this pic: