Example code for MIDI controllers with Pots and Buttons

14 and 15 are being used as analog pins (A0, A1) to control two linear potentiometers, do i still need to make the changes to the
const int DIGITAL_PINS[D_PINS]
section if 14 & 15 are used as analog?

I am using 0-n pins in order, but would dropping the DIGITAL_PINS array work given that 14 and 15 are being used as analog pins?
 
Then this should work...
Code:
// 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),
  Bounce(DIGITAL_PINS[8], BOUNCE_TIME), 
  Bounce(DIGITAL_PINS[9], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[10], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[11], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[12], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[13], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[14], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[15], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[16], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[17], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[18], BOUNCE_TIME),
  Bounce(DIGITAL_PINS[19], BOUNCE_TIME)
};

EDIT - watch for extra comma after last item... that might be the issue why it didn't compile without additional items.
DIGITAL_PINS[20] and 21 shouldn't be needed. That's were my latest confusion comes in. There are only 20 members in that array and so you shouldn't need 22 bounce objects and the last two might be a problem if they compile with an unpredictable value for the pin number (I don't know).

You could try the loop version:
Code:
  for (int i = 0; i < D_PINS ; i++) {
      digital[i] = new Bounce(DIGITAL_PINS[i],  BOUNCE_TIME); // initialize
  }


But if the version you have is working it's fine to leave the extra bounce objects but I'm concerned that it not compiling without them implies some other error.
 
Last edited:
The extra comma seems like it could be the issue.

I never tried it with only 20 and not 21, or without having the last comma after 19. Ill try to test both later today and see what i can find out. But so far i'm not having issues with 20 and 21 being included.

I'll also try to play with the loop version and see if it works.

Also I've really interested in diving deep into coding for teensys, is there a good starting point you can recommend? Should i just find intro tutorials for coding for Arduino and c++? maybe there is a book you can recommend? I have a very limited background in coding (mostly basic C++ in school a few years ago). I just hate feeling so in the dark that i would never think to
 
Hello Oddson,I made your midi controller and have an issue I can't resolve. The pots all work fine switch 1 and 2 work well but switches 3 - 6 act like there fighting for the same pin out. According to my midi program I'm seeing different note numbers but they all try to control the same item.I had soldered the headers to teensy so I checked for continuity on the pins and there fine. I tried putting the pin outs to 24 -29 instead of 0-5 and that didn't work at all. Could you please look at the code to make sure I didn't leave something out or maybe I added something wrong.
Also the code compiles

Thanks

Code:
/* Use arrays to manage lists of knobs/pots and pushbuttons.



   By Leif Oddson

   https://forum.pjrc.com/threads/45376



   This more complex example demonstrates how to use arrays to

   manage a larger number of inputs, without duplicating your

   code for every signal.



   You must select MIDI from the "Tools > USB Type" menu



   This example code is in the public domain.

*/





//************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 = 5; // number of Analog PINS

const int D_PINS = 6; // 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,};

const int CCID[A_PINS] = {21,22,23,24,25,};



// define the pins and notes for digital events

const int DIGITAL_PINS[D_PINS] = {0,1,2,3,4,5};

const int note[D_PINS] = {60,61,62,63,64,65};

const int BOUNCE_TIME = 5; // 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);

      }

    }

  }

}

 





const int D_LEDS = 6; // How many Pins for LEDS

const int DIGITAL_LEDS[D_LEDS] = {7,8,9,10,11,12}; // Numbers of the LED Pins





//************DIGITAL SECTION**************

void getDigitalData(){

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

  digital[i].update();

    if (digital[i].risingEdge()) {

      usbMIDI.sendNoteOn(note[i], ON_VELOCITY, channel);

      digitalWrite(DIGITAL_LEDS[0], LOW); // TURNS OFF DIGITAL_LEDS 1 - PIN 7

      digitalWrite(DIGITAL_LEDS[1], LOW); // TURNS OFF DIGITAL_LEDS 2 - PIN 8

      digitalWrite(DIGITAL_LEDS[2], LOW); // TURNS OFF DIGITAL_LEDS 3 - PIN 9

      digitalWrite(DIGITAL_LEDS[3], LOW); // TURNS OFF DIGITAL_LEDS 4 - PIN 10

      digitalWrite(DIGITAL_LEDS[4], LOW); // TURNS OFF DIGITAL_LEDS 5 - PIN 11

      digitalWrite(DIGITAL_LEDS[5], LOW); // TURNS OFF DIGITAL_LEDS 6 - PIN 12

    }

    // Note Off messages when each button is released

    if (digital[i].fallingEdge()) {

      usbMIDI.sendNoteOff(note[i], 0, channel);

      digitalWrite(DIGITAL_LEDS[i], HIGH); // TURN ON THE PRESSED BUTTON EQUIVALENT LED WHEN BUTTON IS RELEASED.

      Serial.print(" Note OFF ");

    }

  }

}
[/CODE]
 
Last edited:
Back
Top