Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: usbMIDI controller for teensy 3.6 with 20 pots and 14 buttons

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    6

    usbMIDI controller for teensy 3.6 with 20 pots and 14 buttons

    Hi there,

    I'm new here and to teensy so don't really have a clue what I'm talking about but I'm trying to make a midi controller through teensy 3.6 for 20 pots and 14 buttons/switches, so far I've managed to successfully code the switches but don't really have a clue about the pots. I've tried various bits of ode from the internet but haven't managed to get them to do what i want!

    #include <Bounce.h>
    // cc values for buttons
    int cc_off = 0;
    int cc_on = 127;

    // map buttons to cc for button
    int cc0 = 51;
    int cc1 = 52;
    int cc2 = 53;
    int cc3 = 54;
    int cc4 = 55;
    int cc5 = 56;
    int cc6 = 57;
    int cc7 = 58;
    int cc8 = 59;
    int cc9 = 60;
    int cc10 = 61;
    int cc11 = 62;
    int cc12 = 63;
    int cc13 = 64;



    Bounce button0 = Bounce(0, 13);
    Bounce button1 = Bounce(1, 13); // 5 = 5 ms debounce time
    Bounce button2 = Bounce(2, 13); // which is appropriate for good
    Bounce button3 = Bounce(3, 13); // quality mechanical pushbuttons
    Bounce button4 = Bounce(4, 13);
    Bounce button5 = Bounce(5, 13); // if a button is too "sensitive"
    Bounce button6 = Bounce(6, 13); // to rapid touch, you can
    Bounce button7 = Bounce(7, 13); // increase this time.
    Bounce button8 = Bounce(8, 13);
    Bounce button9 = Bounce(9, 13);
    Bounce button10 = Bounce(10, 13);
    Bounce button11 = Bounce(11, 13);
    Bounce button12 = Bounce(12, 13);
    Bounce button13 = Bounce(13, 13);



    void setup() {
    // Configure the pins for input mode with pullup resistors.
    // The pushbuttons connect from each pin to ground. When
    // the button is pressed, the pin reads LOW because the button
    // shorts it to ground. When released, the pin reads HIGH
    // because the pullup resistor connects to +5 volts inside
    // the chip. LOW for "on", and HIGH for "off" may seem
    // backwards, but using the on-chip pullup resistors is very
    // convenient. The scheme is called "active low", and it's
    // very commonly used in electronics... so much that the chip
    // has built-in pullup resistors!
    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); // Teensy++ 2.0 LED, may need 1k resistor pullup
    pinMode(7, INPUT_PULLUP);
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);
    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    pinMode(13, 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); // Teensy++ 2.0 LED, may need 1k resistor 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);
    pinMode(28, INPUT_PULLUP);
    pinMode(29, INPUT_PULLUP);
    pinMode(30, INPUT_PULLUP);
    pinMode(31, INPUT_PULLUP);
    pinMode(32, INPUT_PULLUP);
    pinMode(33, INPUT_PULLUP);
    }


    void loop() {


    // 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();



    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 (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);
    }

    }



    Thats my code so far, it works fine for the buttons, this is the closest code for working i could find for the pots:




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



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

    its the only thing i could actually get a reading on, but when i run it through MainStage it has lots of interference and cc5 constantly flicks between 90-95! Which somewhat breaks it and my computer cant process it all in time, the other knobs work but only halfway, when the pot is on min and max it translates cc to 0 and middle is 80, i just want a 0-127 as i turn the pot up, does anyone know how to fix all of these?

  2. #2
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    * The pins that they are on:
    0-13 buttons/switches
    14-34 pots (10 knobs 10 sliders)

    dont really mind what cc values they translate to, i can just assign in mainstage

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    * The pins that they are on:
    0-13 buttons/switches
    14-34 pots (10 knobs 10 sliders)

    dont really mind what cc values they translate to, i can just assign in mainstage!

    thank you for your time

  4. #4
    Senior Member
    Join Date
    Aug 2019
    Location
    Melbourne Australia
    Posts
    155
    Maybe some pot wiring issue with those odd sounding read values. A picture or wiring diagram of your project would be helpful.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    so the switches, one pin is connected to ground the other to a pin on the board
    pots: left one plus the top to ground, right to 3.3v(still works when not connected), and middle to pin on the board
    there are also lots of LEDs round the edges

    a few other things:
    when i connect the 3.3v the teensy completely stops and gets quite hot round the usb socket, I'm presuming this was because the LEDs were over the ampage of the usb and couldn't take anymore, switches and pots still work 'fine' without the 3.3v though so i was planning on just powering it from an old model train controller and powering the LEDs completely from that, i also guessed the usb might be overloaded on the ground??
    it does work for a bit when i connect the 3.3v but then it stops

    don't worry about the mess around the board, i had to solder the original lead to a breadboard jumper one so it would be easier to solder straight to the board.

    hope that answered it??
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG_0282.jpg 
Views:	13 
Size:	178.8 KB 
ID:	21309  

  6. #6
    Senior Member
    Join Date
    Aug 2019
    Location
    Melbourne Australia
    Posts
    155
    Thank you, a picture says a thousand words.

    Leds must have a resistor in series to limit the current otherwise they will fry themselves or fry what is trying to power them or worse, both.

    I think I see 30 or so Leds all wired to 3.3v and not a resistor anywhere so when you power it up, you have a bunch of Leds all trying to fry themselves and the poor 3.3v regulator on the Teensy gets overloaded and shuts down.

    I expect that at least one (or more) of the Leds IS fried and to figure out which ones are dead, every single one needs to be disconnected to test it so you need to start again only this time, just focus on wiring up the switches and pots and getting them working first and add the glowy bits later.

  7. #7
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    I've disconnected the LEDs, but the board still doesn't work while the 3.3v is connected, however the pots work fine without them, heres a screenshot of the midi message thats coming through, i understand its probably a dodgy pot but would be good if theres a way to fix it. the pots still sort of go halfway up then down again, do you have any idea how to fix it?
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Screen Shot 2020-08-09 at 10.01.33.jpg 
Views:	10 
Size:	163.5 KB 
ID:	21323  

  8. #8
    Senior Member
    Join Date
    Jul 2020
    Posts
    350
    Quote Originally Posted by JoshC View Post
    so the switches, one pin is connected to ground the other to a pin on the board
    pots: left one plus the top to ground, right to 3.3v(still works when not connected), and middle to pin on the board
    there are also lots of LEDs round the edges
    The layout for your pot wiring is going to be very susceptible to noise pick up - you're running the signal wires without
    an accompanying ground, making a whole set of large loops to act as antennas.

    I would run all the pot signal wires in the same loom as their 0V and 3.3V wires, so no loops are created, or use
    a metal enclosure to shield the whole thing. And I'd probably have used a ribbon cable for the signals to keep
    things organized and neat.

  9. #9
    Junior Member
    Join Date
    Aug 2020
    Posts
    6
    ok, don't think i can do any of that sorry. i think its probably just a dodgy pot, they were very cheap! if after i run the code it still dosent work ill just remove the pot

  10. #10
    Senior Member
    Join Date
    Aug 2019
    Location
    Melbourne Australia
    Posts
    155
    Sorry, I'd completely missed this:-

    In this part of your code

    Code:
    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); // Teensy++ 2.0 LED, may need 1k resistor pullup
    pinMode(7, INPUT_PULLUP);
    pinMode(8, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
    pinMode(10, INPUT_PULLUP);
    pinMode(11, INPUT_PULLUP);
    pinMode(12, INPUT_PULLUP);
    pinMode(13, 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); // Teensy++ 2.0 LED, may need 1k resistor 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);
    pinMode(28, INPUT_PULLUP);
    pinMode(29, INPUT_PULLUP);
    pinMode(30, INPUT_PULLUP);
    pinMode(31, INPUT_PULLUP);
    pinMode(32, INPUT_PULLUP);
    pinMode(33, INPUT_PULLUP);
    Sets the pins up to do digital reads which is what is needed for the buttons, but not for pins which have the pots connected to them.

    Delete this bunch

    Code:
    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); // Teensy++ 2.0 LED, may need 1k resistor 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);
    pinMode(28, INPUT_PULLUP);
    pinMode(29, INPUT_PULLUP);
    pinMode(30, INPUT_PULLUP);
    pinMode(31, INPUT_PULLUP);
    pinMode(32, INPUT_PULLUP);
    pinMode(33, INPUT_PULLUP);
    Because we want them to stay as analog pins.

    Hope this helps.

Posting Permissions

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