MIDI - save mux state into Program Change #

Hello,
my first project! i'm using Mega 2560:

  • divided into 8 section (A to G )
  • each section is 1 X cd4067 + 1 UP button + 1 DN button ( button to go up or down in the cd4067 channel)
  • 1 save button

So far:

  • i can make each cd4067 go up and down.
  • I'm using 2 section for now to simplify,
  • code gathering from everywhere...
  • i've found the way to have a start position

I want to be able to save the state (channel) of each cd4067 into each Program Change #(from 0 to 127)
So a Program Change # should be able to have 8 X (4 pin state) and make it change on receiving MIDI message....

I've found the MIDI library: #include <MIDI.h>
But not sure how to totally use it....

code sor far:

#include <Bounce2.h>

const int channel[] = { 22, 23, 24, 25};
const int buttonPinUp = 26;    
const int buttonPinDn = 29;
int buttonStateUp = 0; 
int lastButtonStateUp = LOW; 
int buttonStateDn = 0;
int lastButtonStateDn = LOW; 
int thisChannel = 8;

const int channelB[] = { 36, 37, 38, 39};
const int buttonPinUpB = 40;    
const int buttonPinDnB = 41;
int buttonStateUpB = 0; 
int lastButtonStateUpB = LOW; 
int buttonStateDnB = 0;
int lastButtonStateDnB = LOW; 
int thisChannelB = 1;

// Instantiate a Bounce object
Bounce debouncer1 = Bounce(); 
// Instantiate another Bounce object
Bounce debouncer2 = Bounce();

// Instantiate a Bounce object
Bounce debouncer1B = Bounce(); 
// Instantiate another Bounce object
Bounce debouncer2B = Bounce();

void setup() {
   
  // initialize the pushbutton pin as an input:
    pinMode(buttonPinUp, INPUT);
      debouncer1.attach(buttonPinUp);
      debouncer1.interval(5); // interval in ms
    pinMode(buttonPinDn, INPUT);
      debouncer2.attach(buttonPinDn);
      debouncer2.interval(5); // interval in ms
  // set up all pins as output:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
  // initialize the pushbutton pin as an input:
    pinMode(buttonPinUpB, INPUT);
      debouncer1B.attach(buttonPinUpB);
      debouncer1B.interval(5); // interval in ms
    pinMode(buttonPinDnB, INPUT);
      debouncer2B.attach(buttonPinDnB);
      debouncer2B.interval(5); // interval in ms
  // set up all pins as output:
  for (int thisPinB = 2; thisPinB < 7; thisPinB++) {
    pinMode(thisPinB, OUTPUT);
  }
}

  
void loop() {
  muxWrite(thisChannel); //start position
  muxWriteB(thisChannelB); //start position
  debouncer1.update();
  debouncer2.update();
    // Get the updated value :
  int value1 = debouncer1.read();
  int value2 = debouncer2.read();
   // read the state of the pushbutton value:
  buttonStateUp = digitalRead(buttonPinUp);
// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateUp == HIGH && thisChannel < 15 && value1 == LOW) {
    thisChannel++; 
    muxWrite(thisChannel);
      } 
// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  buttonStateDn = digitalRead(buttonPinDn);
  if (buttonStateDn == HIGH && thisChannel > 0 && value2 == LOW) {
    thisChannel--; 
    muxWrite(thisChannel);
      } 
 debouncer1B.update();
 debouncer2B.update();
    // Get the updated value :
  int value1B = debouncer1B.read();
  int value2B = debouncer2B.read();
   // read the state of the pushbutton value:
  buttonStateUpB = digitalRead(buttonPinUpB);
// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonStateUpB == HIGH && thisChannelB < 15 && value1B == LOW) {
    thisChannelB++; 
    muxWriteB(thisChannelB);
      } 
// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  buttonStateDnB = digitalRead(buttonPinDnB);
  if (buttonStateDnB == HIGH && thisChannelB > 0 && value2B == LOW) {
    thisChannelB--; 
    muxWriteB(thisChannelB);
      } 
      delay(50);
}

void muxWrite(int whichChannel) {
  // iterate over the number of pins you're using:
  for (int thisPin = 0; thisPin < 4; thisPin++) {
    // calculate the state of this pin based on 
    // its bit value in whichChannel:
    int pinState = bitRead(whichChannel, thisPin);
    // turn the pin on or off:
    digitalWrite(channel[thisPin],pinState);
  }
}

void muxWriteB(int whichChannelB) {
  // iterate over the number of pins you're using:
  for (int thisPinB = 0; thisPinB < 4; thisPinB++) {
    // calculate the state of this pin based on 
    // its bit value in whichChannel:
    int pinStateB = bitRead(whichChannelB, thisPinB);
    // turn the pin on or off:
    digitalWrite(channelB[thisPinB],pinStateB);
  }
}

