Go Down

Topic: MIDI - save mux state into Program Change # (Read 3339 times) previous topic - next topic

Bentfx

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:

Code: [Select]
#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

Bentfx

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....

Grumpy_Mike

#2
Nov 23, 2016, 12:04 am Last Edit: Nov 23, 2016, 12:08 am by Grumpy_Mike
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

Bentfx

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

Bentfx

#4
Nov 30, 2016, 03:32 pm Last Edit: Nov 30, 2016, 04:41 pm by Bentfx
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....
Code: [Select]
#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);
}



Bentfx

after few test, i found that the :
&& type == 0xC   is wrong...
this is working:
Code: [Select]
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 #

Bentfx

#6
Nov 30, 2016, 07:57 pm Last Edit: Nov 30, 2016, 08:25 pm by Bentfx
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...

Code: [Select]
#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.

Bentfx

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;

Grumpy_Mike

Why not do this in place of your switch statement:-
Code: [Select]

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
Code: [Select]
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?

Bentfx

#9
Dec 01, 2016, 06:32 pm Last Edit: Dec 01, 2016, 08:44 pm by Bentfx
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
Code: [Select]
#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);
}


Bentfx

#10
Dec 01, 2016, 10:03 pm Last Edit: Dec 01, 2016, 10:11 pm by Bentfx
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....

Bentfx

#11
Dec 01, 2016, 11:09 pm Last Edit: Dec 01, 2016, 11:26 pm by Bentfx
alright, i did this modification, and it now respond to the first midi message, and seem's to fix saving problem....
Code: [Select]
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.....

Bentfx

#12
Dec 02, 2016, 04:13 pm Last Edit: Dec 02, 2016, 04:29 pm by Bentfx
i made this modification, and seem'S to solved the problem, BUT
Code: [Select]
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......


Grumpy_Mike

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

Bentfx

Thank's for helping!!!!!
Code: [Select]
#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);
}



Go Up