Rotary Encoder - MIDI Serial

I'm still in the fight !!!
I was able to integrate in a sketch the functions with multiplexers for both buttons and faders, including an encoder. Now the problems are 2:
1_ so far I have 2 multiplexers 74hc4067 for buttons, 1 for faders and 1 encoder and it tells me 99% memory. Is there a way to "compress" the code? since I still need to add several MUX for more buttons, and another for the encoders.

2_ How can I put 4 encoders in a multiplexer.
I attach a code that I made separately to test the encoders in a MUX, to see if you can guide me.

Once again, thank you very much!!!

#include <Encoder.h>
#include "Control_Surface.h" 
HairlessMIDI_Interface midi;

CD74HC4067 mux = {  //MUX1
  11,           // Digital input pin
  {2, 3, 4, 5} // Address pins S0, S1, S2, S3
};

CCRotaryEncoder enc[] = {
 { mux.pin(0, 1), {MIDI_CC::Damper_Pedal, CHANNEL_1} }, 
};
void setup() {
  RelativeCCSender::setMode(relativeCCmode::KORG_KONTROL_INC_DEC_1);
  Control_Surface.begin();  
  // put your setup code here, to run once:

}

void loop() {
  Control_Surface.loop(); 
  // put your main code here, to run repeatedly:

}

Memory usage is the main disadvantage of using the Control Surface library over a hand-written solution. Usually, I recommend using a board that has more RAM.

Some things you can do to lower the memory footprint:

You cannot use multiplexers to read encoders, see for more details #191.

From what I was reading, an ARDUINO MEGA 2560 would be my solution, is that so?
the Arduino Mega would solve the problems I have:

1_ More memory for the program.

2_ More pins to be able to use my 8 digital MUX for buttons, 4 rotary encoders, and 16 faders.

3_ the Arduino Mega can be used as USB MIDI.

Am I correct in the 3 statements?

If I am correct, this is what they offer me, is it correct?

Arduino Mega 2560 R3 16au + USB CABLE

-ATmega2560 Microprocessor
-Power supply (recommended) 7-12V
-Integrates regulation and stabilization of + 5Vdc
-54 lines of Digital Inputs / Outputs (14 of them can be used as PWM outputs)
-16 Analog Inputs >>>> these are 16 more digital I/O that also have analog input function
-Maximum direct OUTPUT current for inputs: 40 mA (20mA max recommended)
-Power output at 3.3V with 50 mA >>> 150mA is spec'ed for the regulator, 50mA is from when 3.3V came from FT232 chip (USB adapter).
-256Kb program memory (bootloader occupies 8Kb) >>> current bootloader I think only takes up 4 kbytes
-8Kb SRAM memory for data and program variables
EEPROM memory for data and non-volatile variables
-16MHz work clock speed
-Reduced dimensions of 100 x 50 mm

Captura.JPG

Captura.JPG

I modded the capabilities some.

I did not understand the answer. What do you mean?

PieterP:
Memory usage is the main disadvantage of using the Control Surface library over a hand-written solution. Usually, I recommend using a board that has more RAM.

Some things you can do to lower the memory footprint:

You cannot use multiplexers to read encoders, see for more details #191.

Memory usage is From what I was reading, an ARDUINO MEGA 2560 would be my solution, is that so?
the Arduino Mega would solve the problems I have:

1_ More memory for the program.

2_ More pins to be able to use my 8 digital MUX for buttons, 4 rotary encoders, and 16 faders.

3_ the Arduino Mega can be used as USB MIDI.

Am I correct in the 3 statements?

If I am correct, this is what they offer me, is it correct?

Arduino Mega 2560 R3 16au + USB CABLE

-ATmega2560 Microprocessor
-Power supply (recommended) 7-12V
-Integrates regulation and stabilization of + 5Vdc
-54 lines of Digital Inputs / Outputs (14 of them can be used as PWM outputs)
-16 Analog Inputs >>>> these are 16 more digital I / O that also have analog input function
-Maximum direct OUTPUT current for inputs: 40 mA (20mA max recommended)
-Power output at 3.3V with 50 mA >>> 150mA is spec'ed for the regulator, 50mA is from when 3.3V came from FT232 chip (USB adapter).
-256Kb program memory (bootloader occupies 8Kb) >>> current bootloader I think only takes up 4 kbytes
-8Kb SRAM memory for data and program variables
EEPROM memory for data and non-volatile variables
-16MHz work clock speed
-Reduced dimensions of 100 x 50 mm

No, the MEGA doesn't support MIDI over USB. It's hardly an upgrade from an UNO, it's still a slow 8-bit AVR, albeit with a little more memory and pins.

I usually recommend a Teensy for more serious MIDI controllers. They are cheaper than the MEGA, and are much more powerful (faster CPU, much more RAM and flash, faster IO, native USB support, etc.). They don't have quite as many pins as the MEGA, but since you're already using multiplexers, that shouldn't be an issue. ARM-based Teensies also have interrupt capabilities on all pins, which is an advantage if you want to use multiple encoders. Polling multiple encoders might not be feasible on a MEGA if you have to read that many buttons and faders as well.

Also see this page: MIDI over USB

PieterP:
I usually recommend a Teensy for more serious MIDI controllers.

It is incredible, which in arduino mega occupied 88% in teensy and 3.6 only occupies 4% of memory and luckily when verifying I did not accuse any errors. Unfortunately teensy don't work in Argentina, but I can get it imported. In README.md it only names teensy 3.2, Teensy 3.6 is also compatible with the control surface library? (The question is because there is almost no price difference)
Once again, thank you very much!!!