any help is very appreciated!

regards,

Bent

mux.txt (3.86 KB)

my first thought is to create a switch case.
something like:

  • get the PC#

  • switch (PC#):

  • case 0 { sectionA, section B ....}
    break

  • case 1 { section....}
    break
    .....repeat until:

  • case 127 { section A, ....}
    break

long but can probably do the job....

I would forget about debounce, until you know you need them. At this stage it just over complicates the code and with other stuff going on you seldom need it anyway.

Any long repetitive code can be made much shorter by using loops and arrays there is no need for 128 switch cases.

However it is not altogether clear to me as to what you are trying to do.

Have a read of this Tutorial Arrays

Thanks!!!! It's really a good start to reduce the programmation!!!
I'll redo my homework, and comeback soon! :slight_smile:

ALRIGHT, I'VE MANAGE THE CODE...
SO EACH CHANNEL IS GOING UP OR DOWN DEPENDING ON THE SWITCH I PUSH...
BUT THE SAVE IS NOT WORKING....AND THE CHANEL CHANGE WHEN RECEIVING PROGRAM CHANGE....

#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


int address0 [128] = {};
int address1 [128] = {};
int address2 [128] = {};
int address3 [128] = {};
int address4 [128] = {};
int address5 [128] = {};
int address6 [128] = {};
int address7 [128] = {};

const int buttonPinDn [8] = { 2, 4, 6, 8, 10, 12, 16, 18}; // ARDUINO PIN FOR EACH BUTTON UP,  2 = SECTION 0-UP, 4 = SECTION 1-UP, ETC...
const int buttonPinUp [8] = { 3, 5, 7, 9, 11, 15, 17, 19};  // ARDUINO PIN FOR EACH BUTTON DOWN  3 = SECTION 0-DOWN, ETC..
const int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp0 = 0; 
int buttonStateDn0 = 0;
int thisChannel0;
int channel0 [4] = {22, 23, 24, 25};
int buttonStateUp1 = 0; 
int buttonStateDn1 = 0;
int thisChannel1 = 0;
int channel1 [4] = {26, 27, 28, 29};
int buttonStateUp2 = 0; 
int buttonStateDn2 = 0;
int thisChannel2;
int channel2 [4] = {30, 31, 32, 33}; 
int buttonStateUp3 = 0; 
int buttonStateDn3 = 0;
int thisChannel3;
int channel3 [4] = {34, 35, 36, 37}; 
int buttonStateUp4 = 0; 
int buttonStateDn4 = 0;
int thisChannel4;
int channel4 [4] = {38, 39, 40, 41}; 
int buttonStateUp5 = 0; 
int buttonStateDn5 = 0;
int thisChannel5;
int channel5 [4] = {42, 43, 44, 45}; 
int buttonStateUp6 = 0; 
int buttonStateDn6 = 0;
int thisChannel6;
int channel6 [4] = {46, 47, 48, 49}; 
int buttonStateUp7 = 0; 
int buttonStateDn7 = 0;
int thisChannel7;
int channel7 [4] = {50, 51, 52, 53}; 

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
  
  for( int i=2; i<13; i++) 
    pinMode(i, INPUT);
  for( int y=14; y<19; y++) 
    pinMode(y, INPUT);
  for( int x=22; x<54; x++) 
    pinMode(x, OUTPUT);

}



void muxWrite0(int whichChannel0) {for (int thisPin0 = 0; thisPin0 < 4; thisPin0++) {int pinState0 = bitRead(whichChannel0, thisPin0);digitalWrite(channel0[thisPin0],pinState0);}}
void muxWrite1(int whichChannel1) {for (int thisPin1 = 0; thisPin1 < 4; thisPin1++) {int pinState1 = bitRead(whichChannel1, thisPin1);digitalWrite(channel1[thisPin1],pinState1);}}
void muxWrite2(int whichChannel2) {for (int thisPin2 = 0; thisPin2 < 4; thisPin2++) {int pinState2 = bitRead(whichChannel2, thisPin2);digitalWrite(channel2[thisPin2],pinState2);}}
void muxWrite3(int whichChannel3) {for (int thisPin3 = 0; thisPin3 < 4; thisPin3++) {int pinState3 = bitRead(whichChannel3, thisPin3);digitalWrite(channel3[thisPin3],pinState3);}}
void muxWrite4(int whichChannel4) {for (int thisPin4 = 0; thisPin4 < 4; thisPin4++) {int pinState4 = bitRead(whichChannel4, thisPin4);digitalWrite(channel4[thisPin4],pinState4);}}
void muxWrite5(int whichChannel5) {for (int thisPin5 = 0; thisPin5 < 4; thisPin5++) {int pinState5 = bitRead(whichChannel5, thisPin5);digitalWrite(channel5[thisPin5],pinState5);}}
void muxWrite6(int whichChannel6) {for (int thisPin6 = 0; thisPin6 < 4; thisPin6++) {int pinState6 = bitRead(whichChannel6, thisPin6);digitalWrite(channel6[thisPin6],pinState6);}}
void muxWrite7(int whichChannel7) {for (int thisPin7 = 0; thisPin7 < 4; thisPin7++) {int pinState7 = bitRead(whichChannel7, thisPin7);digitalWrite(channel7[thisPin7],pinState7);}}


