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

Thread: here's some USB MIDI controller code

  1. #1

    here's some USB MIDI controller code

    Hey folks

    Just thought this might be handy for some - I've posted some relatively flexible and straightforward code on my website for using Teensy boards as USB MIDI controllers. It's only MIDI input, but that's still pretty fun. I have some good code for MIDI output too, but I need to format that a bit better before posting it.

    Anyway: http://www.yannseznec.com/works/teen...ntroller-code/

    The code has comments to show you where to input the number of pins you want to have each function, and which specific pin numbers to use for each function. For MIDI notes there are arrays where you can specify which notes to use. The different available functions in version 1.0 are:

    - Analog inputs to send CC values
    - Digital inputs to send note values
    - Digital inputs to send CC values
    - Touch pins to send note values (with constant velocity)

    Still to do:

    - Add ability to specify specific CC assignments for analog and digital inputs
    - Add ability to use Touch pins as CC (this is trickierů)

    I've used this code for a bunch of things I've built recently, such as the cigar box you can see me playing in this video. I suspect it is somewhat elementary for most of the users of this forum but I figured I'd post it here in case anyone comes a-looking. Some bits are kind of lazy but it's a start.

    Here's the entirety of the code in case you don't want to download it...



    Code:
    /* some code designed for using the Teensy 3 as a USB MIDI controller
    v1.0, December 17 2013
    by Yann Seznec www.yannseznec.com
    
    no copyright or anything, use however you want
    
    remember to select MIDI as your USB Type in the Tools menu
    
    this should also work with other Teensy boards, apart from the "touch" pins
    
    things that are kind of dumb in this code:
    - the touch threshold is hard coded (int touchThreshold)
    - touch pins only send midi note on/off with no velocity change
    - no system for sending touch pin as CC
    - the CC assignments are sort of random, probably should have another array for them
    
    */
    
    
    int const numPins = 9; //  number of analog inputs 
    int currentVal[numPins];
    int newVal[numPins];
    int analogPins[] = {  
      0,4,5,6,7,8,9,10,11   // which analog pins to use
    };
    
    
    int const numDigPins = 0; // number of digital pins to send note values
    int currentDig[numDigPins];
    int digitalpin[] = {
      2,3,4,5,6,7    // which digital pins to use for sending note values
    };
    int digitalpitch[] = {
      48,50,51,53,55,57}; // which midi notes to send from the digital pins selected above
    int digInput[numDigPins];
    
    
    int const numDigPinsCC = 9; // number of digital pins to send CC
    int currentDigcc[numDigPinsCC];
    int digitalpincc[] = {
       2,3,4,5,6,7,8,9,10 // which digital pins to use for sending CC
    };
    int digInputcc[numDigPinsCC];
    
    
    int const numTouchPins = 5; // number of pins to use as touchpins, sending note values
    int touch[numTouchPins];
    int touchon[numTouchPins];
    int touchpin[] = {
      0,1,15,16,17}; // which digital pins to use as touch pins
      int touchpitch[] = {
      60,63,65,67,70}; // which midi notes to send from the touch pins
      int touchThreshold = 2000; 
      int touchMax = 5000; 
    
    
    // the MIDI channel number to send messages
    const int channel = 1;
    
    
    
    void setup() {
      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);
      pinMode(12, INPUT_PULLUP); 
    
      Serial.begin(38400);
    
    }
    
    void loop() {
    
    //  touchpads
      for (int i = 0; i < numTouchPins; i++) {
        touch[i] = touchRead(touchpin[i]); 
    
        if (touch[i] > touchThreshold && touchon[i] == 0) {
    usbMIDI.sendControlChange(i+25,map(touch[i], touchThreshold, touchMax, 0, 127),4);
          touchon[i] = 1;
        }
        if (touch[i] < touchThreshold && touchon[i] == 1) {
    usbMIDI.sendControlChange(i+25,0,4);
          touchon[i] = 0;
        }
    
      }
     
    
    // digital pins sending notes
      for (int i = 0; i < numDigPins; i++) {
        if (digitalRead(digitalpin[i]) == 1 && currentDig[i] == 0) {
          usbMIDI.sendNoteOff(digitalpitch[i], 100, channel); 
          currentDig[i] = 1;
        }  
        if (digitalRead(digitalpin[i]) == 0  && currentDig[i] == 1) {
          usbMIDI.sendNoteOn(digitalpitch[i], 100, channel);
          currentDig[i] = 0;
        }  
      }
    
    // digital pins sending CC
    
      for (int i = 0; i < numDigPinsCC; i++) {
        if (digitalRead(digitalpincc[i]) == 1 && currentDigcc[i] == 0) {
          usbMIDI.sendControlChange(i+50, 0, channel); 
          currentDigcc[i] = 1;
        }  
        if (digitalRead(digitalpincc[i]) == 0  && currentDigcc[i] == 1) {
          usbMIDI.sendControlChange(i+50, 127, channel);
          currentDigcc[i] = 0;
        }  
      }
    
    // analog pins
    
      for (int i = 0; i < numPins; i++) {
    
        newVal[i] = analogRead(analogPins[i]);
    
        if (abs(newVal[i] - currentVal[i])>3) {
          usbMIDI.sendControlChange(i+1, newVal[i]>>3, channel); 
          currentVal[i] = newVal[i];
        }  
      }
      
      // i think if you remove these last two lines everything breaks and things are sad and people cry
      while (usbMIDI.read()); // read and discard any incoming MIDI messages
      delay(25); 
    }

  2. #2
    lolol i guess i need resistors to conect between the footswitches and the chip?

Tags for this Thread

Posting Permissions

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