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

Thread: MIDI stompbox + expedal (spdt switches - help with code)

Threaded View

  1. #1
    Junior Member
    Join Date
    Oct 2019

    MIDI stompbox + expedal (spdt switches - help with code)

    Greetings all!
    First time here and first teensy project.

    So im building a stompbox for my bandmate. It is a four button + expedal.
    Iv got almost everything working exept the behavior of the switches.

    The switches are spdt-switches, the problem is that it only sends midiCC when the switches are in gnd state(pullup) hence i have to press two times (instead of one) for a mapped button in my daw to change (since its a toggleswitch), the code seems to be for momentary buttons.

    Im using the code posted by oddson: mod on "manybuttonsknobs"
    // 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 = 1; // number of Analog PINS
    const int D_PINS = 4; // number of Digital PINS
    const int ON_VALUE = 127; // 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] = {A1};
    const int CCID[A_PINS] = {21};
    // define the pins and notes for digital events
    const int DIGITAL_PINS[D_PINS] = {7,8,9,10};
    const int CC[D_PINS] = {60,61,62,63};
    const int BOUNCE_TIME = 7; // 5 ms is usually sufficient
    const boolean toggled = false;
    // 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
    byte CClast[D_PINS];
    //************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[]{
    // initialize the bounce objects 
    Bounce digital[] =   {
      Bounce(DIGITAL_PINS[3], BOUNCE_TIME)/*,
      Bounce(DIGITAL_PINS[5], BOUNCE_TIME)/*,
      Bounce(DIGITAL_PINS[7], BOUNCE_TIME),*/
    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);
    void loop() {
      while ( {
         // 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
        // 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++){
        if (digital[i].fallingEdge()) {
          if (CClast[i]){
            usbMIDI.sendControlChange(CC[i], 0, channel);
            CClast[i] = 0;
            usbMIDI.sendControlChange(CC[i], ON_VALUE, channel);
            CClast[i] = ON_VALUE;
        // rising ignored for toggles
    i dont really know how to do a wiringdiagram but this is basically how the wiring is done.

    Click image for larger version. 

Name:	output.jpg 
Views:	11 
Size:	57.0 KB 
ID:	17952

    So, i humbly ask for help regarding the code to make it momentary (since its toggle switches), also dont really know how to make a wiringdiagram..

    Any help is appreciated, take care
    Last edited by Lemonmeth; 10-18-2019 at 08:42 PM. Reason: spelling

Posting Permissions

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