void loop() {                                             


buttonStateUp0 = digitalRead(buttonPinUp[0]);   if (buttonStateUp0 == HIGH && thisChannel0 < 15) {thisChannel0++; muxWrite0(thisChannel0); } 
buttonStateDn0 = digitalRead(buttonPinDn[0]);   if (buttonStateDn0 == HIGH && thisChannel0 > 0)  {thisChannel0--; muxWrite0(thisChannel0); } 

buttonStateUp1 = digitalRead(buttonPinUp[1]);   if (buttonStateUp1 == HIGH && thisChannel1 < 15) {thisChannel1++; muxWrite1(thisChannel1); } 
buttonStateDn1 = digitalRead(buttonPinDn[1]);   if (buttonStateDn1 == HIGH && thisChannel1 > 0)  {thisChannel1--; muxWrite1(thisChannel1); }
      
buttonStateUp2 = digitalRead(buttonPinUp[2]);   if (buttonStateUp2 == HIGH && thisChannel2 < 15) {thisChannel2++; muxWrite2(thisChannel2); } 
buttonStateDn2 = digitalRead(buttonPinDn[2]);   if (buttonStateDn2 == HIGH && thisChannel2 > 0)  {thisChannel2--; muxWrite2(thisChannel2); }

buttonStateUp3 = digitalRead(buttonPinUp[3]);   if (buttonStateUp3 == HIGH && thisChannel3 < 15) {thisChannel3++; muxWrite3(thisChannel3); } 
buttonStateDn3 = digitalRead(buttonPinDn[3]);   if (buttonStateDn3 == HIGH && thisChannel3 > 0)  {thisChannel3--; muxWrite3(thisChannel3); }
      
buttonStateUp4 = digitalRead(buttonPinUp[4]);   if (buttonStateUp4 == HIGH && thisChannel4 < 15) {thisChannel4++; muxWrite4(thisChannel4); } 
buttonStateDn4 = digitalRead(buttonPinDn[4]);   if (buttonStateDn4 == HIGH && thisChannel4 > 0)  {thisChannel4--; muxWrite4(thisChannel4); }
      
buttonStateUp5 = digitalRead(buttonPinUp[5]);   if (buttonStateUp5 == HIGH && thisChannel5 < 15) {thisChannel5++; muxWrite5(thisChannel5); } 
buttonStateDn5 = digitalRead(buttonPinDn[5]);   if (buttonStateDn5 == HIGH && thisChannel5 > 0)  {thisChannel5--; muxWrite5(thisChannel5); }
      
buttonStateUp6 = digitalRead(buttonPinUp[6]);   if (buttonStateUp6 == HIGH && thisChannel6 < 15) {thisChannel6++; muxWrite6(thisChannel6); } 
buttonStateDn6 = digitalRead(buttonPinDn[6]);   if (buttonStateDn6 == HIGH && thisChannel6 > 0)  {thisChannel6--; muxWrite6(thisChannel6); }
      
buttonStateUp7 = digitalRead(buttonPinUp[7]);   if (buttonStateUp7 == HIGH && thisChannel7 < 15) {thisChannel7++; muxWrite7(thisChannel7); } 
buttonStateDn7 = digitalRead(buttonPinDn[7]);   if (buttonStateDn7 == HIGH && thisChannel7 > 0)  {thisChannel7--; muxWrite7(thisChannel7); }
      


MIDI.read();
byte type = MIDI.getType();
byte number =  MIDI.getData1();
   if (MIDI.read()&& type == 0xC){                   
                thisChannel0 = EEPROM.read(address0[number]);
                thisChannel1 = EEPROM.read(address1[number]);
                thisChannel2 = EEPROM.read(address2[number]);
                thisChannel3 = EEPROM.read(address3[number]);
                thisChannel4 = EEPROM.read(address4[number]);
                thisChannel5 = EEPROM.read(address5[number]);
                thisChannel6 = EEPROM.read(address6[number]);
                thisChannel7 = EEPROM.read(address7[number]);
                muxWrite0(thisChannel0);
                muxWrite1(thisChannel1);
                muxWrite2(thisChannel2);
                muxWrite3(thisChannel3);               
                muxWrite4(thisChannel4);
                muxWrite5(thisChannel5);
                muxWrite6(thisChannel6);
                muxWrite7(thisChannel7);
              }                                                            
               

savebuttonState = digitalRead(saveButton); 
  if (savebuttonState == HIGH ) {
    EEPROM.update(address0[number],thisChannel0);
    EEPROM.update(address1[number],thisChannel1);
    EEPROM.update(address2[number],thisChannel2);
    EEPROM.update(address3[number],thisChannel3);
    EEPROM.update(address4[number],thisChannel4);
    EEPROM.update(address5[number],thisChannel5);
    EEPROM.update(address6[number],thisChannel6);
    EEPROM.update(address7[number],thisChannel7);
    
  }
        
            
            
      
   
    delay(50);
}

