Help with Teensy 3.2 Midi controller code.

Status
Not open for further replies.

stygallj

New member
I am doing a DIY midi controller project using 9 potentiometers and 16 arcade buttons. I have edited an existing script written for a controller with less buttons made for the Teensy 2.0

The circuit uses analog inputs A0-A9
And digital inputs 0-12 & 24-27

Will this code work on the teensy 3.2 ? Or is there anything else i need to change ?

#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 9
// 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;

// map buttons to cc for button
int cc0 = 45;
int cc1 = 46;
int cc2 = 47;
int cc3 = 48;
int cc4 = 49;
int cc5 = 50;
int cc6 = 51;
int cc7 = 52;
int cc8 = 53;
int cc9 = 54;
int cc10 = 55;
int cc11 = 56;
int cc12 = 57;
int cc13 = 58;
int cc14 = 59;
int cc15 = 60;

Bounce

Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);
Bounce button2 = Bounce(2, 5);
Bounce button3 = Bounce(3, 5);
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5);
Bounce button6 = Bounce(6, 5);
Bounce button7 = Bounce(7, 5);
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);
Bounce button12 = Bounce(12, 5);
Bounce button13 = Bounce(13, 5);
Bounce button14 = Bounce(14, 5);
Bounce button15 = Bounce(15, 5);



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(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
}

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

// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button13.update();
button14.update();
button15.update();



