Teensy 2.0 Code MIDI Box Help!!

Status
Not open for further replies.
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.

Analogs are working i changed the code like you said i have connected 3 of them(2 Linear potentiometers and 1 rotary Potentiometer) so they worked fine just need to solder them better. But the buttons now don't i left 1 button in the code but it wasn't working.
The code i only need help is for the buttons now.

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 = 3; // number of Analog PINS
const int D_PINS = 1; // 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};
const int CCID[A_PINS] = {21,22,23};

// define the pins and notes for digital events
const int DIGITAL_PINS[D_PINS] = {0};
const int note[D_PINS] = {60};
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);  
    }
  }
}


And one more silly question what should the buttons be connected with the (Signal(pin that goes on Teensy) and Power(VCC)) or (Signal(pin that goes on Teensy) and GrounD(GND))??
 
Last edited:
My bad. For incrementally adding analog, you can just change the value here to an explicit:

for (int i=0;i<A_PINS;i++){

Just replace A_PINS with 1 to 8.
 
My bad. For incrementally adding analog, you can just change the value here to an explicit:

for (int i=0;i<A_PINS;i++){

Just replace A_PINS with 1 to 8.



Amm all the Analogs are working fine (all 4 rotary potentiometers and 4 slide potentiometers)

I only need the buttons to function properly now. So i don't really understand with this code why should i change A_PINS
 
You dont.... button part is in previous post.

Connect digital pins to ground via momentary switches..
 
You dont.... button part is in previous post.

Connect digital pins to ground via momentary switches..

SO the code is OK i just need to connect the buttons with ground, power and signal. For now i have like this on Buttons Red=power and blue= Signal. So i need to attach the ground cable to the buttons?

skatla 1.jpgskatla 2.jpg
 
Switches do not have power connected.


They are connected to ground on one side and the data pin on the other.

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

If your red wire is ground and the blue wires go to pins 0 to 3 then that should work.


In my case from that picture the RED wire is power(VCC) and BLUE wires for pins 0-3. BLACK wire is for ground(GND).

So then in my case i need to switch the RED(VCC) and BLACK(GND) wire so that in the end it should be BLUE for pins 0-3 on one side and BLACK(GND) on the other side for the switches(buttons) .???


But what if i only add a 10k ohm resistor to the RED(VCC) wire should that fixed it??
 
My final project is done and it is functioning properly. I just want to say thank you to everybody that helped me and especially thank you oddson for you help and time for helping me to complete this project in the right time. :) :D
 
Glad you got it all working in the end.

Any advise to share, for beginners seeking to build a similar MIDI controller who might later find this thread by search?
 
Status
Not open for further replies.
Back
Top