after few test, i found that the :
&& type == 0xC is wrong...
this is working:

if (MIDI.read()){                   
                thisChannel0 = EEPROM.read(address0[number]);
                thisChannel1 = EEPROM.read(address1[number]);
                thisChannel2 = EEPROM.read(address2[number]);
                thisChannel3 = EEPROM.read(address3[number]);
                thisChannel4 = EEPROM.read(address4[number]);
                thisChannel5 = EEPROM.read(address5[number]);
                thisChannel6 = EEPROM.read(address6[number]);
                thisChannel7 = EEPROM.read(address7[number]);
                muxWrite0(thisChannel0);
                muxWrite1(thisChannel1);
                muxWrite2(thisChannel2);
                muxWrite3(thisChannel3);               
                muxWrite4(thisChannel4);
                muxWrite5(thisChannel5);
                muxWrite6(thisChannel6);
                muxWrite7(thisChannel7);            
              
              }

BUT, now i only save at 1 place. It's always callback the last save, it doesn't save for each Program Change #

i made a smaller version for testing...

same problem, when i recall a Program Change # always the last save... doesn't save a specific value for each specific program change #
i want:

  • when i call Program Change #00 to have value save in pc00a
  • when i call Program Change #01 to have value save in pc01a
    etc...
#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


byte pc00a;
byte pc01a;
byte pc02a;
byte pc03a;
byte pc04a;
byte pc05a;
byte pc06a;
byte pc07a;


const int buttonPinDn  = 2; // ARDUINO PIN FOR EACH BUTTON UP,  2 = SECTION 0-UP, 4 = SECTION 1-UP, ETC...
const int buttonPinUp  = 3; // ARDUINO PIN FOR EACH BUTTON DOWN  3 = SECTION 0-DOWN, ETC..
const int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp = 0; 
int buttonStateDn = 0;
int thisChannel;
int channel [4] = {22, 23, 24, 25}; 

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
  
     pinMode(2, INPUT);
     pinMode(3, INPUT);
     pinMode(14, INPUT);
   for( int x=22; x<25; x++) 
    pinMode(x, OUTPUT);

}

void muxWrite(int whichChannel) {for (int thisPin = 0; thisPin < 4; thisPin++) {int pinState = bitRead(whichChannel, thisPin);digitalWrite(channel[thisPin],pinState);}}

void loop() {                                             

MIDI.read();
byte type = MIDI.getType();
byte number =  MIDI.getData1();
   if (MIDI.read()){                   
        switch (MIDI.getData1()) {
          case 00: thisChannel = EEPROM.read(pc00a);muxWrite(thisChannel);break;
          case 01: thisChannel = EEPROM.read(pc01a);muxWrite(thisChannel);break;
          case 02: thisChannel = EEPROM.read(pc02a);muxWrite(thisChannel);break;
          case 03: thisChannel = EEPROM.read(pc03a);muxWrite(thisChannel);break;
          case 04: thisChannel = EEPROM.read(pc04a);muxWrite(thisChannel);break;
          case 05: thisChannel = EEPROM.read(pc05a);muxWrite(thisChannel);break;
          case 06: thisChannel = EEPROM.read(pc06a);muxWrite(thisChannel);break;
          case 07: thisChannel = EEPROM.read(pc07a);muxWrite(thisChannel);break;
           
           }                        
              
              } 
              
buttonStateUp = digitalRead(buttonPinUp);   if (buttonStateUp == HIGH && thisChannel < 15) {thisChannel++; muxWrite(thisChannel); } 
buttonStateDn = digitalRead(buttonPinDn);   if (buttonStateDn == HIGH && thisChannel > 0)  {thisChannel--; muxWrite(thisChannel); } 

savebuttonState = digitalRead(saveButton); 
  if (savebuttonState == HIGH ) {
    switch (MIDI.getData1()) {
      case 00: EEPROM.update(pc00a,thisChannel);break;
      case 01: EEPROM.update(pc01a,thisChannel);break;
      case 02: EEPROM.update(pc02a,thisChannel);break;
      case 03: EEPROM.update(pc03a,thisChannel);break;
      case 04: EEPROM.update(pc04a,thisChannel);break;
      case 05: EEPROM.update(pc05a,thisChannel);break;
      case 06: EEPROM.update(pc06a,thisChannel);break;
      case 07: EEPROM.update(pc07a,thisChannel);break;
    }
       
  } 
    delay(50);
}

