Teensy 2.0 with DIN

Status
Not open for further replies.

NibblesIsGod

New member
Hey y'all.

So to start, I really have no idea what I'm doing, I'm not a programmer by any means and I figured out what I have so far by a very long game of trial and error. I started with a code from a website about making your own MIDI controller, but that code sent the same CC message from every pot so I just messed around til I made it work. That being said, somehow I managed to make this do exactly what I want over usbMIDI, but I can't figure out how to make the teensy 2.0 send MIDI data from the TX pin to my 5 pin DIN.

Do I need to actually add code to have the teensy send MIDI from the TX pin or should it automatically send? Also, this page from the PJRC site is a bit confusing:

https://www.pjrc.com/teensy/td_libs_MIDI.html

The schematic and the example photo show pins 4 and 5 on the DIN in opposite positions.

Anyhow, here's my code. As I said before, I have no idea what the hell I am doing as far as writing code goes. Honestly, I'm pretty amazed I was able to get it actually working and I'm sure there are better ways to do this and that this code is way wrong, but if anyone could help me figure out how to make it transmit midi data to the TX pin I would be really grateful.

#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 12
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;

// cc values for buttons
int cc_off = 0;
int cc_on = 65;
int cc_super = 127;


Bounce button0 = Bounce(0, 3);
Bounce button1 = Bounce(1, 3);
Bounce button2 = Bounce(2, 3);
Bounce button3 = Bounce(3, 3);


void setup() {
// MIDI rate
Serial.begin(31250);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}

void loop() {
// loop trough active inputs for knobs
{
// read current value at i-th input
inputAnalog[0] = analogRead(0);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[0] - iAlag[0]) > 7){
// calc the CC value based on the raw value
ccValue[0] = inputAnalog[0]/8;
// send the MIDI
usbMIDI.sendControlChange(21, ccValue[0], 1);
// set raw reading to lagged array for next comparison
iAlag[0] = inputAnalog[0];
}

inputAnalog[1] = analogRead(1);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[1] - iAlag[1]) > 7){
// calc the CC value based on the raw value
ccValue[1] = inputAnalog[1]/8;
// send the MIDI
usbMIDI.sendControlChange(22, ccValue[1], 1);
// set raw reading to lagged array for next comparison
iAlag[1] = inputAnalog[1];
}

inputAnalog[2] = analogRead(2);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[2] - iAlag[2]) > 7){
// calc the CC value based on the raw value
ccValue[2] = inputAnalog[2]/8;
// send the MIDI
usbMIDI.sendControlChange(25, ccValue[2], 1);
// set raw reading to lagged array for next comparison
iAlag[2] = inputAnalog[2];
}

inputAnalog[3] = analogRead(3);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[3] - iAlag[3]) > 7){
// calc the CC value based on the raw value
ccValue[3] = inputAnalog[3]/8;
// send the MIDI
usbMIDI.sendControlChange(26, ccValue[3], 1);
// set raw reading to lagged array for next comparison
iAlag[3] = inputAnalog[3];
}

inputAnalog[4] = analogRead(4);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[4] - iAlag[4]) > 7){
// calc the CC value based on the raw value
ccValue[4] = inputAnalog[4]/8;
// send the MIDI
usbMIDI.sendControlChange(27, ccValue[4], 1);
// set raw reading to lagged array for next comparison
iAlag[4] = inputAnalog[4];

}
inputAnalog[5] = analogRead(5);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[5] - iAlag[5]) > 7){
// calc the CC value based on the raw value
ccValue[5] = inputAnalog[5]/8;
// send the MIDI
usbMIDI.sendControlChange(28, ccValue[5], 1);
// set raw reading to lagged array for next comparison
iAlag[5] = inputAnalog[5];

}
inputAnalog[6] = analogRead(6);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[6] - iAlag[6]) > 7){
// calc the CC value based on the raw value
ccValue[6] = inputAnalog[6]/8;
// send the MIDI
usbMIDI.sendControlChange(15, ccValue[6], 1);
// set raw reading to lagged array for next comparison
iAlag[6] = inputAnalog[6];

}
inputAnalog[7] = analogRead(7);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[7] - iAlag[7]) > 7){
// calc the CC value based on the raw value
ccValue[7] = inputAnalog[7]/8;
// send the MIDI
usbMIDI.sendControlChange(34, ccValue[7], 1);
// set raw reading to lagged array for next comparison
iAlag[7] = inputAnalog[7];

}
inputAnalog[8] = analogRead(8);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[8] - iAlag[8]) > 7){
// calc the CC value based on the raw value
ccValue[8] = inputAnalog[8]/8;
// send the MIDI
usbMIDI.sendControlChange(30, ccValue[8], 1);
// set raw reading to lagged array for next comparison
iAlag[8] = inputAnalog[8];

}
inputAnalog[9] = analogRead(9);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[9] - iAlag[9]) > 7){
// calc the CC value based on the raw value
ccValue[9] = inputAnalog[9]/8;
// send the MIDI
usbMIDI.sendControlChange(31, ccValue[9], 1);
// set raw reading to lagged array for next comparison
iAlag[9] = inputAnalog[9];

}
inputAnalog[10] = analogRead(10);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[10] - iAlag[10]) > 7){
// calc the CC value based on the raw value
ccValue[10] = inputAnalog[10]/8;
// send the MIDI
usbMIDI.sendControlChange(32, ccValue[10], 1);
// set raw reading to lagged array for next comparison
iAlag[10] = inputAnalog[10];

}
inputAnalog[11] = analogRead(11);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[11] - iAlag[11]) > 7){
// calc the CC value based on the raw value
ccValue[11] = inputAnalog[11]/8;
// send the MIDI
usbMIDI.sendControlChange(33, ccValue[11], 1);
// set raw reading to lagged array for next comparison
iAlag[11] = inputAnalog[11];
}
delay(5); // limits MIDI messages to reasonable number
}

// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();


if (button0.fallingEdge())
{
usbMIDI.sendProgramChange(95, 1);
}
if (button1.fallingEdge())
{
usbMIDI.sendProgramChange(96, 1);
}
if (button2.fallingEdge())
{
usbMIDI.sendProgramChange(97, 1);
}
if (button3.fallingEdge())
{
usbMIDI.sendProgramChange(98, 1);
}
}
 
If anyone else later finds this, usbMIDI sends to the USB port. MIDI (via MIDI.h) sends to the hardware serial port.
 
Thank you for your reply with this! I have one more question, so I got the code all set and working, but I am coming across an issue where the potentiometers seem to over riding the presets on the synth regardless of whether I turn them or not. I hooked the controller up to a midi monitor and it doesn't seem to send any data until a knob is moved, so this may be an issue with the synth itself, but I am wondering if anyone else has had this issue and may have a fix?

Thanks!

Here's the code:

#include <MIDI.h>

#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 12
// make arrays for input values and lagged input values
int inputAnalog[analogInputs];
int iAlag[analogInputs];
// make array of cc values
int ccValue[analogInputs];
// index variable for loop
int i;

// cc values for buttons
int cc_off = 0;
int cc_on = 65;
int cc_super = 127;


Bounce button0 = Bounce(0, 3);
Bounce button1 = Bounce(1, 3);
Bounce button2 = Bounce(2, 3);
Bounce button3 = Bounce(3, 3);

// Created and binds the MIDI interface to the default hardware Serial port
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

void setup() {
// MIDI rate
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial1.begin(31250);
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
}

void loop() {
// loop trough active inputs for knobs
{
// read current value at i-th input
inputAnalog[0] = analogRead(0);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[0] - iAlag[0]) > 7){
// calc the CC value based on the raw value
ccValue[0] = inputAnalog[0]/8;
// send the MIDI
usbMIDI.sendControlChange(21, ccValue[0], 1);
MIDI.sendControlChange(21, ccValue[0], 1);
// set raw reading to lagged array for next comparison
iAlag[0] = inputAnalog[0];
}

inputAnalog[1] = analogRead(1);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[1] - iAlag[1]) > 7){
// calc the CC value based on the raw value
ccValue[1] = inputAnalog[1]/8;
// send the MIDI
usbMIDI.sendControlChange(22, ccValue[1], 1);
MIDI.sendControlChange(22, ccValue[1], 1);
// set raw reading to lagged array for next comparison
iAlag[1] = inputAnalog[1];
}

inputAnalog[2] = analogRead(2);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[2] - iAlag[2]) > 7){
// calc the CC value based on the raw value
ccValue[2] = inputAnalog[2]/8;
// send the MIDI
usbMIDI.sendControlChange(25, ccValue[2], 1);
MIDI.sendControlChange(25, ccValue[2], 1);
// set raw reading to lagged array for next comparison
iAlag[2] = inputAnalog[2];
}

inputAnalog[3] = analogRead(3);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[3] - iAlag[3]) > 7){
// calc the CC value based on the raw value
ccValue[3] = inputAnalog[3]/8;
// send the MIDI
usbMIDI.sendControlChange(26, ccValue[3], 1);
MIDI.sendControlChange(26, ccValue[3], 1);
// set raw reading to lagged array for next comparison
iAlag[3] = inputAnalog[3];
}

