Forum Rule: Always post complete source code & details to reproduce any issue!

# Thread: Teensy 2.0 Code MIDI Box Help!!

1. ## Teensy 2.0 Code MIDI Box Help!!

Hi everyone and I'm really sorry in advance for my English,Here is a link that is based on my final assigement that im working on for school and i really need you're help: https://djtechtools.com/2015/08/25/how- ... ontroller/

I started this project nearly 2 weeks ago, but i have a really important question. I did everything exactly like this guy i followed the video. When i upload the code that he has there on the website,to Teensy 2.0 it does it without any errors, but when i go to Ableton it shows that the Teensy MIDI is connected and when i click on that box for maping MIDI my (note/Control) CC is randomaizing numbers from 0-8 like crazy and it doesn't stop, but when he clicks the box for mapping in the video at 13:08 the CC value instantly got 51 and 52 like he declared it in the code.I think that there is problem in my code, and i'm not very good with these coding so I just want to know how to fix that so that i could finish my final assigement. Thank you all for your help in advance.

2. Here is the code that i used with Arduino 1.8.9
Code:
```#include <Bounce.h>

// define how many pots are active up to number of available analog inputs
#define analogInputs 8
// 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 = 51;
int cc1 = 52;
int cc2 = 53;
int cc3 = 54;

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
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
}

// Push Button code
button0.update();
button1.update();
button2.update();
button3.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 (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);
}

}```
I' am runing Windows 10 Home 64xbit
and Ableton live 10

3. Likely a mistake or misunderstanding how to connect the wires, especially for the pots.

Can you show us photos of how you actually connected the wires?

4. I don't think that is the problem with wires because that is happening before i even connect the Teensy. If you watch his video that i put in the link before he did not connected the Teensy with wires his CC values go instantly to 51 and 52. But mine like in this https://gyazo.com/6c137d00fa5dfd924be096b3fb476878
It's going crazy.

5. Almost certainly one or more analog pin read by the code is floating (i.e. not attached to a voltage source or ground).

Floating pins generate noise that generates MIDI garbage.

Do you have eight pots?

This (somewhat naive) code expects pots to be on analog pins A0 to A7 in sequence with as many pots as you indicate in the line
#define analogInputs 8;

Reduce '8' to the actual number of pots you have used.

If you are not required to use that particular code you could try Many_button_knobs example code from the examples that lead with Teensyduino.

6. ...didn't notice your clip....

Looks like A0 to A3 are floating...

This code only works by connecting starting from A0.

7. ...it's possible you have everything correct but the signals on A0 to A3 are noisier that those on A4 to A7.

Increasing the threshold value can get rid of this but too high and it becomes hard to nudge controller values.