i've try without the EEPROm and just using int (flash)... It's working, but dont keep the info in memory... so when i power up, all memory is gone.

FOUND IT!!!!!!!!!!!!!!!!!!!!!!!!!!!
byte pc00a = 0;
byte pc01a = 1;
byte pc02a = 2;
byte pc03a = 3;
byte pc04a = 4;
byte pc05a = 5;
byte pc06a = 6;
byte pc07a = 7;

Why not do this in place of your switch statement:-

byte type = MIDI.getType();
byte number =  MIDI.getData1();
   if (MIDI.read() && number <8){ 
        muxWrite(EEPROM.read(number));
      }

And apply the same technique to that other case statement.

Note that

MIDI.read();
byte type = MIDI.getType();
byte number =  MIDI.getData1();
   if (MIDI.read()){ .....

Seems to be throwing away data from the first MIDI.getData1() call and is calling it twice. Is that what you want?

Thank you very much Grumpy_mike for helping! Really appreciated!!!

I've take your suggestion! really nice!!!!

i've made some ajustement, it's working but 2 problems occur:
-it's take 2 send of MIDI Program change to make the arduino change.... the led (13) light up on the first message, but arduino change only on the second send....

  • when i change program, if i push up or down, it move to the next channel of the last time i change channel with up or down, it doesn't move up or down from the position of the channel in use
#include <Bounce2.h>

#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();

byte number = 0;

int buttonPinDn  = 2; 
int buttonPinUp  = 3; 
int buttonPinDn1  = 16;
int buttonPinUp1  = 17; 

int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp = 0; 
int buttonStateDn = 0;
int thisChannel;
int channel [4] = {22, 23, 24, 25}; 
int buttonStateUp1 = 0; 
int buttonStateDn1 = 0;
int thisChannel1;
int channel1 [4] = {46, 47, 48, 49}; 

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);

debouncer1.attach(buttonPinUp);
debouncer1.interval(5); // interval in ms
debouncer2.attach(buttonPinDn);
debouncer2.interval(5); // interval in ms
debouncer3.attach(buttonPinUp1);
debouncer3.interval(5); // interval in ms
debouncer4.attach(buttonPinDn1);
debouncer4.interval(5); // interval in ms
debouncer5.attach(saveButton);
debouncer5.interval(5); // interval in ms


     pinMode(2, INPUT);
     pinMode(3, INPUT);
     pinMode(14, INPUT);
     pinMode(16, INPUT);
     pinMode(17, INPUT);
   for( int x=22; x<53; x++) {
    pinMode(x, OUTPUT);
    }
}
void muxWrite(int whichChannel) {for (int thisPin = 0; thisPin < 4; thisPin++) {int pinState = bitRead(whichChannel, thisPin);digitalWrite(channel[thisPin],pinState);}}
void muxWrite1(int whichChannel1) {for (int thisPin1 = 0; thisPin1 < 4; thisPin1++) {int pinState1 = bitRead(whichChannel1, thisPin1);digitalWrite(channel1[thisPin1],pinState1);}}

void loop() {                                             

debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();


byte type = MIDI.getType();
byte number =  MIDI.getData1(); 
 
   if (MIDI.read() && type == 0xC0){                   
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
    }      
  
buttonStateUp = digitalRead(buttonPinUp);   if (buttonStateUp == HIGH && thisChannel < 15 && value1 == LOW) {thisChannel++; muxWrite(thisChannel); } 
buttonStateDn = digitalRead(buttonPinDn);   if (buttonStateDn == HIGH && thisChannel > 0 && value2 == LOW)  {thisChannel--; muxWrite(thisChannel); } 
buttonStateUp1 = digitalRead(buttonPinUp1);   if (buttonStateUp1 == HIGH && thisChannel1 < 15 && value3 == LOW) {thisChannel1++; muxWrite1(thisChannel1); } 
buttonStateDn1 = digitalRead(buttonPinDn1);   if (buttonStateDn1 == HIGH && thisChannel1 > 0 && value4 == LOW)  {thisChannel1--; muxWrite1(thisChannel1); }

savebuttonState = digitalRead(saveButton); 
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    }
delay(50);
}

