Updates in MIDI Library, anyone?

Revalogics

Well-known member
I have a sketch that uses the MIDI library, and some other library. My code, as of Teensyduino 1.36 works flawlessly, as I didn't have an issue with it while running it on Teensy 3.6 + external circuitry. However, as I updated my Teensyduino to version 1.37, there appears an error about this MIDI library. I checked the library files, and found out that it was completely changed, much more like how one uses usbMIDI callbacks.

I think this MIDI Library page must be updated.

My original MIDI function using switch-case like in the page mentioned, but revised to work with the library
Code:
void HWMIDIupdate() { // called inside loop
  if(MIDI.read()) {
    MIDIRX1 = 1;
    MIDIRX1t = 0;
    byte type = MIDI.getType();
    byte note, velocity, channel, control, value, program, d1, d2;
    int d3, pitch;
    switch(type) {
      case midi::NoteOff:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        if(chsw[0]) {
          usbMIDI.sendNoteOff(note, velocity, channels[0]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[1]) {
          usbMIDI.sendNoteOff(note, velocity, channels[1]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[2]) {
          usbMIDI.sendNoteOff(note, velocity, channels[2]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[3]) {
          usbMIDI.sendNoteOff(note, velocity, channels[3]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[4]) {
          usbMIDI.sendNoteOff(note, velocity, channels[4]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[5]) {
          usbMIDI.sendNoteOff(note, velocity, channels[5]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[6]) {
          usbMIDI.sendNoteOff(note, velocity, channels[6]);
          MIDITX = 1;
          MIDITXt = 0;
        }
        if(chsw[7]) {
          usbMIDI.sendNoteOff(note, velocity, channels[7]);
          MIDITX = 1;
          MIDITXt = 0;
        }
      break;
      case midi::NoteOn:
        note = MIDI.getData1();
        velocity = MIDI.getData2();
        channel = MIDI.getChannel();
        if(velocity == 0) {
          if(chsw[0]) {
            usbMIDI.sendNoteOff(note, 127, channels[0]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[1]) {
            usbMIDI.sendNoteOff(note, 127, channels[1]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[2]) {
            usbMIDI.sendNoteOff(note, 127, channels[2]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[3]) {
            usbMIDI.sendNoteOff(note, 127, channels[3]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[4]) {
            usbMIDI.sendNoteOff(note, 127, channels[4]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[5]) {
            usbMIDI.sendNoteOff(note, 127, channels[5]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[6]) {
            usbMIDI.sendNoteOff(note, 127, channels[6]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[7]) {
            usbMIDI.sendNoteOff(note, 127, channels[7]);
            MIDITX = 1;
            MIDITXt = 0;
          }
        }
        else {
          if(chsw[0]) {
            usbMIDI.sendNoteOn(note, velocity, channels[0]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[1]) {
            usbMIDI.sendNoteOn(note, velocity, channels[1]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[2]) {
            usbMIDI.sendNoteOn(note, velocity, channels[2]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[3]) {
            usbMIDI.sendNoteOn(note, velocity, channels[3]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[4]) {
            usbMIDI.sendNoteOn(note, velocity, channels[4]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[5]) {
            usbMIDI.sendNoteOn(note, velocity, channels[5]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[6]) {
            usbMIDI.sendNoteOn(note, velocity, channels[6]);
            MIDITX = 1;
            MIDITXt = 0;
          }
          if(chsw[7]) {
            usbMIDI.sendNoteOn(note, velocity, channels[7]);
            MIDITX = 1;
            MIDITXt = 0;
          }
        }
      break;
      case midi::ControlChange:
        control = MIDI.getData1();
        value = MIDI.getData2();
        channel = MIDI.getChannel();
        if(control == 1 || control == 64) {
          if(control == 1) {
            masterVolume = value;
            for(int i = 0; i < 8; i++) {
              potsSend(i, 1);
            }
          }
          if(control == 64) {
            if(value == 127) PEDAL = 1;
            if(value == 0) PEDAL = 0;
            usbMIDI.sendControlChange(control, value, channels[0]);
            usbMIDI.sendControlChange(control, value, channels[1]);
            usbMIDI.sendControlChange(control, value, channels[2]);
            usbMIDI.sendControlChange(control, value, channels[3]);
            usbMIDI.sendControlChange(control, value, channels[4]);
            usbMIDI.sendControlChange(control, value, channels[5]);
            usbMIDI.sendControlChange(control, value, channels[6]);
            usbMIDI.sendControlChange(control, value, channels[7]);
            MIDITX = 1;
            MIDITXt = 0;
          }
        }
      break;
      case midi::ProgramChange:
        program = MIDI.getData1();
        channel = MIDI.getChannel();
        //usbMIDI.sendProgramChange(program, channel);
      break;
      case midi::PitchBend:
        d1 = MIDI.getData1();
        d2 = MIDI.getData2();
        d3 = d2 << 9;
        pitch = (d3 >> 2) + d1;
        channel = MIDI.getChannel();
        usbMIDI.sendPitchBend(pitch, channels[0]);
        usbMIDI.sendPitchBend(pitch, channels[1]);
        usbMIDI.sendPitchBend(pitch, channels[2]);
        usbMIDI.sendPitchBend(pitch, channels[3]);
        usbMIDI.sendPitchBend(pitch, channels[4]);
        usbMIDI.sendPitchBend(pitch, channels[5]);
        usbMIDI.sendPitchBend(pitch, channels[6]);
        usbMIDI.sendPitchBend(pitch, channels[7]);
        MIDITX = 1;
        MIDITXt = 0;
      break;
      default:
      break;
    }
  }
}

My revised MIDI function using callbacks
Code:
void setup() {
  usbMIDI.setHandleNoteOff(MIDInoteOff);
  usbMIDI.setHandleNoteOn(MIDInoteOn);
  usbMIDI.setHandleControlChange(MIDIcontrolChange);
  usbMIDI.setHandleProgramChange(MIDIprogramChange);
  usbMIDI.setHandlePitchChange(MIDIpitchChange);
  MIDI.setHandleNoteOff(MIDInoteOff);
  MIDI.setHandleNoteOn(MIDInoteOn);
  MIDI.setHandleControlChange(MIDIcontrolChange);
  MIDI.setHandleProgramChange(MIDIprogramChange);
  MIDI.setHandlePitchBend(MIDIpitchChange);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  // code truncated, it's too long
}

void HWMIDIupdate() { // called inside loop()
  if(MIDI.read()) {
    MIDIRX1 = 1; // variable used to turn on an LED
    MIDIRX1t = 0;
  }
}

void MIDInoteOff(byte channel, byte note, byte velocity) {
  if(chsw[0]) {
    usbMIDI.sendNoteOff(note, velocity, channels[0]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[1]) {
    usbMIDI.sendNoteOff(note, velocity, channels[1]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[2]) {
    usbMIDI.sendNoteOff(note, velocity, channels[2]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[3]) {
    usbMIDI.sendNoteOff(note, velocity, channels[3]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[4]) {
    usbMIDI.sendNoteOff(note, velocity, channels[4]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[5]) {
    usbMIDI.sendNoteOff(note, velocity, channels[5]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[6]) {
    usbMIDI.sendNoteOff(note, velocity, channels[6]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[7]) {
    usbMIDI.sendNoteOff(note, velocity, channels[7]);
    MIDITX = 1;
    MIDITXt = 0;
  }
}
void MIDInoteOn(byte channel, byte note, byte velocity) {
  if(velocity == 0) {
    if(chsw[0]) {
      usbMIDI.sendNoteOff(note, 127, channels[0]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[1]) {
      usbMIDI.sendNoteOff(note, 127, channels[1]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[2]) {
      usbMIDI.sendNoteOff(note, 127, channels[2]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[3]) {
      usbMIDI.sendNoteOff(note, 127, channels[3]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[4]) {
      usbMIDI.sendNoteOff(note, 127, channels[4]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[5]) {
      usbMIDI.sendNoteOff(note, 127, channels[5]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[6]) {
      usbMIDI.sendNoteOff(note, 127, channels[6]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[7]) {
      usbMIDI.sendNoteOff(note, 127, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
  else {
    if(chsw[0]) {
      usbMIDI.sendNoteOn(note, velocity, channels[0]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[1]) {
      usbMIDI.sendNoteOn(note, velocity, channels[1]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[2]) {
      usbMIDI.sendNoteOn(note, velocity, channels[2]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[3]) {
      usbMIDI.sendNoteOn(note, velocity, channels[3]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[4]) {
      usbMIDI.sendNoteOn(note, velocity, channels[4]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[5]) {
      usbMIDI.sendNoteOn(note, velocity, channels[5]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[6]) {
      usbMIDI.sendNoteOn(note, velocity, channels[6]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[7]) {
      usbMIDI.sendNoteOn(note, velocity, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
}
void MIDIcontrolChange(byte channel, byte control, byte value) {
  if(control == 1 || control == 64) {
    if(control == 1) {
      masterVolume = value;
      for(int i = 0; i < 8; i++) {
        potsSend(i, 1);
      }
    }
    if(control == 64) {
      if(value == 127) PEDAL = 1;
      if(value == 0) PEDAL = 0;
      usbMIDI.sendControlChange(control, value, channels[0]);
      usbMIDI.sendControlChange(control, value, channels[1]);
      usbMIDI.sendControlChange(control, value, channels[2]);
      usbMIDI.sendControlChange(control, value, channels[3]);
      usbMIDI.sendControlChange(control, value, channels[4]);
      usbMIDI.sendControlChange(control, value, channels[5]);
      usbMIDI.sendControlChange(control, value, channels[6]);
      usbMIDI.sendControlChange(control, value, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
}
void MIDIprogramChange(byte channel, byte program) {}
void MIDIpitchChange(byte channel, int pitch) {
  usbMIDI.sendPitchBend(pitch, channels[0]);
  usbMIDI.sendPitchBend(pitch, channels[1]);
  usbMIDI.sendPitchBend(pitch, channels[2]);
  usbMIDI.sendPitchBend(pitch, channels[3]);
  usbMIDI.sendPitchBend(pitch, channels[4]);
  usbMIDI.sendPitchBend(pitch, channels[5]);
  usbMIDI.sendPitchBend(pitch, channels[6]);
  usbMIDI.sendPitchBend(pitch, channels[7]);
  MIDITX = 1;
  MIDITXt = 0;
}

In case you want to examine my whole sketch, here it is: (1605 lines)
Code:
// Started June 20, 2017, 12:06:08 AM
// Programmable harware used is Teensy 3.6
//#define USE_VOCODER
#ifndef ENCLOSETHIS
/** BOTTOM SIDE **/
//MIDI Rx         0
//MIDI Tx         1
#define LRS       2
//I2S Tx          3
#define LRW       4
#define LEN       5
#define LD4       6
#define LD5       7
#define LD6       8
#define LD7       9
#define ENC1A     10
#define ENC1B     11
#define ENC1BTN   12
#define ENC2A     25
#define ENC2B     26
//I2S Rx          27
#define ENC2BTN   28
//I2S BCLK        29
//I2S LRCLK       30
/** TOP SIDE **/
#define VPOT01    23
#define VPOT02    22
#define VPOT03    21
#define VPOT04    20
#define VPOT05    19
#define VPOT06    18
#define VPOT07    17
#define VPOT08    16
#define BPMBTN    15
#define BPMLED    14
//I2S MCLK        35
#define LEDLAT    36  // 74HC595 pin 12
#define LEDCLK    37  // 74HC595 pin 11
#define LEDDAT    38  // 74HC595 pin 14

#include <LiquidCrystalFast.h>
#include <Audio.h>
#include <MIDI.h>
#include <ResponsiveAnalogRead.h>
elapsedMicros BPM1t;
elapsedMicros BPM2t;
elapsedMicros OFFt;
elapsedMillis BPMBTNt;
elapsedMillis BPMLEDt;
elapsedMillis ENC1BTNt;
elapsedMillis ENC2BTNt;
elapsedMillis LCDt;
elapsedMillis MIDIRX1t;
elapsedMillis MIDIRX2t;
elapsedMillis MIDITXt;
elapsedMillis POTSt;
elapsedMillis SETTINGSt = 2001;
elapsedMillis SERIALt;
IntervalTimer interruptThisFunction;
LiquidCrystalFast MC2004(LRS, LRW, LEN, LD4, LD5, LD6, LD7);
MIDI_CREATE_DEFAULT_INSTANCE();
ResponsiveAnalogRead vPot1(VPOT01, true, 0.01);
ResponsiveAnalogRead vPot2(VPOT02, true, 0.01);
ResponsiveAnalogRead vPot3(VPOT03, true, 0.01);
ResponsiveAnalogRead vPot4(VPOT04, true, 0.01);
ResponsiveAnalogRead vPot5(VPOT05, true, 0.01);
ResponsiveAnalogRead vPot6(VPOT06, true, 0.01);
ResponsiveAnalogRead vPot7(VPOT07, true, 0.01);
ResponsiveAnalogRead vPot8(VPOT08, true, 0.01);
volatile byte chsw[8], r[8], g[8], b[8], potc[8], potp[8], pots[8], BPM, thisnum, pwmCounter, mode, masterVolume = 127,
bpmValue = 128,
channels[8] = {1, 3, 5, 7, 9, 11, 13, 15},
CClist[8] = {7, 1, 6, 100, 101, 7, 7, 7},
bank = 1,
globalCC = CClist[0];
volatile bool OFF[8], PEDAL, MIDIRX1, MIDIRX2, MIDITX, bpmButtonCheck, encoder1direction, encoder1rotated, encoder2direction, encoder2rotated,
br, bg, bb;
float rawPeaks[19], valPeaks[19];
float res = 5; // filter resonance
const float attack = 0.9;
float freq[19] = {
  110.0000000, // A2
  138.5913155, // C#3
  174.6141157, // F3
  220.0000000, // A3
  277.1826310, // C#4
  349.2282314, // F4
  440.0000000, // A4
  554.3652620, // C#5
  698.4564629, // F5
  880.0000000, // A5
  1108.730524, // C#6
  1396.912926, // F6
  1760.000000, // A6
  2217.461048, // C#7
  2793.825851, // F7
  3520.000000, // A7
  4434.922096, // C#8
  5587.651703, // F8
  7040.000000  // A8
};
#endif
#ifdef USE_VOCODER
AudioInputI2S                 AudioIn;
AudioMixer4                   M001;
AudioMixer4                   M002;
AudioConnection                 C001(AudioIn, 0, M001, 0);
AudioConnection                 C002(AudioIn, 1, M002, 0);

AudioFilterStateVariable      F101_1;
AudioConnection                 C003(M001, 0, F101_1, 0);
AudioFilterStateVariable      F101_2;
AudioConnection                 C004(F101_1, 0, F101_2, 0);
AudioAnalyzePeak              P101;
AudioConnection                 C005(F101_2, 0, P101, 0);
AudioFilterStateVariable      F201_1;
AudioConnection                 C006(M002, 0, F201_1, 0);
AudioFilterStateVariable      F201_2;
AudioConnection                 C007(F201_1, 0, F201_2, 0);
AudioMixer4                   M201;
AudioConnection                 C008(F201_2, 0, M201, 0);

AudioFilterStateVariable      F102_1;
AudioConnection                 C009(M001, 0, F102_1, 0);
AudioFilterStateVariable      F102_2;
AudioConnection                 C010(F102_1, 1, F102_2, 0);
AudioAnalyzePeak              P102;
AudioConnection                 C011(F102_2, 1, P102, 0);
AudioFilterStateVariable      F202_1;
AudioConnection                 C012(M002, 0, F202_1, 0);
AudioFilterStateVariable      F202_2;
AudioConnection                 C013(F202_1, 1, F202_2, 0);
AudioMixer4                   M202;
AudioConnection                 C014(F202_2, 1, M202, 0);

AudioFilterStateVariable      F103_1;
AudioConnection                 C015(M001, 0, F103_1, 0);
AudioFilterStateVariable      F103_2;
AudioConnection                 C016(F103_1, 1, F103_2, 0);
AudioAnalyzePeak              P103;
AudioConnection                 C017(F103_2, 1, P103, 0);
AudioFilterStateVariable      F203_1;
AudioConnection                 C018(M002, 0, F203_1, 0);
AudioFilterStateVariable      F203_2;
AudioConnection                 C019(F203_1, 1, F203_2, 0);
AudioMixer4                   M203;
AudioConnection                 C020(F203_2, 1, M203, 0);

AudioFilterStateVariable      F104_1;
AudioConnection                 C021(M001, 0, F104_1, 0);
AudioFilterStateVariable      F104_2;
AudioConnection                 C022(F104_1, 1, F104_2, 0);
AudioAnalyzePeak              P104;
AudioConnection                 C023(F104_2, 1, P104, 0);
AudioFilterStateVariable      F204_1;
AudioConnection                 C024(M002, 0, F204_1, 0);
AudioFilterStateVariable      F204_2;
AudioConnection                 C025(F204_1, 1, F204_2, 0);
AudioMixer4                   M204;
AudioConnection                 C026(F204_2, 1, M204, 0);

AudioFilterStateVariable      F105_1;
AudioConnection                 C027(M001, 0, F105_1, 0);
AudioFilterStateVariable      F105_2;
AudioConnection                 C028(F105_1, 1, F105_2, 0);
AudioAnalyzePeak              P105;
AudioConnection                 C029(F105_2, 1, P105, 0);
AudioFilterStateVariable      F205_1;
AudioConnection                 C030(M002, 0, F205_1, 0);
AudioFilterStateVariable      F205_2;
AudioConnection                 C031(F205_1, 1, F205_2, 0);
AudioMixer4                   M205;
AudioConnection                 C032(F205_2, 1, M205, 0);

AudioFilterStateVariable      F106_1;
AudioConnection                 C033(M001, 0, F106_1, 0);
AudioFilterStateVariable      F106_2;
AudioConnection                 C034(F106_1, 1, F106_2, 0);
AudioAnalyzePeak              P106;
AudioConnection                 C035(F106_2, 1, P106, 0);
AudioFilterStateVariable      F206_1;
AudioConnection                 C036(M002, 0, F206_1, 0);
AudioFilterStateVariable      F206_2;
AudioConnection                 C037(F206_1, 1, F206_2, 0);
AudioMixer4                   M206;
AudioConnection                 C038(F206_2, 1, M206, 0);

AudioFilterStateVariable      F107_1;
AudioConnection                 C039(M001, 0, F107_1, 0);
AudioFilterStateVariable      F107_2;
AudioConnection                 C040(F107_1, 1, F107_2, 0);
AudioAnalyzePeak              P107;
AudioConnection                 C041(F107_2, 1, P107, 0);
AudioFilterStateVariable      F207_1;
AudioConnection                 C042(M002, 0, F207_1, 0);
AudioFilterStateVariable      F207_2;
AudioConnection                 C043(F207_1, 1, F207_2, 0);
AudioMixer4                   M207;
AudioConnection                 C044(F207_2, 1, M207, 0);

AudioFilterStateVariable      F108_1;
AudioConnection                 C045(M001, 0, F108_1, 0);
AudioFilterStateVariable      F108_2;
AudioConnection                 C046(F108_1, 1, F108_2, 0);
AudioAnalyzePeak              P108;
AudioConnection                 C047(F108_2, 1, P108, 0);
AudioFilterStateVariable      F208_1;
AudioConnection                 C048(M002, 0, F208_1, 0);
AudioFilterStateVariable      F208_2;
AudioConnection                 C049(F208_1, 1, F208_2, 0);
AudioMixer4                   M208;
AudioConnection                 C050(F208_2, 1, M208, 0);

AudioFilterStateVariable      F109_1;
AudioConnection                 C051(M001, 0, F109_1, 0);
AudioFilterStateVariable      F109_2;
AudioConnection                 C052(F109_1, 1, F109_2, 0);
AudioAnalyzePeak              P109;
AudioConnection                 C053(F109_2, 1, P109, 0);
AudioFilterStateVariable      F209_1;
AudioConnection                 C054(M002, 0, F209_1, 0);
AudioFilterStateVariable      F209_2;
AudioConnection                 C055(F209_1, 1, F209_2, 0);
AudioMixer4                   M209;
AudioConnection                 C056(F209_2, 1, M209, 0);

AudioFilterStateVariable      F110_1;
AudioConnection                 C057(M001, 0, F110_1, 0);
AudioFilterStateVariable      F110_2;
AudioConnection                 C058(F110_1, 1, F110_2, 0);
AudioAnalyzePeak              P110;
AudioConnection                 C059(F110_2, 1, P110, 0);
AudioFilterStateVariable      F210_1;
AudioConnection                 C060(M002, 0, F210_1, 0);
AudioFilterStateVariable      F210_2;
AudioConnection                 C061(F210_1, 1, F210_2, 0);
AudioMixer4                   M210;
AudioConnection                 C062(F210_2, 1, M210, 0);

AudioFilterStateVariable      F111_1;
AudioConnection                 C063(M001, 0, F111_1, 0);
AudioFilterStateVariable      F111_2;
AudioConnection                 C064(F111_1, 1, F111_2, 0);
AudioAnalyzePeak              P111;
AudioConnection                 C065(F111_2, 1, P111, 0);
AudioFilterStateVariable      F211_1;
AudioConnection                 C066(M002, 0, F211_1, 0);
AudioFilterStateVariable      F211_2;
AudioConnection                 C067(F211_1, 1, F211_2, 0);
AudioMixer4                   M211;
AudioConnection                 C068(F211_2, 1, M211, 0);

AudioFilterStateVariable      F112_1;
AudioConnection                 C069(M001, 0, F112_1, 0);
AudioFilterStateVariable      F112_2;
AudioConnection                 C070(F112_1, 1, F112_2, 0);
AudioAnalyzePeak              P112;
AudioConnection                 C071(F112_2, 1, P112, 0);
AudioFilterStateVariable      F212_1;
AudioConnection                 C072(M002, 0, F212_1, 0);
AudioFilterStateVariable      F212_2;
AudioConnection                 C073(F212_1, 1, F212_2, 0);
AudioMixer4                   M212;
AudioConnection                 C074(F212_2, 1, M212, 0);

AudioFilterStateVariable      F113_1;
AudioConnection                 C075(M001, 0, F113_1, 0);
AudioFilterStateVariable      F113_2;
AudioConnection                 C076(F113_1, 1, F113_2, 0);
AudioAnalyzePeak              P113;
AudioConnection                 C077(F113_2, 1, P113, 0);
AudioFilterStateVariable      F213_1;
AudioConnection                 C078(M002, 0, F213_1, 0);
AudioFilterStateVariable      F213_2;
AudioConnection                 C079(F213_1, 1, F213_2, 0);
AudioMixer4                   M213;
AudioConnection                 C080(F213_2, 1, M213, 0);

AudioFilterStateVariable      F114_1;
AudioConnection                 C081(M001, 0, F114_1, 0);
AudioFilterStateVariable      F114_2;
AudioConnection                 C082(F114_1, 1, F114_2, 0);
AudioAnalyzePeak              P114;
AudioConnection                 C083(F114_2, 1, P114, 0);
AudioFilterStateVariable      F214_1;
AudioConnection                 C084(M002, 0, F214_1, 0);
AudioFilterStateVariable      F214_2;
AudioConnection                 C085(F214_1, 1, F214_2, 0);
AudioMixer4                   M214;
AudioConnection                 C086(F214_2, 1, M214, 0);

AudioFilterStateVariable      F115_1;
AudioConnection                 C087(M001, 0, F115_1, 0);
AudioFilterStateVariable      F115_2;
AudioConnection                 C088(F115_1, 1, F115_2, 0);
AudioAnalyzePeak              P115;
AudioConnection                 C089(F115_2, 1, P115, 0);
AudioFilterStateVariable      F215_1;
AudioConnection                 C090(M002, 0, F215_1, 0);
AudioFilterStateVariable      F215_2;
AudioConnection                 C091(F215_1, 1, F215_2, 0);
AudioMixer4                   M215;
AudioConnection                 C092(F215_2, 1, M215, 0);

AudioFilterStateVariable      F116_1;
AudioConnection                 C093(M001, 0, F116_1, 0);
AudioFilterStateVariable      F116_2;
AudioConnection                 C094(F116_1, 1, F116_2, 0);
AudioAnalyzePeak              P116;
AudioConnection                 C095(F116_2, 1, P116, 0);
AudioFilterStateVariable      F216_1;
AudioConnection                 C096(M002, 0, F216_1, 0);
AudioFilterStateVariable      F216_2;
AudioConnection                 C097(F216_1, 1, F216_2, 0);
AudioMixer4                   M216;
AudioConnection                 C098(F216_2, 1, M216, 0);

AudioFilterStateVariable      F117_1;
AudioConnection                 C099(M001, 0, F117_1, 0);
AudioFilterStateVariable      F117_2;
AudioConnection                 C100(F117_1, 1, F117_2, 0);
AudioAnalyzePeak              P117;
AudioConnection                 C101(F117_2, 1, P117, 0);
AudioFilterStateVariable      F217_1;
AudioConnection                 C102(M002, 0, F217_1, 0);
AudioFilterStateVariable      F217_2;
AudioConnection                 C103(F217_1, 1, F217_2, 0);
AudioMixer4                   M217;
AudioConnection                 C104(F217_2, 1, M217, 0);

AudioFilterStateVariable      F118_1;
AudioConnection                 C105(M001, 0, F118_1, 0);
AudioFilterStateVariable      F118_2;
AudioConnection                 C106(F118_1, 1, F118_2, 0);
AudioAnalyzePeak              P118;
AudioConnection                 C107(F118_2, 1, P118, 0);
AudioFilterStateVariable      F218_1;
AudioConnection                 C108(M002, 0, F218_1, 0);
AudioFilterStateVariable      F218_2;
AudioConnection                 C109(F218_1, 1, F218_2, 0);
AudioMixer4                   M218;
AudioConnection                 C110(F218_2, 1, M218, 0);

AudioFilterStateVariable      F119_1;
AudioConnection                 C111(M001, 0, F119_1, 0);
AudioFilterStateVariable      F119_2;
AudioConnection                 C112(F119_1, 2, F119_2, 0);
AudioAnalyzePeak              P119;
AudioConnection                 C113(F119_2, 2, P119, 0);
AudioFilterStateVariable      F219_1;
AudioConnection                 C114(M002, 0, F219_1, 0);
AudioFilterStateVariable      F219_2;
AudioConnection                 C115(F219_1, 2, F219_2, 0);
AudioMixer4                   M219;
AudioConnection                 C116(F219_2, 2, M219, 0);

AudioSynthNoiseWhite          noiseGen;
AudioFilterStateVariable      F220_1;
AudioConnection                 C117(noiseGen, 0, F220_1, 0);
AudioFilterStateVariable      F220_2;
AudioConnection                 C118(F220_1, 2, F220_2, 0);
AudioMixer4                   M220;
AudioConnection                 C119(F220_2, 2, M220, 0);

AudioMixer4                   M003;
AudioConnection                 C120(M201, 0, M003, 0);
AudioConnection                 C121(M202, 0, M003, 1);
AudioConnection                 C122(M203, 0, M003, 2);
AudioConnection                 C123(M204, 0, M003, 3);

AudioMixer4                   M004;
AudioConnection                 C124(M205, 0, M004, 0);
AudioConnection                 C125(M206, 0, M004, 1);
AudioConnection                 C126(M207, 0, M004, 2);
AudioConnection                 C127(M208, 0, M004, 3);

AudioMixer4                   M005;
AudioConnection                 C128(M209, 0, M005, 0);
AudioConnection                 C129(M210, 0, M005, 1);
AudioConnection                 C130(M211, 0, M005, 2);
AudioConnection                 C131(M212, 0, M005, 3);

AudioMixer4                   M006;
AudioConnection                 C132(M213, 0, M006, 0);
AudioConnection                 C133(M214, 0, M006, 1);
AudioConnection                 C134(M215, 0, M006, 2);
AudioConnection                 C135(M216, 0, M006, 3);

AudioMixer4                   M007;
AudioConnection                 C136(M217, 0, M007, 0);
AudioConnection                 C137(M218, 0, M007, 1);
AudioConnection                 C138(M219, 0, M007, 2);
AudioConnection                 C139(M220, 0, M007, 3);

AudioMixer4                   M008;
AudioConnection                 C140(M003, 0, M008, 0);
AudioConnection                 C141(M004, 0, M008, 1);
AudioConnection                 C142(M005, 0, M008, 2);
AudioConnection                 C143(M006, 0, M008, 3);

AudioMixer4                   M009;
AudioConnection                 C144(M007, 0, M009, 0);

AudioMixer4                   M010;
AudioConnection                 C145(M008, 0, M010, 0);
AudioConnection                 C146(M009, 0, M010, 1);
AudioConnection                 C147(M001, 0, M010, 2);

AudioMixer4                   M011;
AudioConnection                 C148(M008, 0, M011, 0);
AudioConnection                 C149(M009, 0, M011, 1);
AudioConnection                 C150(M002, 0, M011, 2);

AudioOutputI2S                AudioOut;
AudioConnection                 C151(M010, 0, AudioOut, 0);
AudioConnection                 C152(M011, 0, AudioOut, 1);

void vocoderBegin() {
  M001.gain(0, 0); // voice input gain
  M002.gain(0, 0); // instrument input gain
  M010.gain(0, 0); // vocoder out 1 level (16 bands) to voice out
  M010.gain(1, 0); // vocoder out 2 level (4 bands) to voice out
  M010.gain(2, 0); // voice dry level
  M011.gain(0, 0); // vocoder out 1 level (16 bands) to instrument out
  M011.gain(1, 0); // vocoder out 2 level (4 bands) to instrument out
  M011.gain(2, 0); // instrument dry level
  noiseGen.amplitude(0); // sibilance level
  M003.gain(0, 0.25); // level 2 vocoder bands mixer
  M003.gain(1, 0.25); // level 2 vocoder bands mixer
  M003.gain(2, 0.25); // level 2 vocoder bands mixer
  M003.gain(3, 0.25); // level 2 vocoder bands mixer
  M004.gain(0, 0.25); // level 2 vocoder bands mixer
  M004.gain(1, 0.25); // level 2 vocoder bands mixer
  M004.gain(2, 0.25); // level 2 vocoder bands mixer
  M004.gain(3, 0.25); // level 2 vocoder bands mixer
  M005.gain(0, 0.25); // level 2 vocoder bands mixer
  M005.gain(1, 0.25); // level 2 vocoder bands mixer
  M005.gain(2, 0.25); // level 2 vocoder bands mixer
  M005.gain(3, 0.25); // level 2 vocoder bands mixer
  M006.gain(0, 0.25); // level 2 vocoder bands mixer
  M006.gain(1, 0.25); // level 2 vocoder bands mixer
  M006.gain(2, 0.25); // level 2 vocoder bands mixer
  M006.gain(3, 0.25); // level 2 vocoder bands mixer
  M007.gain(0, 0.25); // level 2 vocoder bands mixer
  M007.gain(1, 0.25); // level 2 vocoder bands mixer
  M007.gain(2, 0.25); // level 2 vocoder bands mixer
  M007.gain(3, 0.25); // level 2 vocoder bands mixer
  M008.gain(0, 0.25); // level 3 vocoder bands mixer
  M008.gain(1, 0.25); // level 3 vocoder bands mixer
  M008.gain(2, 0.25); // level 3 vocoder bands mixer
  M008.gain(3, 0.25); // level 3 vocoder bands mixer
  M009.gain(0, 0.25); // level 3 vocoder bands mixer
  F101_1.frequency(freq[0]);
  F101_2.frequency(freq[0]);
  F201_1.frequency(freq[0]);
  F201_2.frequency(freq[0]);
  F102_1.frequency(freq[1]);
  F102_2.frequency(freq[1]);
  F202_1.frequency(freq[1]);
  F202_2.frequency(freq[1]);
  F103_1.frequency(freq[2]);
  F103_2.frequency(freq[2]);
  F203_1.frequency(freq[2]);
  F203_2.frequency(freq[2]);
  F104_1.frequency(freq[3]);
  F104_2.frequency(freq[3]);
  F204_1.frequency(freq[3]);
  F204_2.frequency(freq[3]);
  F105_1.frequency(freq[4]);
  F105_2.frequency(freq[4]);
  F205_1.frequency(freq[4]);
  F205_2.frequency(freq[4]);
  F106_1.frequency(freq[5]);
  F106_2.frequency(freq[5]);
  F206_1.frequency(freq[5]);
  F206_2.frequency(freq[5]);
  F107_1.frequency(freq[6]);
  F107_2.frequency(freq[6]);
  F207_1.frequency(freq[6]);
  F207_2.frequency(freq[6]);
  F108_1.frequency(freq[7]);
  F108_2.frequency(freq[7]);
  F208_1.frequency(freq[7]);
  F208_2.frequency(freq[7]);
  F109_1.frequency(freq[8]);
  F109_2.frequency(freq[8]);
  F209_1.frequency(freq[8]);
  F209_2.frequency(freq[8]);
  F110_1.frequency(freq[9]);
  F110_2.frequency(freq[9]);
  F210_1.frequency(freq[9]);
  F210_2.frequency(freq[9]);
  F111_1.frequency(freq[10]);
  F111_2.frequency(freq[10]);
  F211_1.frequency(freq[10]);
  F211_2.frequency(freq[10]);
  F112_1.frequency(freq[11]);
  F112_2.frequency(freq[11]);
  F212_1.frequency(freq[11]);
  F212_2.frequency(freq[11]);
  F113_1.frequency(freq[12]);
  F113_2.frequency(freq[12]);
  F213_1.frequency(freq[12]);
  F213_2.frequency(freq[12]);
  F114_1.frequency(freq[13]);
  F114_2.frequency(freq[13]);
  F214_1.frequency(freq[13]);
  F214_2.frequency(freq[13]);
  F115_1.frequency(freq[14]);
  F115_2.frequency(freq[14]);
  F215_1.frequency(freq[14]);
  F215_2.frequency(freq[14]);
  F116_1.frequency(freq[15]);
  F116_2.frequency(freq[15]);
  F216_1.frequency(freq[15]);
  F216_2.frequency(freq[15]);
  F117_1.frequency(freq[16]);
  F117_2.frequency(freq[16]);
  F217_1.frequency(freq[16]);
  F217_2.frequency(freq[16]);
  F118_1.frequency(freq[17]);
  F118_2.frequency(freq[17]);
  F218_1.frequency(freq[17]);
  F218_2.frequency(freq[17]);
  F119_1.frequency(freq[18]);
  F119_2.frequency(freq[18]);
  F219_1.frequency(freq[18]);
  F219_2.frequency(freq[18]);
  F220_1.frequency(freq[18]);
  F220_2.frequency(freq[18]);
  F101_1.resonance(res);
  F101_2.resonance(res);
  F201_1.resonance(res);
  F201_2.resonance(res);
  F102_1.resonance(res);
  F102_2.resonance(res);
  F202_1.resonance(res);
  F202_2.resonance(res);
  F103_1.resonance(res);
  F103_2.resonance(res);
  F203_1.resonance(res);
  F203_2.resonance(res);
  F104_1.resonance(res);
  F104_2.resonance(res);
  F204_1.resonance(res);
  F204_2.resonance(res);
  F105_1.resonance(res);
  F105_2.resonance(res);
  F205_1.resonance(res);
  F205_2.resonance(res);
  F106_1.resonance(res);
  F106_2.resonance(res);
  F206_1.resonance(res);
  F206_2.resonance(res);
  F107_1.resonance(res);
  F107_2.resonance(res);
  F207_1.resonance(res);
  F207_2.resonance(res);
  F108_1.resonance(res);
  F108_2.resonance(res);
  F208_1.resonance(res);
  F208_2.resonance(res);
  F109_1.resonance(res);
  F109_2.resonance(res);
  F209_1.resonance(res);
  F209_2.resonance(res);
  F110_1.resonance(res);
  F110_2.resonance(res);
  F210_1.resonance(res);
  F210_2.resonance(res);
  F111_1.resonance(res);
  F111_2.resonance(res);
  F211_1.resonance(res);
  F211_2.resonance(res);
  F101_1.resonance(res);
  F112_2.resonance(res);
  F212_1.resonance(res);
  F212_2.resonance(res);
  F113_1.resonance(res);
  F113_2.resonance(res);
  F213_1.resonance(res);
  F213_2.resonance(res);
  F114_1.resonance(res);
  F114_2.resonance(res);
  F214_1.resonance(res);
  F214_2.resonance(res);
  F115_1.resonance(res);
  F115_2.resonance(res);
  F215_1.resonance(res);
  F215_2.resonance(res);
  F116_1.resonance(res);
  F116_2.resonance(res);
  F216_1.resonance(res);
  F216_2.resonance(res);
  F117_1.resonance(res);
  F117_2.resonance(res);
  F217_1.resonance(res);
  F217_2.resonance(res);
  F118_1.resonance(res);
  F118_2.resonance(res);
  F218_1.resonance(res);
  F218_2.resonance(res);
  F119_1.resonance(res);
  F119_2.resonance(res);
  F219_1.resonance(res);
  F219_2.resonance(res);
  F220_1.resonance(res);
  F220_2.resonance(res);
  valPeaks[0] = 1;
  valPeaks[1] = 1;
  valPeaks[2] = 1;
  valPeaks[3] = 1;
  valPeaks[4] = 1;
  valPeaks[5] = 1;
  valPeaks[6] = 1;
  valPeaks[7] = 1;
  valPeaks[8] = 1;
  valPeaks[9] = 1;
  valPeaks[10] = 1;
  valPeaks[11] = 1;
  valPeaks[12] = 1;
  valPeaks[13] = 1;
  valPeaks[14] = 1;
  valPeaks[15] = 1;
  valPeaks[16] = 1;
  valPeaks[17] = 1;
  valPeaks[18] = 1;
  AudioMemory(64);
}
void vocoderUpdate() {
  if(P101.available()) rawPeaks[0] = P101.read();
  if(rawPeaks[0] > valPeaks[0]) {
    valPeaks[0] /= attack;
    M201.gain(0, valPeaks[0]);
  }
  if(rawPeaks[0] < valPeaks[0]) {
    valPeaks[0] *= attack;
    M201.gain(0, valPeaks[0]);
  }
  if(P102.available()) rawPeaks[1] = P102.read();
  if(rawPeaks[1] > valPeaks[1]) {
    valPeaks[1] /= attack;
    M202.gain(0, valPeaks[1]);
  }
  if(rawPeaks[1] < valPeaks[1]) {
    valPeaks[1] *= attack;
    M202.gain(0, valPeaks[1]);
  }
  if(P103.available()) rawPeaks[2] = P103.read();
  if(rawPeaks[2] > valPeaks[2]) {
    valPeaks[2] /= attack;
    M203.gain(0, valPeaks[2]);
  }
  if(rawPeaks[2] < valPeaks[2]) {
    valPeaks[2] *= attack;
    M203.gain(0, valPeaks[2]);
  }
  if(P104.available()) rawPeaks[3] = P104.read();
  if(rawPeaks[3] > valPeaks[3]) {
    valPeaks[3] /= attack;
    M204.gain(0, valPeaks[3]);
  }
  if(rawPeaks[3] < valPeaks[3]) {
    valPeaks[3] *= attack;
    M204.gain(0, valPeaks[3]);
  }
  if(P105.available()) rawPeaks[4] = P105.read();
  if(rawPeaks[4] > valPeaks[4]) {
    valPeaks[4] /= attack;
    M205.gain(0, valPeaks[4]);
  }
  if(rawPeaks[4] < valPeaks[4]) {
    valPeaks[4] *= attack;
    M205.gain(0, valPeaks[4]);
  }
  if(P106.available()) rawPeaks[5] = P106.read();
  if(rawPeaks[5] > valPeaks[5]) {
    valPeaks[5] /= attack;
    M206.gain(0, valPeaks[5]);
  }
  if(rawPeaks[5] < valPeaks[5]) {
    valPeaks[5] *= attack;
    M206.gain(0, valPeaks[5]);
  }
  if(P107.available()) rawPeaks[6] = P107.read();
  if(rawPeaks[6] > valPeaks[6]) {
    valPeaks[6] /= attack;
    M207.gain(0, valPeaks[6]);
  }
  if(rawPeaks[6] < valPeaks[6]) {
    valPeaks[6] *= attack;
    M207.gain(0, valPeaks[6]);
  }
  if(P108.available()) rawPeaks[7] = P108.read();
  if(rawPeaks[7] > valPeaks[7]) {
    valPeaks[7] /= attack;
    M208.gain(0, valPeaks[7]);
  }
  if(rawPeaks[7] < valPeaks[7]) {
    valPeaks[7] *= attack;
    M208.gain(0, valPeaks[7]);
  }
  if(P109.available()) rawPeaks[8] = P109.read();
  if(rawPeaks[8] > valPeaks[8]) {
    valPeaks[8] /= attack;
    M209.gain(0, valPeaks[8]);
  }
  if(rawPeaks[8] < valPeaks[8]) {
    valPeaks[8] *= attack;
    M209.gain(0, valPeaks[8]);
  }
  if(P110.available()) rawPeaks[9] = P110.read();
  if(rawPeaks[9] > valPeaks[9]) {
    valPeaks[9] /= attack;
    M210.gain(0, valPeaks[9]);
  }
  if(rawPeaks[9] < valPeaks[9]) {
    valPeaks[9] *= attack;
    M210.gain(0, valPeaks[9]);
  }
  if(P111.available()) rawPeaks[10] = P111.read();
  if(rawPeaks[10] > valPeaks[10]) {
    valPeaks[10] /= attack;
    M211.gain(0, valPeaks[10]);
  }
  if(rawPeaks[10] < valPeaks[10]) {
    valPeaks[10] *= attack;
    M211.gain(0, valPeaks[10]);
  }
  if(P112.available()) rawPeaks[11] = P112.read();
  if(rawPeaks[11] > valPeaks[11]) {
    valPeaks[11] /= attack;
    M212.gain(0, valPeaks[11]);
  }
  if(rawPeaks[11] < valPeaks[11]) {
    valPeaks[11] *= attack;
    M212.gain(0, valPeaks[11]);
  }
  if(P113.available()) rawPeaks[12] = P113.read();
  if(rawPeaks[12] > valPeaks[12]) {
    valPeaks[12] /= attack;
    M213.gain(0, valPeaks[12]);
  }
  if(rawPeaks[12] < valPeaks[12]) {
    valPeaks[12] *= attack;
    M213.gain(0, valPeaks[12]);
  }
  if(P114.available()) rawPeaks[13] = P114.read();
  if(rawPeaks[13] > valPeaks[13]) {
    valPeaks[13] /= attack;
    M214.gain(0, valPeaks[13]);
  }
  if(rawPeaks[13] < valPeaks[13]) {
    valPeaks[13] *= attack;
    M214.gain(0, valPeaks[13]);
  }
  if(P115.available()) rawPeaks[14] = P115.read();
  if(rawPeaks[14] > valPeaks[14]) {
    valPeaks[14] /= attack;
    M215.gain(0, valPeaks[14]);
  }
  if(rawPeaks[14] < valPeaks[14]) {
    valPeaks[14] *= attack;
    M215.gain(0, valPeaks[14]);
  }
  if(P116.available()) rawPeaks[15] = P116.read();
  if(rawPeaks[15] > valPeaks[15]) {
    valPeaks[15] /= attack;
    M216.gain(0, valPeaks[15]);
  }
  if(rawPeaks[15] < valPeaks[15]) {
    valPeaks[15] *= attack;
    M216.gain(0, valPeaks[15]);
  }
  if(P117.available()) rawPeaks[16] = P117.read();
  if(rawPeaks[16] > valPeaks[16]) {
    valPeaks[16] /= attack;
    M217.gain(0, valPeaks[16]);
  }
  if(rawPeaks[16] < valPeaks[16]) {
    valPeaks[16] *= attack;
    M217.gain(0, valPeaks[16]);
  }
  if(P118.available()) rawPeaks[17] = P118.read();
  if(rawPeaks[17] > valPeaks[17]) {
    valPeaks[17] /= attack;
    M218.gain(0, valPeaks[17]);
  }
  if(rawPeaks[17] < valPeaks[17]) {
    valPeaks[17] *= attack;
    M218.gain(0, valPeaks[17]);
  }
  if(P119.available()) rawPeaks[18] = P119.read();
  if(rawPeaks[18] > valPeaks[18]) {
    valPeaks[18] /= attack;
    M219.gain(0, valPeaks[18]);
    M220.gain(0, valPeaks[18]);
  }
  if(rawPeaks[18] < valPeaks[18]) {
    valPeaks[18] *= attack;
    M219.gain(0, valPeaks[18]);
    M220.gain(0, valPeaks[18]);
  }
}
void vocoderSibilance(float value) {
  noiseGen.amplitude(value);
}
void vocoderVoiceInputGain(float value) {
  M001.gain(0, value);
}
void vocoderInstrInputGain(float value) {
  M002.gain(0, value);
}
void vocoderVcdr2VoiceOutGain(float value) {
  M010.gain(0, value);
  M010.gain(1, value);
}
void vocoderVoiceBypssOutGain(float value) {
  M010.gain(2, value);
}
void vocoderVcdr2InstrOutGain(float value) {
  M011.gain(0, value);
  M011.gain(1, value);
}
void vocoderInstrBypssOutGain(float value) {
  M011.gain(2, value);
}
#endif
void setup() {
  usbMIDI.setHandleNoteOff(MIDInoteOff);
  usbMIDI.setHandleNoteOn(MIDInoteOn);
  usbMIDI.setHandleControlChange(MIDIcontrolChange);
  usbMIDI.setHandleProgramChange(MIDIprogramChange);
  usbMIDI.setHandlePitchChange(MIDIpitchChange);
  MIDI.setHandleNoteOff(MIDInoteOff);
  MIDI.setHandleNoteOn(MIDInoteOn);
  MIDI.setHandleControlChange(MIDIcontrolChange);
  MIDI.setHandleProgramChange(MIDIprogramChange);
  MIDI.setHandlePitchBend(MIDIpitchChange);
  MIDI.begin(MIDI_CHANNEL_OMNI);
  MC2004.begin(20, 4);
  MC2004.clear();
  Serial.begin(115200);
  pinMode(ENC1A, INPUT_PULLUP);
  pinMode(ENC1B, INPUT_PULLUP);
  pinMode(ENC1BTN, INPUT_PULLUP);
  pinMode(ENC2A, INPUT_PULLUP);
  pinMode(ENC2B, INPUT_PULLUP);
  pinMode(ENC2BTN, INPUT_PULLUP);
  pinMode(BPMBTN, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  pinMode(LEDDAT, OUTPUT);
  pinMode(LEDCLK, OUTPUT);
  pinMode(LEDLAT, OUTPUT);
  pinMode(BPMLED, OUTPUT);
  digitalWriteFast(LEDDAT, LOW);
  digitalWriteFast(LEDCLK, LOW);
  digitalWriteFast(LEDLAT, HIGH);
#ifdef USE_VOCODER
  vocoderBegin();
#endif
  digitalWriteFast(13, HIGH);
  interruptThisFunction.begin(scheduledInterruptLEDupdate, 10);
  attachInterrupt(digitalPinToInterrupt(BPMBTN), pinInterruptBpmButton, FALLING);
  attachInterrupt(digitalPinToInterrupt(ENC1A), pinInterruptCheckEncoder1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENC1B), pinInterruptCheckEncoder1, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENC1BTN), pinInterruptEncoder1Button, FALLING);
  attachInterrupt(digitalPinToInterrupt(ENC2A), pinInterruptCheckEncoder2, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENC2B), pinInterruptCheckEncoder2, CHANGE);
  attachInterrupt(digitalPinToInterrupt(ENC2BTN), pinInterruptEncoder2Button, FALLING);
}
void loop() {
  elapsedMicros ping;
  static unsigned long pingMax;
  HWMIDIupdate();
  usbMIDIupdate();
  bpmRoutine();
  checkLEDoff();
  potsRead();
  LCDupdate();
#ifdef USE_VOCODER
  vocoderUpdate();
#endif
  if(SERIALt >= 50) {
    SERIALt = 0;
    Serial.print(ping);
    Serial.print("\t\t\t");
    Serial.println(pingMax);
  }
  if(ping > pingMax) pingMax = ping;
}
void HWMIDIupdate() {
  if(MIDI.read()) {
    MIDIRX1 = 1;
    MIDIRX1t = 0;
  }
}
void usbMIDIupdate() {
  if(usbMIDI.read()) {
    MIDIRX2 = 1;
    MIDIRX2t = 0;
  }
}
void MIDInoteOff(byte channel, byte note, byte velocity) {
  if(chsw[0]) {
    usbMIDI.sendNoteOff(note, velocity, channels[0]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[1]) {
    usbMIDI.sendNoteOff(note, velocity, channels[1]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[2]) {
    usbMIDI.sendNoteOff(note, velocity, channels[2]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[3]) {
    usbMIDI.sendNoteOff(note, velocity, channels[3]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[4]) {
    usbMIDI.sendNoteOff(note, velocity, channels[4]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[5]) {
    usbMIDI.sendNoteOff(note, velocity, channels[5]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[6]) {
    usbMIDI.sendNoteOff(note, velocity, channels[6]);
    MIDITX = 1;
    MIDITXt = 0;
  }
  if(chsw[7]) {
    usbMIDI.sendNoteOff(note, velocity, channels[7]);
    MIDITX = 1;
    MIDITXt = 0;
  }
}
void MIDInoteOn(byte channel, byte note, byte velocity) {
  if(velocity == 0) {
    if(chsw[0]) {
      usbMIDI.sendNoteOff(note, 127, channels[0]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[1]) {
      usbMIDI.sendNoteOff(note, 127, channels[1]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[2]) {
      usbMIDI.sendNoteOff(note, 127, channels[2]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[3]) {
      usbMIDI.sendNoteOff(note, 127, channels[3]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[4]) {
      usbMIDI.sendNoteOff(note, 127, channels[4]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[5]) {
      usbMIDI.sendNoteOff(note, 127, channels[5]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[6]) {
      usbMIDI.sendNoteOff(note, 127, channels[6]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[7]) {
      usbMIDI.sendNoteOff(note, 127, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
  else {
    if(chsw[0]) {
      usbMIDI.sendNoteOn(note, velocity, channels[0]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[1]) {
      usbMIDI.sendNoteOn(note, velocity, channels[1]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[2]) {
      usbMIDI.sendNoteOn(note, velocity, channels[2]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[3]) {
      usbMIDI.sendNoteOn(note, velocity, channels[3]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[4]) {
      usbMIDI.sendNoteOn(note, velocity, channels[4]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[5]) {
      usbMIDI.sendNoteOn(note, velocity, channels[5]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[6]) {
      usbMIDI.sendNoteOn(note, velocity, channels[6]);
      MIDITX = 1;
      MIDITXt = 0;
    }
    if(chsw[7]) {
      usbMIDI.sendNoteOn(note, velocity, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
}
void MIDIcontrolChange(byte channel, byte control, byte value) {
  if(control == 1 || control == 64) {
    if(control == 1) {
      masterVolume = value;
      for(int i = 0; i < 8; i++) {
        potsSend(i, 1);
      }
    }
    if(control == 64) {
      if(value == 127) PEDAL = 1;
      if(value == 0) PEDAL = 0;
      usbMIDI.sendControlChange(control, value, channels[0]);
      usbMIDI.sendControlChange(control, value, channels[1]);
      usbMIDI.sendControlChange(control, value, channels[2]);
      usbMIDI.sendControlChange(control, value, channels[3]);
      usbMIDI.sendControlChange(control, value, channels[4]);
      usbMIDI.sendControlChange(control, value, channels[5]);
      usbMIDI.sendControlChange(control, value, channels[6]);
      usbMIDI.sendControlChange(control, value, channels[7]);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
}
void MIDIprogramChange(byte channel, byte program) {}
void MIDIpitchChange(byte channel, int pitch) {
  usbMIDI.sendPitchBend(pitch, channels[0]);
  usbMIDI.sendPitchBend(pitch, channels[1]);
  usbMIDI.sendPitchBend(pitch, channels[2]);
  usbMIDI.sendPitchBend(pitch, channels[3]);
  usbMIDI.sendPitchBend(pitch, channels[4]);
  usbMIDI.sendPitchBend(pitch, channels[5]);
  usbMIDI.sendPitchBend(pitch, channels[6]);
  usbMIDI.sendPitchBend(pitch, channels[7]);
  MIDITX = 1;
  MIDITXt = 0;
}
void bpmRoutine() {
  if(BPM1t > 937500) {
    BPM1t = 0;
    bpmButtonCheck = 0;
  }
  if(BPM2t >= 60000000/bpmValue) {
    BPM2t = 0;
    digitalWriteFast(BPMLED, HIGH);
    BPMLEDt = 0;
    OFFt = 0;
    thisnum = 7;
  }
}
void checkLEDoff() {
  if(MIDIRX1 && MIDIRX1t >= 50) MIDIRX1 = 0;
  if(MIDIRX2 && MIDIRX2t >= 50) MIDIRX2 = 0;
  if(MIDITX && MIDITXt >= 50) MIDITX = 0;
  if(digitalRead(BPMLED) && BPMLEDt >= 25) digitalWriteFast(BPMLED, LOW);
}
void potsRead() {
  if(POTSt >= 20) {
    POTSt = 0;
    vPot1.update();
    vPot2.update();
    vPot3.update();
    vPot4.update();
    vPot5.update();
    vPot6.update();
    vPot7.update();
    vPot8.update();
    potc[0] = vPot1.getValue() >> 3;
    potc[1] = vPot2.getValue() >> 3;
    potc[2] = vPot3.getValue() >> 3;
    potc[3] = vPot4.getValue() >> 3;
    potc[4] = vPot5.getValue() >> 3;
    potc[5] = vPot6.getValue() >> 3;
    potc[6] = vPot7.getValue() >> 3;
    potc[7] = vPot8.getValue() >> 3;
    for(int i = 0; i < 8; i++) {
      pots[i] = map(potc[i], 0, 127, 0, masterVolume);
    }
    for(int i = 0; i < 8; i++) {
      potsSend(i, 0);
      potsUpdatePWM(i, potc[i]);
    }
  }
}
void potsSend(byte channel, byte bypassChange) {
  if(potc[channel] != potp[channel] || bypassChange) {
    usbMIDI.sendControlChange(globalCC, pots[channel], channels[channel]);
    MIDITX = 1;
    MIDITXt = 0;
    potp[channel] = potc[channel];
    if(potc[channel] == 0) {
      if(globalCC == 7) {
        chsw[channel] = 0;
        usbMIDI.sendControlChange(123, 0, channels[channel]);
        MIDITX = 1;
        MIDITXt = 0;
      }
    }
    else chsw[channel] = 1;
  }
}
void potsUpdatePWM(byte num, byte value) {
  if(value == 0) {
    r[num] = 255;
    g[num] = 0;
    b[num] = 0;
  }
  if(value <= 42 && value > 0) {
    byte val1 = map(value, 1, 42, 0, 255);
    byte val2 = 255 - val1;
    r[num] = val2;
    g[num] = val1;
    b[num] = 0;
  }
  if(value <= 84 && value > 42) {
    byte val3 = map(value, 43, 84, 0, 255);
    byte val4 = 255 - val3;
    r[num] = 0;
    g[num] = val4;
    b[num] = val3;
  }
  if(value <= 127 && value > 84) {
    byte val5 = map(value, 85, 127, 0, 255);
    r[num] = val5;
    g[num] = val5;
    b[num] = 255;
  }
}
void LCDupdate() {
  LCDsettings();
  if(LCDt >= 100) {
    LCDt = 0;
    if(SETTINGSt >= 2000) {
      MC2004.setCursor(0, 0);
      MC2004.print("DIY MIDI CONTROLLER ");
      MC2004.setCursor(0, 1);
      for(int i = 0; i < 4; i++) LCDpots(i);
      MC2004.setCursor(0, 2);
      for(int i = 4; i < 8; i++) LCDpots(i);
    }
    else {
      switch(mode) {
        case 0:
          MC2004.setCursor(0, 0);
          MC2004.print("SET BPM VALUE       ");
          MC2004.setCursor(0, 1);
          for(int i = 0; i < 4; i++) LCDpots(i);
          MC2004.setCursor(0, 2);
          for(int i = 4; i < 8; i++) LCDpots(i);
        break;
        case 1:
          MC2004.setCursor(0, 0);
          MC2004.print("SET BANK            ");
          MC2004.setCursor(0, 1);
          for(int i = 0; i < 4; i++) LCDpots(i);
          MC2004.setCursor(0, 2);
          for(int i = 4; i < 8; i++) LCDpots(i);
        break;
      }
    }
    MC2004.setCursor(0, 3);
    MC2004.print("BANK");
    MC2004.print(bank);
    MC2004.print(" CC");
    if(globalCC < 10) MC2004.print("00");
    if(globalCC < 100 && globalCC >= 10) MC2004.print("0");
    MC2004.print(globalCC);
    MC2004.print("  BPM ");
    if(bpmValue < 100) MC2004.print(" ");
    MC2004.print(bpmValue);
  }
}
void LCDpots(byte num) {
  if(pots[num] < 10) MC2004.print("  ");
  if(pots[num] < 100 && pots[num] >= 10) MC2004.print(" ");
  MC2004.print(pots[num]);
  MC2004.print("  ");
}
void LCDsettings() {
  if(SETTINGSt < 2000) {
    if(encoder1rotated) {
      encoder1rotated = 0;
      SETTINGSt = 0;
      switch(mode) {
        case 0:
          if(encoder1direction) {
            bpmValue++;
            if(bpmValue > 191) bpmValue = 191;
          }
          else {
            bpmValue--;
            if(bpmValue < 64) bpmValue = 64;
          }
        break;
        case 1:
          if(encoder1direction) {
            bank++;
            if(bank > 8) bank = 8;
            byte temp = bank - 1;
            globalCC = CClist[temp];
          }
          else {
            bank--;
            if(bank < 1) bank = 1;
            byte temp = bank - 1;
            globalCC = CClist[temp];
          }
        break;
      }
    }
  }
}
void scheduledInterruptLEDupdate() {
  static byte number;
  number++;
  pwmCounter++;
  unsigned long bpmToLed = 7400000 / bpmValue;
  if(OFFt >= bpmToLed) {
    OFFt = 0;
    thisnum++;
    if(thisnum > 7) thisnum = 0;
    switch(thisnum) {
      case 0:
        OFF[0] = 1;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 1:
        OFF[0] = 0;
        OFF[1] = 1;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 2:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 1;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 3:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 1;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 4:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 1;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 5:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 1;
        OFF[6] = 0;
        OFF[7] = 0;
      break;
      case 6:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 1;
        OFF[7] = 0;
      break;
      case 7:
        OFF[0] = 0;
        OFF[1] = 0;
        OFF[2] = 0;
        OFF[3] = 0;
        OFF[4] = 0;
        OFF[5] = 0;
        OFF[6] = 0;
        OFF[7] = 1;
      break;
    }
  }
  if(number > 8) number = 1;
  switch(number) {
    case 1:
      scheduledInterruptPwmCheck(0);
      if(chsw[0]) scheduledInterruptCommand(PEDAL, br, bg, bb, 1);
      else scheduledInterruptCommand(PEDAL, OFF[0], 0, 0, 1);
    break;
    case 2:
      scheduledInterruptPwmCheck(1);
      if(chsw[1]) scheduledInterruptCommand(MIDIRX1, br, bg, bb, 2);
      else scheduledInterruptCommand(MIDIRX1, OFF[1], 0, 0, 2);
    break;
    case 3:
      scheduledInterruptPwmCheck(2);
      if(chsw[2]) scheduledInterruptCommand(MIDIRX2, br, bg, bb, 3);
      else scheduledInterruptCommand(MIDIRX2, OFF[2], 0, 0, 3);
    break;
    case 4:
      scheduledInterruptPwmCheck(3);
      if(chsw[3]) scheduledInterruptCommand(MIDITX, br, bg, bb, 4);
      else scheduledInterruptCommand(MIDITX, OFF[3], 0, 0, 4);
    break;
    case 5:
      scheduledInterruptPwmCheck(4);
      if(chsw[4]) scheduledInterruptCommand(0, br, bg, bb, 5);
      else scheduledInterruptCommand(0, OFF[4], 0, 0, 5);
    break;
    case 6:
      scheduledInterruptPwmCheck(5);
      if(chsw[5]) scheduledInterruptCommand(0, br, bg, bb, 6);
      else scheduledInterruptCommand(0, OFF[5], 0, 0, 6);
    break;
    case 7:
      scheduledInterruptPwmCheck(6);
      if(chsw[6]) scheduledInterruptCommand(0, br, bg, bb, 7);
      else scheduledInterruptCommand(0, OFF[6], 0, 0, 7);
    break;
    case 8:
      scheduledInterruptPwmCheck(7);
      if(chsw[7]) scheduledInterruptCommand(0, br, bg, bb, 8);
      else scheduledInterruptCommand(0, OFF[7], 0, 0, 8);
    break;
  }
}
void scheduledInterruptCommand(byte c1, byte c2, byte c3, byte c4, byte number) {
  switch(number) {
    case 1:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 2:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 3:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 4:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 5:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 6:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
    case 7:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
      scheduledInterruptSendLow();
    break;
    case 8:
      scheduledInterruptParse(c1, c2, c3, c4);
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendHigh();
    break;
    default:
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
      scheduledInterruptSendLow();
    break;
  }
  scheduledInterruptSendLatch();
}
void scheduledInterruptParse(byte c1, byte c2, byte c3, byte c4) {
  if(c4) scheduledInterruptSendLow();
  else scheduledInterruptSendHigh();
  if(c3) scheduledInterruptSendLow();
  else scheduledInterruptSendHigh();
  if(c2) scheduledInterruptSendLow();
  else scheduledInterruptSendHigh();
  if(c1) scheduledInterruptSendLow();
  else scheduledInterruptSendHigh();
}
void scheduledInterruptSendHigh() {
  digitalWriteFast(LEDDAT, HIGH);
  scheduledInterruptEpicDelay();
  digitalWriteFast(LEDCLK, HIGH);
  scheduledInterruptEpicDelay();
  digitalWriteFast(LEDCLK, LOW);
  scheduledInterruptEpicDelay();
}
void scheduledInterruptSendLow() {
  digitalWriteFast(LEDDAT, LOW);
  scheduledInterruptEpicDelay();
  digitalWriteFast(LEDCLK, HIGH);
  scheduledInterruptEpicDelay();
  digitalWriteFast(LEDCLK, LOW);
  scheduledInterruptEpicDelay();
}
void scheduledInterruptSendLatch() {
  digitalWriteFast(LEDLAT, LOW);
  scheduledInterruptEpicDelay();
  digitalWriteFast(LEDLAT, HIGH);
  scheduledInterruptEpicDelay();
}
void scheduledInterruptEpicDelay() {
  digitalWrite(13, HIGH);
}
void scheduledInterruptPwmCheck(byte num) {
  if(r[num] > pwmCounter) br = 1;
  else br = 0;
  if(g[num] > pwmCounter) bg = 1;
  else bg = 0;
  if(b[num] > pwmCounter) bb = 1;
  else bb = 0;
}
void pinInterruptEncoder1Button() {
  if(ENC1BTNt >= 250) {
    ENC1BTNt = 0;
    SETTINGSt = 0;
    mode++;
    if(mode > 1) mode = 0;
  }
}
void pinInterruptEncoder2Button() {
  if(ENC2BTNt >= 250) {
    ENC2BTNt = 0;
  }
}
void pinInterruptBpmButton() {
  if(BPMBTNt >= 250) {
    BPMBTNt = 0;
    if(!bpmButtonCheck) {
      BPM1t = 0;
      BPM2t = 0;
      bpmButtonCheck = 1;
      usbMIDI.sendControlChange(14, BPM, 16);
      MIDITX = 1;
      MIDITXt = 0;
    }
    else {
      bpmValue = 60000000/BPM1t;
      BPM1t = 0;
      BPM2t = 60000000/bpmValue;
      if(bpmValue > 191) bpmValue = 191;
      if(bpmValue < 64) bpmValue = 64;
      BPM = bpmValue - 64;
      usbMIDI.sendControlChange(14, BPM, 16);
      MIDITX = 1;
      MIDITXt = 0;
    }
  }
}
void pinInterruptCheckEncoder1() {
  static byte pos;
  if(digitalRead(ENC1A) && digitalRead(ENC1B)) {
    pos = 0;
  }
  if(digitalRead(ENC1A) && !digitalRead(ENC1B)) {
    if(pos == 2) encoder1rotated = 1;
    pos = 1;
  }
  if(!digitalRead(ENC1A) && !digitalRead(ENC1B)) {
    if(pos == 3) encoder1direction = 0;
    if(pos == 1) encoder1direction = 1;
    pos = 2;
  }
  if(!digitalRead(ENC1A) && digitalRead(ENC1B)) {
    if(pos == 2) encoder1rotated = 1;
    pos = 3;
  }
}
void pinInterruptCheckEncoder2() {
  static byte pos;
  if(digitalRead(ENC2A) && digitalRead(ENC2B)) {
    pos = 0;
  }
  if(digitalRead(ENC2A) && !digitalRead(ENC2B)) {
    if(pos == 2) encoder2rotated = 1;
    pos = 1;
  }
  if(!digitalRead(ENC2A) && !digitalRead(ENC2B)) {
    if(pos == 3) encoder2direction = 0;
    if(pos == 1) encoder2direction = 1;
    pos = 2;
  }
  if(!digitalRead(ENC2A) && digitalRead(ENC2B)) {
    if(pos == 2) encoder2rotated = 1;
    pos = 3;
  }
}

Any response or suggestions (to me) will be greatly appreciated.
Daniel Revalo a.k.a. Revalogics
 
UPDATE: I tested this code on our Church keyboard and moving pitchbend wheel once (and letting it go to its midpoint) creates unforgettable results: I'm playing a whole note lower than the rest of the band.
MIDI library throws pitchbend values from 0 to 16383 but usbMIDI accepts values from -8192 to 8191 (using int) so we need to subtract 8192 from pitch value.
Code:
void MIDIpitchChange(byte channel, int pitch) {
  pitch -= 8192;
  usbMIDI.sendPitchBend(pitch, channels[0]);
  usbMIDI.sendPitchBend(pitch, channels[1]);
  usbMIDI.sendPitchBend(pitch, channels[2]);
  usbMIDI.sendPitchBend(pitch, channels[3]);
  usbMIDI.sendPitchBend(pitch, channels[4]);
  usbMIDI.sendPitchBend(pitch, channels[5]);
  usbMIDI.sendPitchBend(pitch, channels[6]);
  usbMIDI.sendPitchBend(pitch, channels[7]);
  MIDITX = 1;
  MIDITXt = 0;
}
 
Last edited:
Back
Top