If (abs(inputAnalog[i] - iAlag[i]) > 7){...

Increase from seven until you don't see the garbage.

.... or switch to the example code
https://forum.pjrc.com/threads/45376

8. I have 4 potentiometers WL B1k, 4 slide/fader potentiometers and 4 buttons PBS-33B. So in that case i must just raise the number 7 from this code
Code:
`If (abs(inputAnalog[i] - iAlag[i]) > 7)`
until this is working properly and the Ableton recognizes the CC values as they are set in the code. i'm really sorry for taking your time and so grateful for your every advice and help @oddson.

9. Oops misread your reply... deleted

10. Increasing threshold should fix it.

You may still want to switch to the example code.

One thing, the example code sends note events and not CC values with the buttons. This can be changed with two minor tweeks if it's important to use CC for you.

11. Ok i will first try increasing that number. And then for the example code i just open that example and paste my code inside?

12. I'm increasing that number but it goes from 0-8 numbers randomazing, with every increased number in the Ableton goes CC values lower like at
Code:
`if (abs(inputAnalog[i] - iAlag[i]) > 12)`
its from 0-1 randomazing.

And nother question does the Ports in Arduino under Tools effect the code. I have options (Teensy Ports : hid#vid_16c0&pid_0485MIDI(Teensy2.0) and Serial ports: COM1 , Emulated serial)

13. Either you have a bad connection on A0 and A2 or there is something very noisy happening with your build (or both).

The example code will work much better with noise.

The CC numbers tell you which input is the source of the problem. The CC values being sent and whether they are adjacent or varying wildly will tell you if you have a floating pin wit no connection or just noise. You need to know which it is.

Below is the example code configured for the same pins.

As to ports; it is not the source of your problem. If you are getting any MIDI it must have compiled correctly.
Code:
```//************LIBRARIES USED**************
// include the ResponsiveAnalogRead library for analog smoothing
#include <ResponsiveAnalogRead.h>
// include the Bounce library for 'de-bouncing' switches -- removing electrical chatter as contacts settle
#include <Bounce.h>
//usbMIDI.h library is added automatically when code is compiled as a MIDI device

// ******CONSTANT VALUES********
// customize code behaviour here!
const int channel = 1; // MIDI channel
const int A_PINS = 8; // number of Analog PINS
const int D_PINS = 4; // number of Digital PINS
const int ON_VELOCITY = 99; // note-one velocity sent from buttons (should be 65 to  127)

// define the pins you want to use and the CC ID numbers on which to send them..
const int ANALOG_PINS[A_PINS] = {A0,A1,A2,A3,A4,A5,A6,A7};
const int CCID[A_PINS] = {21,22,23,24,25,26,27,28};

// define the pins and notes for digital events
const int DIGITAL_PINS[D_PINS] = {0,1,2,3};
const int note[D_PINS] = {60,61,62,63};
const int BOUNCE_TIME = 7; // 5 ms is usually sufficient
const boolean toggled = true;

//******VARIABLES***********
// a data array and a lagged copy to tell when MIDI changes are required
byte data[A_PINS];
byte dataLag[A_PINS]; // when lag and new are not the same then update MIDI CC value

//************INITIALIZE LIBRARY OBJECTS**************
// not sure if there is a better way... some way run a setup loop on global array??
// use comment tags to comment out unused portions of array definitions

// initialize the ReponsiveAnalogRead objects
ResponsiveAnalogRead analog[]{
{ANALOG_PINS[0],true},
{ANALOG_PINS[1],true},
{ANALOG_PINS[2],true},
{ANALOG_PINS[3],true},
{ANALOG_PINS[4],true},
{ANALOG_PINS[5],true},
{ANALOG_PINS[6],true},
{ANALOG_PINS[7],true},
};

// initialize the bounce objects
Bounce digital[] =   {
Bounce(DIGITAL_PINS[0],BOUNCE_TIME),
Bounce(DIGITAL_PINS[1], BOUNCE_TIME),
Bounce(DIGITAL_PINS[2], BOUNCE_TIME),
Bounce(DIGITAL_PINS[3], BOUNCE_TIME),/*
Bounce(DIGITAL_PINS[4], BOUNCE_TIME),
Bounce(DIGITAL_PINS[5], BOUNCE_TIME),
Bounce(DIGITAL_PINS[6], BOUNCE_TIME),
Bounce(DIGITAL_PINS[7], BOUNCE_TIME),*/
};

//************SETUP**************
void setup() {
// loop to configure input pins and internal pullup resisters for digital section
for (int i=0;i<D_PINS;i++){
pinMode(DIGITAL_PINS[i], INPUT_PULLUP);
}
}

//************LOOP**************
void loop() {
getAnalogData();
getDigitalData();
while (usbMIDI.read()) {
// controllers must call .read() to keep the queue clear even if they are not responding to MIDI
}
}

//************ANALOG SECTION**************
void getAnalogData(){
for (int i=0;i<A_PINS;i++){
// update the ResponsiveAnalogRead object every loop
analog[i].update();
// if the repsonsive value has change, print out 'changed'
if(analog[i].hasChanged()) {
data[i] = analog[i].getValue()>>3;
if (data[i] != dataLag[i]){
dataLag[i] = data[i];
usbMIDI.sendControlChange(CCID[i], data[i], channel);
}
}
}
}

//************DIGITAL SECTION**************
void getDigitalData(){
for (int i=0;i<D_PINS;i++){
digital[i].update();
if (digital[i].fallingEdge()) {
usbMIDI.sendNoteOn(note[i], ON_VELOCITY, channel);
}
// Note Off messages when each button is released
if (digital[i].risingEdge()) {
usbMIDI.sendNoteOff(note[i], 0, channel);
}
}
}```
.... change two send note commands in green to sendControlChange if you want CC messages on the buttons instead of notes.

If this code isn't stable you defiantly have electrical problems with your build.

(I compiled the code with these changes but did not test beyond this; but the changes are minor to it should work.)

14. Here is then my version of the code i reduced digital pins to 4 since i have 4 butttons and at the botom i changed to SendControlChange.

Code:
```//************LIBRARIES USED**************
// include the ResponsiveAnalogRead library for analog smoothing
#include <ResponsiveAnalogRead.h>
// include the Bounce library for 'de-bouncing' switches -- removing electrical chatter as contacts settle
#include <Bounce.h>
//usbMIDI.h library is added automatically when code is compiled as a MIDI device

// ******CONSTANT VALUES********
// customize code behaviour here!
const int channel = 1; // MIDI channel
const int A_PINS = 8; // number of Analog PINS
const int D_PINS = 4; // number of Digital PINS
const int ON_VELOCITY = 99; // note-one velocity sent from buttons (should be 65 to  127)

// define the pins you want to use and the CC ID numbers on which to send them..
const int ANALOG_PINS[A_PINS] = {A0,A1,A2,A3,A4,A5,A6,A7};
const int CCID[A_PINS] = {21,22,23,24,25,26,27,28};

// define the pins and notes for digital events
const int DIGITAL_PINS[D_PINS] = {0,1,2,3};
const int note[D_PINS] = {60,61,62,63};
const int BOUNCE_TIME = 7; // 5 ms is usually sufficient
const boolean toggled = true;

//******VARIABLES***********
// a data array and a lagged copy to tell when MIDI changes are required
byte data[A_PINS];
byte dataLag[A_PINS]; // when lag and new are not the same then update MIDI CC value

//************INITIALIZE LIBRARY OBJECTS**************
// not sure if there is a better way... some way run a setup loop on global array??
// use comment tags to comment out unused portions of array definitions

// initialize the ReponsiveAnalogRead objects
ResponsiveAnalogRead analog[]{
{ANALOG_PINS[0],true},
{ANALOG_PINS[1],true},
{ANALOG_PINS[2],true},
{ANALOG_PINS[3],true},
{ANALOG_PINS[4],true},
{ANALOG_PINS[5],true},
{ANALOG_PINS[6],true},
{ANALOG_PINS[7],true},
};

// initialize the bounce objects
Bounce digital[] =   {
Bounce(DIGITAL_PINS[0],BOUNCE_TIME),
Bounce(DIGITAL_PINS[1], BOUNCE_TIME),
Bounce(DIGITAL_PINS[2], BOUNCE_TIME),
Bounce(DIGITAL_PINS[3], BOUNCE_TIME),
};

//************SETUP**************
void setup() {
// loop to configure input pins and internal pullup resisters for digital section
for (int i=0;i<D_PINS;i++){
pinMode(DIGITAL_PINS[i], INPUT_PULLUP);
}
}

//************LOOP**************
void loop() {
getAnalogData();
getDigitalData();
while (usbMIDI.read()) {
// controllers must call .read() to keep the queue clear even if they are not responding to MIDI
}
}

//************ANALOG SECTION**************
void getAnalogData(){
for (int i=0;i<A_PINS;i++){
// update the ResponsiveAnalogRead object every loop
analog[i].update();
// if the repsonsive value has change, print out 'changed'
if(analog[i].hasChanged()) {
data[i] = analog[i].getValue()>>3;
if (data[i] != dataLag[i]){
dataLag[i] = data[i];
usbMIDI.sendControlChange(CCID[i], data[i], channel);
}
}
}
}

//************DIGITAL SECTION**************
void getDigitalData(){
for (int i=0;i<D_PINS;i++){
digital[i].update();
if (digital[i].fallingEdge()) {
usbMIDI.sendControlChange(note[i], ON_VELOCITY, channel);
}
// Note Off messages when each button is released
if (digital[i].risingEdge()) {
usbMIDI.sendControlChange(note[i], 0, channel);
}
}
}```

I just want to know what are those numbers in red ???

15. ..I just want to know what are those numbers in red ???
Those are what needed to be added to the example code.

You can change the pins you are using or the CC values the code sends in the array definitions at the top of the file.

You should expect that you do have electrical problems... that much noise is unlikely if the wiring is good, unless you are in Chernobyl.

16. Now its jumping from 22-27. At this moment i do not have anything connected to Teensy board but i can show you the picture the way that is going to be connected. And another question do you think that the Teensy might be a problem like some sort of malfunction?? And that i should try another board like Arduino UNO?

17. If the board is not connected than it is going to generate garbage.

Any pin being read must have a stable voltage. The digital pins are pulled up to VCC by the code but not the analog ones so the values being read jump all over the place.

https://forum.arduino.cc/index.php?topic=502228.0

https://electronics.stackexchange.co...-reading-wrong

18. HI @oddson sorry for writing late i was out of town for these couple days. I have conected the MIDI and is still doing the same thing do you think i could try with Arduino UNO because maybe there is something wrong with Teensy? But then again how would the code change for UNO? Thank you in advance

19. I've tried the MIDI-ox to see if it gets the signal and its jumping always on this B0.

20. This data shows you STILL do not have the Teensy connected to a stable voltage on multiple pins.

21. Originally Posted by oddson
This data shows you STILL do not have the Teensy connected to a stable voltage on multiple pins.
OK, then i will solder all the components and try again because i have connected The Teensy with jumper wires on the protobord but only 1 chanel( 1 potentiometer, 1 linear potentiometer and 1 button) to see if it works. I will try all of them then.

22. I know your goal is MIDI, but as an intermediate stepping stone to success, you should really run something much simpler and use the Arduino Serial Monitor.

For example, try running something like this on your Teensy 2.0:

Code:
```void setup() {
}

void loop() {
Serial.print(analogRead(A0));
Serial.print(",");
Serial.print(analogRead(A1));
Serial.print(",");
Serial.print(analogRead(A2));
Serial.println();
delay(100);
}```
Then in Arduino, select the port and open the serial monitor. You should see something like this:

The numbers will scroll at a pace which is easy to see as you adjust the pots. Edit the delay if the scrolling speed is too fast or too slow, and of course change the pin numbers depending on which pins you've actually connected the pots.

The point is to make testing simpler, so you can focus on getting your hardware working WITHOUT the extra complexity and difficulty of also working on MIDI communication and using other software on your PC. The Arduino software was designed to make things easier for you. Please, do yourself a huge favor and use it this simple way.

Later, when you are confident the analog inputs are working correctly and you know the numbers are good in the serial monitor, then try sending MIDI messages. But use the serial monitor and the simplest possible code first to get your hardware tested the easy way.

23. Originally Posted by Mihcc
OK, then i will solder all the components and try again because i have connected The Teensy with jumper wires on the protobord but only 1 chanel( 1 potentiometer, 1 linear potentiometer and 1 button) to see if it works. I will try all of them then.
So you left all the other pins floating and that's why you are getting garbage.

Any PIN active in the code needs to be tied to a stable voltage. You can ground unused ones during testing or disable the unused ones in code but you can't just leave them being read while they are not connected.

You need to understand basics first.

24. const int A_PINS = 1; // number of Analog PINS

Set the number of Analog pins to 1 and connect to A0 to test with one pot. Add each pot to A1, A2 etc. and increment the A_PINS with each added pot.

25. C:\Users\Miha\AppData\Local\Temp\arduino_build_618 189/core\core.a(main.cpp.o): In function `main':

main.cpp.text.startup.main+0x2): undefined reference to `setup'

collect2.exe: error: ld returned 1 exit status

Error compiling for board Teensy 2.0.

what is the problem if it shows Error compiling for board Teensy 2.0

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•