nope.... its working werdo....

there's something with the thisChannel & this Channel1 causing strange reaction...or maybe the 128 i put?

not always saving correctly... sometime i make some up/down on 'channel1' & save, and when i comeback to this pC#, 'channel' is change to another channel..... sometime it's the other way, change on 'channel' & save, then 'channel1' is not on what supposed to be save....

maybe it's save on the last position of the up/down.... maybe they act in parrallel and causing mixt up....

alright, i did this modification, and it now respond to the first midi message, and seem's to fix saving problem....

if (MIDI.read()){  
  byte type = MIDI.getType();
  byte number =  MIDI.getData1(); 
    if (type == 0xC0) {      
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
              
    }      
}

still have problem with the up/down that doesn't start from the channel in use....always start from the last manual change channel...
got to find a way to have the data from the muxWrite(EEPROM.read(number) into the 'thisChannel' from the buttonState.....

i made this modification, and seem'S to solved the problem, BUT

if (type == 0xC0) {      
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
       thisChannel = (EEPROM.read(number));
       thisChannel1 = (EEPROM.read(number+128));
       }

at PC# 09, i lost 'channel1' button up, nothing when pushing
sending PC# 0A, 0B, 0C, 0D,0E...
and at # 0F, i lost both button up.... no more up button working......

Can you post the whole code so I can see it in context.
Thanks

Thank's for helping!!!!!

#include <Bounce2.h>

#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


Bounce debouncer1 = Bounce(); 
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();

byte number = 0;

int buttonPinDn  = 2; 
int buttonPinUp  = 3; 
int buttonPinDn1  = 16;
int buttonPinUp1  = 17; 

int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp = 0; 
int buttonStateDn = 0;
int thisChannel;
int channel [4] = {22, 23, 24, 25}; 
int buttonStateUp1 = 0; 
int buttonStateDn1 = 0;
int thisChannel1;
int channel1 [4] = {46, 47, 48, 49}; 

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);

debouncer1.attach(buttonPinUp);
debouncer1.interval(5); // interval in ms
debouncer2.attach(buttonPinDn);
debouncer2.interval(5); // interval in ms
debouncer3.attach(buttonPinUp1);
debouncer3.interval(5); // interval in ms
debouncer4.attach(buttonPinDn1);
debouncer4.interval(5); // interval in ms
debouncer5.attach(saveButton);
debouncer5.interval(5); // interval in ms


     pinMode(2, INPUT);
     pinMode(3, INPUT);
     pinMode(14, INPUT);
     pinMode(16, INPUT);
     pinMode(17, INPUT);
   for( int x=22; x<53; x++) {
    pinMode(x, OUTPUT);
    }
}
void muxWrite(int whichChannel) {for (int thisPin = 0; thisPin < 4; thisPin++) {int pinState = bitRead(whichChannel, thisPin);digitalWrite(channel[thisPin],pinState);}}
void muxWrite1(int whichChannel1) {for (int thisPin1 = 0; thisPin1 < 4; thisPin1++) {int pinState1 = bitRead(whichChannel1, thisPin1);digitalWrite(channel1[thisPin1],pinState1);}}

void loop() {                                             


debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();

if (MIDI.read()){  
  byte type = MIDI.getType();
  byte number =  MIDI.getData1(); 
    if (type == 0xC0) {      
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
       thisChannel = (EEPROM.read(number));
       thisChannel1 = (EEPROM.read(number+128));
       }      
}

buttonStateUp = digitalRead(buttonPinUp);   if (buttonStateUp == HIGH && thisChannel < 15 && value1 == LOW) {thisChannel++; muxWrite(thisChannel); } 
buttonStateDn = digitalRead(buttonPinDn);   if (buttonStateDn == HIGH && thisChannel > 0 && value2 == LOW)  {thisChannel--; muxWrite(thisChannel); } 
buttonStateUp1 = digitalRead(buttonPinUp1);   if (buttonStateUp1 == HIGH && thisChannel1 < 15 && value3 == LOW) {thisChannel1++; muxWrite1(thisChannel1); } 
buttonStateDn1 = digitalRead(buttonPinDn1);   if (buttonStateDn1 == HIGH && thisChannel1 > 0 && value4 == LOW)  {thisChannel1--; muxWrite1(thisChannel1); }

savebuttonState = digitalRead(saveButton); 
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    }
delay(20);
}