I've personally tested the library with Teensy 3.2 and 4.0. Supporting code for other Teensies is included as well, and users have reported it working with Teensy 3.5 and 3.6 (you can search for "Teensy" in the GitHub issues, for instance).

I'm back :(. I already have teensy 3.2 in my hands, but when testing, I'm having problems. My code made in arduino, now passed to teensyno, when verified is OK. I've loaded it to my teensy with a multiplexer with 16 buttons connected, and what it does is trigger the buttons alone without pressing them. Can I be wrong with my teensy's settings? The connection, I think is fine:

Pin 2 - S0
Pin 3 - S1
Pin 4 - S2
Pin 5 - S3
Pin 6 - Digital Input (Sig multiplexor)

#include <Encoder.h>
#include "Control_Surface.h" 
USBMIDI_Interface midi;

CD74HC4067 mux1 = {  //MUX1
  6,           // Digital input pin
  {2, 3, 4, 5} // Address pins S0, S1, S2, S3
};

CD74HC4067 muxF = { //MUX FADERS
  A0,           // Digital input pin
  {2, 3, 4, 5} // Address pins S0, S1, S2, S3
};
CCRotaryEncoder enc1 = {
  {14, 15},       // pins
  MCU::V_POT_1, // 
   1,            // optional multiplier if the control isn't fast enough
};
CCRotaryEncoder enc2 = {
  {16, 17},       // pins
  MCU::V_POT_2, // 
   1,            // optional multiplier if the control isn't fast enough
};
CCRotaryEncoder enc3 = {
  {18, 19},       // pins
  MCU::V_POT_3, // 
   1,            // optional multiplier if the control isn't fast enough
};
CCRotaryEncoder enc4 = {
  {20, 21},       // pins
  MCU::V_POT_4, // 
   1,            // optional multiplier if the control isn't fast enough
};
CCButton buttons[] = {
 //MUX1
    { mux1.pin(0), {MIDI_CC::Damper_Pedal, CHANNEL_1} },
    { mux1.pin(1), {MIDI_CC::Damper_Pedal, CHANNEL_2} },
    { mux1.pin(2), {MIDI_CC::Damper_Pedal, CHANNEL_3} },
    { mux1.pin(3), {MIDI_CC::Damper_Pedal, CHANNEL_4} },
    { mux1.pin(4), {MIDI_CC::Damper_Pedal, CHANNEL_5} },
    { mux1.pin(5), {MIDI_CC::Damper_Pedal, CHANNEL_6} },
    { mux1.pin(6), {MIDI_CC::Damper_Pedal, CHANNEL_7} },
    { mux1.pin(7), {MIDI_CC::Damper_Pedal, CHANNEL_8} },
    { mux1.pin(8), {MIDI_CC::Damper_Pedal, CHANNEL_9} },
    { mux1.pin(9), {MIDI_CC::Damper_Pedal, CHANNEL_10} },
    { mux1.pin(10), {MIDI_CC::Damper_Pedal, CHANNEL_11} },
    { mux1.pin(11), {MIDI_CC::Damper_Pedal, CHANNEL_12} },
    { mux1.pin(12), {MIDI_CC::Damper_Pedal, CHANNEL_13} },
    { mux1.pin(13), {MIDI_CC::Damper_Pedal, CHANNEL_14} },
    { mux1.pin(14), {MIDI_CC::Damper_Pedal, CHANNEL_15} },
    { mux1.pin(15), {MIDI_CC::Damper_Pedal, CHANNEL_16} },

CCPotentiometer potentiometer[] = {
    { muxF.pin(0), {MIDI_CC::Channel_Volume, CHANNEL_1} },
    { muxF.pin(1), {MIDI_CC::Channel_Volume, CHANNEL_2} },
    { muxF.pin(2), {MIDI_CC::Channel_Volume, CHANNEL_3} },
    { muxF.pin(3), {MIDI_CC::Channel_Volume, CHANNEL_4} },
    { muxF.pin(4), {MIDI_CC::Channel_Volume, CHANNEL_5} },
    { muxF.pin(5), {MIDI_CC::Channel_Volume, CHANNEL_6} },
    { muxF.pin(6), {MIDI_CC::Channel_Volume, CHANNEL_7} },
    { muxF.pin(7), {MIDI_CC::Channel_Volume, CHANNEL_8} },
    { muxF.pin(8), {MIDI_CC::Channel_Volume, CHANNEL_9} },
    { muxF.pin(9), {MIDI_CC::Channel_Volume, CHANNEL_10} },
    { muxF.pin(10), {MIDI_CC::Channel_Volume, CHANNEL_11} },
    { muxF.pin(11), {MIDI_CC::Channel_Volume, CHANNEL_12} },
    { muxF.pin(12), {MIDI_CC::Channel_Volume, CHANNEL_13} },
    { muxF.pin(13), {MIDI_CC::Channel_Volume, CHANNEL_14} },
    { muxF.pin(14), {MIDI_CC::Channel_Volume, CHANNEL_15} },
    { muxF.pin(15), {MIDI_CC::Channel_Volume, CHANNEL_16} },
  
};

void setup() {
  RelativeCCSender::setMode(relativeCCmode::KORG_KONTROL_INC_DEC_1);
  Control_Surface.begin();  
}

void loop() {
Control_Surface.loop(); 
}

buttons.JPG

I don't have much time to look into it right now, but you have some pin conflicts. Pin A0 and 14 are the same pin, you cannot use it for multiple different things.

The MIDI monitor data you posted only shows potentiometer data, not button presses.