if (button0.fallingEdge())
{
usbMIDI.sendControlChange(cc0, cc_on, 3);
}
if (button1.fallingEdge())
{
usbMIDI.sendControlChange(cc1, cc_on, 3);
}
if (button2.fallingEdge())
{
usbMIDI.sendControlChange(cc2, cc_on, 3);
}
if (button3.fallingEdge())
{
usbMIDI.sendControlChange(cc3, cc_on, 3);
}
if (button4.fallingEdge())
{
usbMIDI.sendControlChange(cc4, cc_on, 3);
{
if (button5.fallingEdge())
{
usbMIDI.sendControlChange(cc5, cc_on, 3);
{
if (button6.fallingEdge())
{
usbMIDI.sendControlChange(cc6, cc_on, 3);
{
if (button7.fallingEdge())
{
usbMIDI.sendControlChange(cc7, cc_on, 3);
{
if (button8.fallingEdge())
{
usbMIDI.sendControlChange(cc8, cc_on, 3);
{
if (button9.fallingEdge())
{
usbMIDI.sendControlChange(cc9, cc_on, 3);
{
if (button10.fallingEdge())
{
usbMIDI.sendControlChange(cc10, cc_on, 3);
{
if (button11.fallingEdge())
{
usbMIDI.sendControlChange(cc11, cc_on, 3);
{
if (button12.fallingEdge())
{
usbMIDI.sendControlChange(cc12, cc_on, 3);
{
if (button13.fallingEdge())
{
usbMIDI.sendControlChange(cc13, cc_on, 3);
{
if (button14.fallingEdge())
{
usbMIDI.sendControlChange(cc14, cc_on, 3);
{
if (button15.fallingEdge())
{
usbMIDI.sendControlChange(cc15, cc_on, 3);
{


if (button0.risingEdge())
{
usbMIDI.sendControlChange(cc0, cc_off, 3);
}
if (button1.risingEdge())
{
usbMIDI.sendControlChange(cc1, cc_off, 3);
}
if (button2.risingEdge())
{
usbMIDI.sendControlChange(cc2, cc_off, 3);
}
if (button3.risingEdge())
{
usbMIDI.sendControlChange(cc3, cc_off, 3);
}
if (button4.risingEdge())
{
usbMIDI.sendControlChange(cc4, cc_off, 3);
}
if (button5.risingEdge())
{
usbMIDI.sendControlChange(cc5, cc_off, 3);
}
if (button6.risingEdge())
{
usbMIDI.sendControlChange(cc6, cc_off, 3);
}
if (button7.risingEdge())
{
usbMIDI.sendControlChange(cc7, cc_off, 3);
}
if (button8.risingEdge())
{
usbMIDI.sendControlChange(cc8, cc_off, 3);
}
if (button9.risingEdge())
{
usbMIDI.sendControlChange(cc9, cc_off, 3);
}
if (button10.risingEdge())
{
usbMIDI.sendControlChange(cc10, cc_off, 3);
}
if (button11.risingEdge())
{
usbMIDI.sendControlChange(cc11, cc_off, 3);
}
if (button12.risingEdge())
{
usbMIDI.sendControlChange(cc12, cc_off, 3);
}
if (button13.risingEdge())
{
usbMIDI.sendControlChange(cc13, cc_off, 3);
}
if (button14.risingEdge())
{
usbMIDI.sendControlChange(cc14, cc_off, 3);
}
if (button15.risingEdge())
{
usbMIDI.sendControlChange(cc15, cc_off, 3);
}
}







Any help is greatly appreciated.

thanks

James
 
Your code isn't going to compile. If you click on Tools|Auto Format in the IDE you'll see that the indenting is way off. This is because you've got a whole load of braces which don't match. They first go awry here:
Code:
if (button4.fallingEdge())
{
usbMIDI.sendControlChange(cc4, cc_on, 3);
{
The second open brace should be a close brace. All the other if statements down to, and including, "if (button15.fallingEdge())" have the same problem.

Near the beginning of the code you have the word "Bounce" on a line of its own. Delete it.

The code should work on a T3.2 but it would be a lot briefer and easier to read if you used arrays and for loops.

Pete
 
I've just noticed a wee problem. You said you are using digital pins 0-12 but the code uses 0-15. Pin 13 is the LED. Digital pins 14 and 15 are also Analog pins A0 and A1. You'll have to decide which they are going to be.
Using digital pins 24-27 is OK but I presume that you're aware that they are on the pads on the bottom of the board and aren't as easy to get at as the the pins on the edge.

Pete
 
Changes made

I've just noticed a wee problem. You said you are using digital pins 0-12 but the code uses 0-15. Pin 13 is the LED. Digital pins 14 and 15 are also Analog pins A0 and A1. You'll have to decide which they are going to be.
Using digital pins 24-27 is OK but I presume that you're aware that they are on the pads on the bottom of the board and aren't as easy to get at as the the pins on the edge.

Pete



Hi Pete

Thank you ever so much for your help. I think i have made all the changes you have pointed out. Here is the updated script.




#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 9
// 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;

// map buttons to cc for button
int cc0 = 45;
int cc1 = 46;
int cc2 = 47;
int cc3 = 48;
int cc4 = 49;
int cc5 = 50;
int cc6 = 51;
int cc7 = 52;
int cc8 = 53;
int cc9 = 54;
int cc10 = 55;
int cc11 = 56;
int cc12 = 57;
int cc13 = 58;
int cc14 = 59;
int cc15 = 60;


Bounce button0 = Bounce(0, 5);
Bounce button1 = Bounce(1, 5);
Bounce button2 = Bounce(2, 5);
Bounce button3 = Bounce(3, 5);
Bounce button4 = Bounce(4, 5);
Bounce button5 = Bounce(5, 5);
Bounce button6 = Bounce(6, 5);
Bounce button7 = Bounce(7, 5);
Bounce button8 = Bounce(8, 5);
Bounce button9 = Bounce(9, 5);
Bounce button10 = Bounce(10, 5);
Bounce button11 = Bounce(11, 5);
Bounce button12 = Bounce(12, 5);
Bounce button13 = Bounce(24, 5);
Bounce button14 = Bounce(25, 5);
Bounce button15 = Bounce(26, 5);



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(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pinMode(16, INPUT_PULLUP);
pinMode(17, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(20, INPUT_PULLUP);
pinMode(21, INPUT_PULLUP);
pinMode(22, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
pinMode(24, INPUT_PULLUP);
pinMode(25, INPUT_PULLUP);
pinMode(26, INPUT_PULLUP);
pinMode(27, INPUT_PULLUP);
}

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

// Push Button code
button0.update();
button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();
button7.update();
button8.update();
button9.update();
button10.update();
button11.update();
button12.update();
button13.update();
button14.update();
button15.update();



if (button0.fallingEdge())
{
usbMIDI.sendControlChange(cc0, cc_on, 3);
}
if (button1.fallingEdge())
{
usbMIDI.sendControlChange(cc1, cc_on, 3);
}
if (button2.fallingEdge())
{
usbMIDI.sendControlChange(cc2, cc_on, 3);
}
if (button3.fallingEdge())
{
usbMIDI.sendControlChange(cc3, cc_on, 3);
}
if (button4.fallingEdge())
{
usbMIDI.sendControlChange(cc4, cc_on, 3);
}
if (button5.fallingEdge())
{
usbMIDI.sendControlChange(cc5, cc_on, 3);
}
if (button6.fallingEdge())
{
usbMIDI.sendControlChange(cc6, cc_on, 3);
}
if (button7.fallingEdge())
{
usbMIDI.sendControlChange(cc7, cc_on, 3);
}
if (button8.fallingEdge())
{
usbMIDI.sendControlChange(cc8, cc_on, 3);
}
if (button9.fallingEdge())
{
usbMIDI.sendControlChange(cc9, cc_on, 3);
}
if (button10.fallingEdge())
{
usbMIDI.sendControlChange(cc10, cc_on, 3);
}
if (button11.fallingEdge())
{
usbMIDI.sendControlChange(cc11, cc_on, 3);
}
if (button12.fallingEdge())
{
usbMIDI.sendControlChange(cc12, cc_on, 3);
}
if (button13.fallingEdge())
{
usbMIDI.sendControlChange(cc13, cc_on, 3);
}
if (button14.fallingEdge())
{
usbMIDI.sendControlChange(cc14, cc_on, 3);
}
if (button15.fallingEdge())
{
usbMIDI.sendControlChange(cc15, cc_on, 3);
}


if (button0.risingEdge())
{
usbMIDI.sendControlChange(cc0, cc_off, 3);
}
if (button1.risingEdge())
{
usbMIDI.sendControlChange(cc1, cc_off, 3);
}
if (button2.risingEdge())
{
usbMIDI.sendControlChange(cc2, cc_off, 3);
}
if (button3.risingEdge())
{
usbMIDI.sendControlChange(cc3, cc_off, 3);
}
if (button4.risingEdge())
{
usbMIDI.sendControlChange(cc4, cc_off, 3);
}
if (button5.risingEdge())
{
usbMIDI.sendControlChange(cc5, cc_off, 3);
}
if (button6.risingEdge())
{
usbMIDI.sendControlChange(cc6, cc_off, 3);
}
if (button7.risingEdge())
{
usbMIDI.sendControlChange(cc7, cc_off, 3);
}
if (button8.risingEdge())
{
usbMIDI.sendControlChange(cc8, cc_off, 3);
}
if (button9.risingEdge())
{
usbMIDI.sendControlChange(cc9, cc_off, 3);
}
if (button10.risingEdge())
{
usbMIDI.sendControlChange(cc10, cc_off, 3);
}
if (button11.risingEdge())
{
usbMIDI.sendControlChange(cc11, cc_off, 3);
}
if (button12.risingEdge())
{
usbMIDI.sendControlChange(cc12, cc_off, 3);
}
if (button13.risingEdge())
{
usbMIDI.sendControlChange(cc13, cc_off, 3);
}
if (button14.risingEdge())
{
usbMIDI.sendControlChange(cc14, cc_off, 3);
}
if (button15.risingEdge())
{
usbMIDI.sendControlChange(cc15, cc_off, 3);
}
}

It seems to compile fine. Have i done everything you mentioned correctly ?

Once again, thank you ever so much for all your help.

James
 
When you set the pinMode, you are missing pin 8. You also should only set pinMode for the digital pins. You don't have to do anything to use a pin as analog.

Pete
 
The analog section of this code looks like a variant of my very early MIDI attempts that ended up in a tutorial from djtechtools.com.

Every so often it shows up here with people trying to make it work with minor changes and I cringe when I see it.

The mix of array programming vs. the each-pin-on-its-own approach used in the digital section doesn't make much sense... the digital section could be very tight if arrays are used there too (my current example code uses 7 lines).

(And the usual 'benefit' of avoiding arrays that it's easier for noobs to understand is lost because they are used in the analog section.)

Here is a better example for noobs looking to make a multi-button-knob controllers: https://forum.pjrc.com/threads/45376-Example-code-for-MIDI-controllers-with-Pots-and-Buttons
(The code is also in the MIDI examples in the Teensyduino distribution)

It uses the library ResponsiveAnalogRead() instead of handling smoothing directly with the deadband change>threshold approach used here (if (abs(inputAnalog - iAlag) > 7)...).

If anyone is keen to use deadband in the analog section I have that version somewhere too but I haven't been able to locate it recently (it may be posted here somewhere) but I can recreate it if anyone is interested.
 
The analog section of this code looks like a variant of my very early MIDI attempts that ended up in a tutorial from djtechtools.com.

Every so often it shows up here with people trying to make it work with minor changes and I cringe when I see it.

The mix of array programming vs. the each-pin-on-its-own approach used in the digital section doesn't make much sense... the digital section could be very tight if arrays are used there too (my current example code uses 7 lines).

(And the usual 'benefit' of avoiding arrays that it's easier for noobs to understand is lost because they are used in the analog section.)

Here is a better example for noobs looking to make a multi-button-knob controllers: https://forum.pjrc.com/threads/45376-Example-code-for-MIDI-controllers-with-Pots-and-Buttons
(The code is also in the MIDI examples in the Teensyduino distribution)

It uses the library ResponsiveAnalogRead() instead of handling smoothing directly with the deadband change>threshold approach used here (if (abs(inputAnalog - iAlag) > 7)...).

If anyone is keen to use deadband in the analog section I have that version somewhere too but I haven't been able to locate it recently (it may be posted here somewhere) but I can recreate it if anyone is interested.



I am using the code found in the midi examples on the teensy distribution. I have made changes so that i can implement and test each analog input as i go. Unfortunately the midi info going into ableton jumps around a lot. I am able to get a signal in and map a linear potentiometer to a slider in ableton but when i move it the value jumps back around before setting on the desired value. Even when its not being used it jumps around a lot.

Any ideas how i might solve this ? It's all well and good being able to get the mdii information into the daw but in this state it is pretty unsable.

Also i dont think its a problem with the grounding as i am using a single connection when testing it.

thanks

james
 
I am using the code found in the midi examples on the teensy distribution.
This?
Code:
void loop() {
 // loop trough active inputs for knobs
 for (i = 0; i < analogInputs; i++) {
 // read current value at i-th input
 inputAnalog[i] = analogRead(i);
 // if magnitude of difference is 8 or more...
 if (abs(inputAnalog[i] - iAlag[i]) > 7) {
 // calc the CC value based on the raw value
 ccValue[i] = inputAnalog[i] / 8;
 // send the MIDI
 usbMIDI.sendControlChange(i, ccValue[i], 3);
 // set raw reading to lagged array for next comparison
 iAlag[i] = inputAnalog[i];
 }
 delay(5); // limits MIDI messages to reasonable number
 }
I don't think this code is in the examples (I hope not!).

The code from the link above I gave above is in examples too and it may solve some or all of your stability issues.

I have made changes so that i can implement and test each analog input as i go. Unfortunately the midi info going into ableton jumps around a lot. I am able to get a signal in and map a linear potentiometer to a slider in ableton but when i move it the value jumps back around before setting on the desired value. Even when its not being used it jumps around a lot...

Also i dont think its a problem with the grounding as i am using a single connection when testing it.

Is there any chance you have an analog pin being read that is not connected properly to it's potentiometer?

If you have pins being read without a steady voltage at the pin or you will get crazy readings.
 
Status
Not open for further replies.
Back
Top