if someone can help me out!

Yes I can but I am a bit ill at the moment, and your code needs a bit of working on, please hang on a day or two.

Big thank's grumpy_Mike!!!! I pass many days trying to fix without solving.....

I will wait....

regards,

bent

OK back. This is just the last code you posted but it is a much more readable state:-

#include <Bounce2.h>

#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();

byte number = 0;

int buttonPinDn  = 2;
int buttonPinUp  = 3;
int buttonPinDn1  = 16;
int buttonPinUp1  = 17;

int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp = 0;
int buttonStateDn = 0;
int thisChannel;
int channel [4] = {22, 23, 24, 25};
int buttonStateUp1 = 0;
int buttonStateDn1 = 0;
int thisChannel1;
int channel1 [4] = {46, 47, 48, 49};

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);

debouncer1.attach(buttonPinUp);
debouncer1.interval(5); // interval in ms
debouncer2.attach(buttonPinDn);
debouncer2.interval(5); // interval in ms
debouncer3.attach(buttonPinUp1);
debouncer3.interval(5); // interval in ms
debouncer4.attach(buttonPinDn1);
debouncer4.interval(5); // interval in ms
debouncer5.attach(saveButton);
debouncer5.interval(5); // interval in ms


     pinMode(2, INPUT);
     pinMode(3, INPUT);
     pinMode(14, INPUT);
     pinMode(16, INPUT);
     pinMode(17, INPUT);
   for( int x=22; x<53; x++) {
    pinMode(x, OUTPUT);
    }
}
void muxWrite(int whichChannel) {
  for (int thisPin = 0; thisPin < 4; thisPin++) {
    int pinState = bitRead(whichChannel, thisPin);
    digitalWrite(channel[thisPin],pinState);
    }
 }

void muxWrite1(int whichChannel1) {
  for (int thisPin1 = 0; thisPin1 < 4; thisPin1++) {
    int pinState1 = bitRead(whichChannel1, thisPin1);
    digitalWrite(channel1[thisPin1],pinState1);
    }
  }

void loop() {                                             
debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();

if (MIDI.read()){ 
  byte type = MIDI.getType();
  byte number =  MIDI.getData1();
    if (type == 0xC0) {     
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
       thisChannel = (EEPROM.read(number));
       thisChannel1 = (EEPROM.read(number+128));
       }     
}
// Update if button is pressed
buttonStateUp = digitalRead(buttonPinUp);  
if (buttonStateUp == HIGH && thisChannel < 15 && value1 == LOW) {
  thisChannel++; 
  muxWrite(thisChannel); 
 }

buttonStateDn = digitalRead(buttonPinDn);  
if (buttonStateDn == HIGH && thisChannel > 0 && value2 == LOW)  {
  thisChannel--; 
  muxWrite(thisChannel); 
 }
 
buttonStateUp1 = digitalRead(buttonPinUp1);   
if (buttonStateUp1 == HIGH && thisChannel1 < 15 && value3 == LOW) {
  thisChannel1++; 
  muxWrite1(thisChannel1); 
}
buttonStateDn1 = digitalRead(buttonPinDn1);   
if (buttonStateDn1 == HIGH && thisChannel1 > 0 && value4 == LOW)  {
  thisChannel1--; 
  muxWrite1(thisChannel1); 
}
// write the new values to EEPROM
savebuttonState = digitalRead(saveButton);
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    }
delay(20);
}

There are a number of problems with this. This first one is usage, what you wrote will work but again it is not how to do it correctly.
You define the function definition
void muxWrite1(int whichChannel1)
and call it with
muxWrite1(thisChannel1);

There is no need to have the same name of variable in the call as the function. If you do a new variable of the same name is created and then discarded when the function returns. If, as you have, the variable from the call is global, and you make any changes to it in the function, it is only the copy of the variable that gets changed and the global variable remains the same. This I think was causing your earlier confusion. In fact the way you use the variable I can see no reason why you would need to pass it in the first place.

at PC# 09, i lost 'channel1' button up, nothing when pushing
sending PC# 0A, 0B, 0C, 0D,0E...
and at # 0F, i lost both button up.... no more up button working