inputAnalog[4] = analogRead(4);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[4] - iAlag[4]) > 7){
// calc the CC value based on the raw value
ccValue[4] = inputAnalog[4]/8;
// send the MIDI
usbMIDI.sendControlChange(27, ccValue[4], 1);
MIDI.sendControlChange(27, ccValue[4], 1);
// set raw reading to lagged array for next comparison
iAlag[4] = inputAnalog[4];

}
inputAnalog[5] = analogRead(5);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[5] - iAlag[5]) > 7){
// calc the CC value based on the raw value
ccValue[5] = inputAnalog[5]/8;
// send the MIDI
usbMIDI.sendControlChange(28, ccValue[5], 1);
MIDI.sendControlChange(28, ccValue[5], 1);
// set raw reading to lagged array for next comparison
iAlag[5] = inputAnalog[5];

}
inputAnalog[6] = analogRead(6);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[6] - iAlag[6]) > 7){
// calc the CC value based on the raw value
ccValue[6] = inputAnalog[6]/8;
// send the MIDI
usbMIDI.sendControlChange(18, ccValue[6], 1);
MIDI.sendControlChange(18, ccValue[6], 1);
// set raw reading to lagged array for next comparison
iAlag[6] = inputAnalog[6];

}
inputAnalog[7] = analogRead(7);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[7] - iAlag[7]) > 7){
// calc the CC value based on the raw value
ccValue[7] = inputAnalog[7]/8;
// send the MIDI
usbMIDI.sendControlChange(30, ccValue[7], 1);
MIDI.sendControlChange(30, ccValue[7], 1);
// set raw reading to lagged array for next comparison
iAlag[7] = inputAnalog[7];

}
inputAnalog[8] = analogRead(8);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[8] - iAlag[8]) > 7){
// calc the CC value based on the raw value
ccValue[8] = inputAnalog[8]/8;
// send the MIDI
usbMIDI.sendControlChange(32, ccValue[8], 1);
MIDI.sendControlChange(32, ccValue[8], 1);
// set raw reading to lagged array for next comparison
iAlag[8] = inputAnalog[8];

}
inputAnalog[9] = analogRead(9);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[9] - iAlag[9]) > 7){
// calc the CC value based on the raw value
ccValue[9] = inputAnalog[9]/8;
// send the MIDI
usbMIDI.sendControlChange(33, ccValue[9], 1);
MIDI.sendControlChange(33, ccValue[9], 1);
// set raw reading to lagged array for next comparison
iAlag[9] = inputAnalog[9];

}
inputAnalog[10] = analogRead(10);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[10] - iAlag[10]) > 7){
// calc the CC value based on the raw value
ccValue[10] = inputAnalog[10]/8;
// send the MIDI
usbMIDI.sendControlChange(34, ccValue[10], 1);
MIDI.sendControlChange(34, ccValue[10], 1);
// set raw reading to lagged array for next comparison
iAlag[10] = inputAnalog[10];

}
inputAnalog[11] = analogRead(11);
// if magnitude of difference is 8 or more...
if (abs(inputAnalog[11] - iAlag[11]) > 7){
// calc the CC value based on the raw value
ccValue[11] = inputAnalog[11]/8;
// send the MIDI
usbMIDI.sendControlChange(35, ccValue[11], 1);
MIDI.sendControlChange(35, ccValue[11], 1);
// set raw reading to lagged array for next comparison
iAlag[11] = inputAnalog[11];
}
delay(5); // limits MIDI messages to reasonable number
}

// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();


if (button0.fallingEdge())
{
usbMIDI.sendProgramChange(95, 1);
MIDI.sendProgramChange(95, 1);
}
if (button1.fallingEdge())
{
usbMIDI.sendProgramChange(96, 1);
MIDI.sendProgramChange(96, 1);
}
if (button2.fallingEdge())
{
usbMIDI.sendProgramChange(97, 1);
MIDI.sendProgramChange(97, 1);
}
if (button3.fallingEdge())
{
usbMIDI.sendProgramChange(98, 1);
MIDI.sendProgramChange(98, 1);
}
}
 
This looks like mutated code from something I posted years ago (in my first months of programming MIDI on Teensy).

As is this code is a bit 'non-standard' but given you say it works then the most likely explanation is there is sufficient noise on your voltage sources that occasional recalculations are forced when the last reading deviates from the reference reading by 8 or more in the 10-bit integer value. So it can suddenly get an uninitiated update signal if the reference and current readings differ sufficiently.

If you replace "> 7" with a higher value it might sort it out.

A better solution is to use a more recent usbMIDI example file and make the few required changes to have it work as MIDI instead.

I could help you adjust my Many Buttons & Knobs code which uses the ResponsiveAnalogRead() library to get a very nicely tamed but still responsive data stream even in its stock settings*.

It's likely replacing the usbMIDI.send commands with MIDI.send and adding the MIDI.h file is about all that's needed other than the usual configuration of pins and note/CC values.



*There's a 'stickiness' setting that should sort this out with the custom configuration settings if the stock ones don't eliminate it.
 
It very well may have been your code I adapted, if so thank you! I have never written code at all so I was just trial and erroring the whole thing.

I'll give raising the ">7"a try and let you know what happens. If it doesn't work I would totally appreciate the help adapting your code. I appreciate the help!
 
Status
Not open for further replies.
Back
Top