How to make Multiplexer send other midi-messages

Hi there, I build a Midi-Controller and found a bug in the code I used. Here is the full code:
https://github.com/FuzzyWobble/DIY-Controller/blob/master/ControllerCode_FuzzyWobble/ControllerCode_FuzzyWobble.pde

The controller uses a Teensy2++ and a CD4067BE Multiplexer to provide more analog-ins.
I am using 7 of the Teensys analog-ins (A0-A6) and all 16 analog-ins (0-15) of the Multiplexer. I have pots attached to all of them.

So the bug is, that all Teensy analog-ins (A0-A6) send the same midi-messages like the MUX analog-ins 0-6.

I guess the problem has to be solved somewhere below line 153:

//ANALOG IN
int s0 = 14; //control pin A
int s1 = 15; //control pin B
int s2 = 16; //control pin C
int s3 = 17; //control pin D
int SIG_pin = 45; //read pin
int analogInsPrev[16]; //array to hold previously read analog values - set all to zero for now
int tempAnalogIn = 0; //array to hold previously read analog values 
int tempAnalogInMap = 0;
int analogThreshold = 4; //threshold
int controlPin[] = {s0, s1, s2, s3}; //set contol pins in array
//control array 
int muxChannel[16][4]={ 
  {0,0,0,0}, //channel 0
  {1,0,0,0}, //channel 1
  {0,1,0,0}, //channel 2
  {1,1,0,0}, //channel 3
  {0,0,1,0}, //channel 4
  {1,0,1,0}, //channel 5
  {0,1,1,0}, //channel 6
  {1,1,1,0}, //channel 7
  {0,0,0,1}, //channel 8
  {1,0,0,1}, //channel 9
  {0,1,0,1}, //channel 10
  {1,1,0,1}, //channel 11
  {0,0,1,1}, //channel 12
  {1,0,1,1}, //channel 13
  {0,1,1,1}, //channel 14
  {1,1,1,1}  //channel 15
};
//function to read mux
int readMux(int channel){  
  //loop through the four control pins
  for(int i = 0; i < 4; i ++){ 
    //turn on/off the appropriate control pins according to what channel we are trying to read 
    digitalWrite(controlPin[i], muxChannel[channel][i]); 
  }
  //read the value of the pin
  int val = analogRead(SIG_pin); 
  //return the value
  return val; 
}
int analogPinsTeensy[8] = {38,38,40,41,42,43,44,45};
int analogInsPrevTeensy[8]; //array to hold previously read analog values 
int tempAnalogInTeensy = 0; 
int tempAnalogInMapTeensy = 0;
int analogThresholdTeensy = 4; //threshold

As I am not too familiar with arduino, I just do not know how to solve this.

I already tried a „hack“ someone else proposed. In Line 195 he told the Teensy to have 24 instead of 8 Pins and filled up the first 16 digits with dummy-values like this:

int analogPinsTeensy[24] = {100,102,103,104,105,106,107,108,109,110,111,112,113,114,115,38,39,40,41,42,43,44,45};

The Idea was, that the Teensy would skip the first 16 Values which could be then used by the Multiplexer, but it did not work out for me and I would prefer a cleaner solution anyway. So I would be really happy if someone can help me with this issue!

How can I make the Multiplexer to send other midi-messages than the Teensy?

I’m afraid I don’t have the time to plough through hundreds of lines of code like that, but if you use the Control Surface library I’m working on, you can do something like this with much simpler code.

For example:

[color=#434f54]// Include the library[/color]
[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color]h[color=#434f54]>[/color]
 
[color=#434f54]// Instantiate a MIDI Interface to use[/color]
[b][color=#d35400]USBMIDI_Interface[/color][/b] [color=#00979c]midi[/color];
 
[color=#434f54]// Instantiate an analog multiplexer[/color]
[b][color=#d35400]CD74HC4067[/color][/b] mux [color=#434f54]=[/color] {
  [color=#00979c]A7[/color][color=#434f54],[/color]              [color=#434f54]// Analog input pin (SIG)[/color]
  {14[color=#434f54],[/color] 15[color=#434f54],[/color] 16[color=#434f54],[/color] 17} [color=#434f54]// Address pins (S0, S1, S2, S3)[/color]
};
 
[color=#434f54]// Create an array of potentiometers that send out[/color]
[color=#434f54]// MIDI Control Change messages when you turn the[/color]
[color=#434f54]// potentiometers connected to the 16 input pins of[/color]
[color=#434f54]// the multiplexer[/color]
[b][color=#d35400]CCPotentiometer[/color][/b] muxPots[] [color=#434f54]=[/color] {
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](0)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_1[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](1)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_2[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](2)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_3[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](3)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_4[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](4)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_5[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](5)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_6[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](6)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_7[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](7)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_8[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](8[nobbc])[/nobbc][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_9[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](9)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_10[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](10)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_11[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](11)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_12[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](12)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_13[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](13)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_14[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](14)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_15[/color]}}[color=#434f54],[/color]
  {mux[color=#434f54].[/color][color=#d35400]pin[/color](15)[color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Channel_Volume[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_16[/color]}}[color=#434f54],[/color]
};

[b][color=#d35400]CCPotentiometer[/color][/b] teensyPots[] [color=#434f54]=[/color] {
  {[color=#00979c]A0[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_1[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A1[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_2[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A2[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_3[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A3[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_4[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A4[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_5[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A5[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_6[/color]}}[color=#434f54],[/color]
  {[color=#00979c]A6[/color][color=#434f54],[/color] {[b][color=#d35400]MIDI_CC[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Pan[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_7[/color]}}[color=#434f54],[/color]
};
 
[color=#434f54]// Initialize the Control Surface[/color]
[color=#00979c]void[/color] [color=#5e6d03]setup[/color]() {
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color]();
}
 
[color=#434f54]// Update the Control Surface[/color]
[color=#00979c]void[/color] [color=#5e6d03]loop[/color]() {
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#5e6d03]loop[/color]();
}

More information: Control Surface: Getting Started

Pieter

Hi Pieter, thanks for the tip. Well, I got the feeling if I start rewriting the code using Control_Surface I would have to change several other parts of the code aswell and I am not sure if I have enough experience to do that.

Yes, the code is 7 years old and yes, there are better&advanced methods now. In the long run I have to make myself familiar with Control_Surface, I see that now. But I also got the feeling, that changing a single line of code might solve the problem here.

There must be a simple way to tell the multiplexer to just skip the first 8 Values so they can be used by the teensy. Maybe in Line 99 or somewhere between line 153 to 200?

I don't understand the hack you're describing. What are these dummy numbers? Where are they used? There are two different kinds of analog input, right? The normal ones A0-A6, and then 16 multiplexed ones through A7. In that case, it would make sense to treat them separately, I wouldn't put them in the same array.

My advice would be to isolate the A0-A6 part and get it working in a separate sketch, nothing else, just 7 pots sending MIDI output. Try to understand what each line of code is doing before moving on.

Then do the same for the multiplexed version. Once that's working, you can combine the two pieces of code.

Right now it's very hard for someone who's not familiar with the fuzzywobble code to understand what's going on, let alone what's going wrong.

In that case, it would make sense to treat them separately, I wouldn't put them in the same array.

That was just the Inspiration I needed - PROBLEM SOLVED

I just set up a seperate channelNumber for the mux to make it send on a different midi-channel like the teensy and called it channelNumberMux

I added in line 25:

int channelNumberMux = 9; // different number like channelNumber

And changed line 403 to:

usbMIDI.sendControlChange(i,tempAnalogInMap,channelNumberMux);

I knew the solution would be simple!

Thank you very much for your advice & patience, PieterP!