The numbers ( bit patterns ) you are writing are between 0 and 15, so the numbers you use in your checks are wrong.
if (buttonStateUp1 == HIGH && thisChannel1 < 15
should be
if (buttonStateUp1 == HIGH && thisChannel1 < 16
and
if (buttonStateDn1 == HIGH && thisChannel1 > 0
should be
if (buttonStateDn1 == HIGH && thisChannel1 > -1

Also when you come to incrementing or decrementing the values for thisChannel you should employ either a limit or a wrap round.
This is a limit

thisChannel++; 
if (thisChannel > 15) thisChannel=15;

this is a wrap round

thisChannel++; 
if (thisChannel > 15) thisChannel=0;

Hope that helps

Thank's Grumpy_mike!

I've try the modification but i still have the problem with the upbutton...

Bentfx:
at PC# 09, i lost 'channel1' button up, nothing when pushing
sending PC# 0A, 0B, 0C, 0D,0E...
and at # 0F, i lost both button up.... no more up button working......

ALSo notice, that when it happens, it only going down (dnbutton) and when it reach channel 0 , after it goes back to channel 15. keep pushing down, and go down: 2-1-0-15-14-13....

#include <Bounce2.h>

#include <EEPROM.h>

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();


Bounce debouncer1 = Bounce();
Bounce debouncer2 = Bounce();
Bounce debouncer3 = Bounce();
Bounce debouncer4 = Bounce();
Bounce debouncer5 = Bounce();

byte number = 0;

int buttonPinDn  = 2;
int buttonPinUp  = 3;
int buttonPinDn1  = 16;
int buttonPinUp1  = 17;

int saveButton = 14;
int savebuttonState = 0;

int buttonStateUp = 0;
int buttonStateDn = 0;
int thisChannel;
int channel [4] = {22, 23, 24, 25};
int buttonStateUp1 = 0;
int buttonStateDn1 = 0;
int thisChannel1;
int channel1 [4] = {46, 47, 48, 49};

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);

debouncer1.attach(buttonPinUp);
debouncer1.interval(5); // interval in ms
debouncer2.attach(buttonPinDn);
debouncer2.interval(5); // interval in ms
debouncer3.attach(buttonPinUp1);
debouncer3.interval(5); // interval in ms
debouncer4.attach(buttonPinDn1);
debouncer4.interval(5); // interval in ms
debouncer5.attach(saveButton);
debouncer5.interval(5); // interval in ms


     pinMode(2, INPUT);
     pinMode(3, INPUT);
     pinMode(14, INPUT);
     pinMode(16, INPUT);
     pinMode(17, INPUT);
   for( int x=22; x<53; x++) {
    pinMode(x, OUTPUT);
    }
}
void muxWrite(int thisChannel) {
  for (int thisPin = 0; thisPin < 4; thisPin++) {
    int pinState = bitRead(thisChannel, thisPin);
    digitalWrite(channel[thisPin],pinState);
    }
 }

void muxWrite1(int thisChannel1) {
  for (int thisPin1 = 0; thisPin1 < 4; thisPin1++) {
    int pinState1 = bitRead(thisChannel1, thisPin1);
    digitalWrite(channel1[thisPin1],pinState1);
    }
  }

void loop() {                                             
debouncer1.update();
debouncer2.update();
debouncer3.update();
debouncer4.update();
debouncer5.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();

if (MIDI.read()){ 
  byte type = MIDI.getType();
  byte number =  MIDI.getData1();
    if (type == 0xC0) {     
       muxWrite(EEPROM.read(number));
       muxWrite1(EEPROM.read(number+128));
       thisChannel = (EEPROM.read(number));
       thisChannel1 = (EEPROM.read(number+128));
       }     
}
// Update if button is pressed
buttonStateUp = digitalRead(buttonPinUp);  
if (buttonStateUp == HIGH && thisChannel < 16 && value1 == LOW) {
  thisChannel++; 
if (thisChannel > 15) {thisChannel=15; }
  muxWrite(thisChannel); 
 }

buttonStateDn = digitalRead(buttonPinDn);  
if (buttonStateDn == HIGH && thisChannel > -1 && value2 == LOW)  {
  thisChannel--; 
if (thisChannel < 0) {thisChannel=0;}
  muxWrite(thisChannel); 
 }
 
buttonStateUp1 = digitalRead(buttonPinUp1);   
if (buttonStateUp1 == HIGH && thisChannel1 < 16 && value3 == LOW) {
  thisChannel1++;
if (thisChannel1 > 15){ thisChannel1=15;}
  muxWrite1(thisChannel1); 
}
buttonStateDn1 = digitalRead(buttonPinDn1);   
if (buttonStateDn1 == HIGH && thisChannel1 > -1 && value4 == LOW)  {
  thisChannel1--;  
if (thisChannel1 < 0) {thisChannel1=0;}
  muxWrite1(thisChannel1); 
}
// write the new values to EEPROM
savebuttonState = digitalRead(saveButton);
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    }
delay(20);
}

regards,

bent