In Need of sample 4067 code

Greetings i am working on building a midi controller using an Arduino Mega 2560 and a few 4067b Multiplexers

I've searched around online for a few days and have only seen sample coding for reading all of the mux channels in a single function.

I'd like to control the Mux switching with buttons or a rotary encoder eventually.

I'm thinking that i should be able to put the reading of each channel into an individual function and call that function with a button press to access the specific channel...

Problem is i'm not very familiar with the mux coding

can any one provide me with a simple code using 1 4067 with 4 pots and 4 buttons?

each button accessing an individual pot through a mux channel..............

The 4067 acts as a rotary switch connecting the common (pin1) to one of the I0..I15 depending on the levels on the S0..S3 and the -EN pins. When the -En is high the common is disconnected. When the - EN is low the combination on S0..S3 selects which of I0..I15 is connected to common.

//designate first pin to S0 on the 4067, consecutive pins will go to S1..S3
const int S0 = 8;  
const int ENABLE = 7;//remember enable is active low


void setup(){
  //make pins connected to 4067 outputs
  for(int i = 0; i<4; i++) pinMode(i+S0, OUTPUT);
  pinMode(ENABLE, OUTPUT);
  digitalWrite(ENABLE, 0);//activate enable
}//setup() 

void loop(){}//loop()

void select4067(byte sel){//controls the select pins on 4067
  for(int i = 0; i<4; i++){
    digitalWrite(i+S0, (sel&1));//output bit 0 of sel to current pin
    sel>>=1;//shift next bit into bit 0
  }//for(i)
}//select4067()

Ok i understand the operation and function of the 4067 physically I'm just not getting the part where a button would call a function to select a mux input in the code

I don't need an actual code to copy and paste- im moreso looking for a sample to help me to understand and apply the mux functions

i'm sure there are different ways to implement the functions but it just doesnt make sense to me....

for example why is "i" the specific variable that is always used in reference to the in/out pins?

and why cant I just declare a function and call on it when needed ....

ex...

function readPot1 (
activate mux pin 1)

if digitalPin 1 is High (
readPot1)

excuse my attempt at the example- it isnt in the proper language but i'm hoping someone can interpret this incredibly simplified idea and help me to put it into actual code

heres a better idea using C++ of what i want to do
I'd like to know if it will work- and if the above code is a shorter version of the same thing- and if so then would this use more memory than the above code

int sa0 = 2;
int sa1 = 3;
int sa2 = 4;
int sa3 = 5; //mux control pins

int ch1_btn = 6;
int ch2_btn = 7;
int ch3_btn = 8;


int mux1_sig = A0;  //Mux in "SIG" pin



void setup()
{
  pinMode(s0, OUTPUT); 
  pinMode(s1, OUTPUT); 
  pinMode(s2, OUTPUT); 
  pinMode(s3, OUTPUT); 

  pinMode(ch1_btn, INPUT); 
  pinMode(ch2_btn, INPUT);
  pinMode(ch3_btn, INPUT);

  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
}
void readpot1 ()
{
  digitalWrite(s0, LOW);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
}

void readpot2 ()
{
  digitalWrite(s0, HIGH);
  digitalWrite(s1, LOW);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
}

void readpot3 ()
{
  digitalWrite(s0, LOW);
  digitalWrite(s1, HIGH);
  digitalWrite(s2, LOW);
  digitalWrite(s3, LOW);
}

void loop(){
if (ch1_btn == HIGH) {         
    readpot1(); 
analogRead(mux1_sig);
  } 

if (ch2_btn == HIGH) {         
    readpot2(); 
analogRead(mux1_sig); 
  } 

if (ch3_btn == HIGH) {         
    readpot3(); 
analogRead(mux1_sig); 
  } 
}

You have to use digitalRead() to get the digital value from a pin.
This is the way i would have done it:

//designate first pin to S0 on the 4067, consecutive pins will go to S1..S3
const int S0 = 2;  
const int ENABLE = 7;//remember enable is active low
const int MUXPIN = A0;
const int ch1_Btn = 6;//first button pin

byte button;//no of button pressed
int analogMuxVal;//holds value read from mux

void setup(){
  //make pins connected to 4067 select outputs
  for(int i = 0; i<4; i++) pinMode(i+S0, OUTPUT);
  pinMode(ENABLE, OUTPUT);
  digitalWrite(ENABLE, 0);//activate enable
}//setup() 

void loop(){
  button = readBtn();
  if(button) analogMuxVal = readMuxAnalog(button);
}//loop()

void select4067(byte sel){//controls the select pins on 4067
  for(int i = 0; i<4; i++){
    digitalWrite(i+S0, (sel&1));//output bit 0 of sel to current pin
    sel>>=1;//shift next bit into bit 0
  }//for(i)
}//select4067()

int readMuxAnalog(byte channel){
  select4067(channel);
  return analogRead(MUXPIN);
}//readMuxAnalog()

byte readBtn(){
  for(int i=0; i<3; i++) if(digitalRead(ch1_Btn+i)) return i+1;//check all buttons and return first found
  return 0;//no button pressed
}//readButton