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

Thread: Teensy 2.0 Code MIDI Box Help!!

  1. #26
    Junior Member
    Join Date
    Jun 2019
    Posts
    17
    Quote Originally Posted by oddson View Post
    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))??

  2. #27
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,138
    Buttons can be active even if not connected as they work by pulling the signal low while an internal pullup resistor, activated in the setup, otherwise keeps the signal high; even if nothing is connected

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

    A momentary switch pulls the signal to ground and the bounce library sends a fallingEdge event when the chatter stops and the signal is at ground.

    https://www.pjrc.com/teensy/td_libs_Bounce.html
    Last edited by oddson; 06-16-2019 at 02:06 AM.

  3. #28
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,138
    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.

  4. #29
    Junior Member
    Join Date
    Jun 2019
    Posts
    17
    Quote Originally Posted by oddson View Post
    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

  5. #30
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,138
    You dont.... button part is in previous post.

    Connect digital pins to ground via momentary switches..

  6. #31
    Junior Member
    Join Date
    Jun 2019
    Posts
    17
    Quote Originally Posted by oddson View Post
    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?

    Click image for larger version. 

Name:	skatla 1.jpg 
Views:	7 
Size:	111.3 KB 
ID:	16793Click image for larger version. 

Name:	skatla 2.jpg 
Views:	7 
Size:	99.8 KB 
ID:	16794

  7. #32
    Senior Member oddson's Avatar
    Join Date
    Feb 2013
    Location
    Isle in the Salish Sea
    Posts
    1,138
    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.

  8. #33
    Junior Member
    Join Date
    Jun 2019
    Posts
    17
    Quote Originally Posted by oddson View Post
    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??

  9. #34
    Junior Member
    Join Date
    Jun 2019
    Posts
    17
    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.

  10. #35
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,323
    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?

Posting Permissions

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