Go Down

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

Bentfx


Grumpy_Mike

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.

Bentfx

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

I will wait....

regards,

bent

Grumpy_Mike

OK back. This is just the last code you posted but it is a much more readable state:-
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));
       }    
}
// 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.

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

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

this is a wrap round
Code: [Select]

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


Hope that helps

Bentfx

#19
Dec 13, 2016, 12:53 am Last Edit: Dec 13, 2016, 01:41 am by Bentfx
Thank's Grumpy_mike!

I've try the modification but i still have the problem with the upbutton...
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....
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 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

Bentfx

when i disable
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));
       ////thisChannel = (EEPROM.read(number));        this disable
       //thisChannel1 = (EEPROM.read(number+128));   this disable
       }     
}


it run perfectly, except that it start up/down from the last manual movement

Grumpy_Mike

The variable thisChannel is always pointing at the one you have just written to. The only time it is increments is on the buttons. You never change the value in the MIDI read situation.

I said don't pass it as a variable to the write mux function. If you do that you can increment it in there.

Bentfx

#22
Dec 13, 2016, 02:13 am Last Edit: Dec 13, 2016, 04:36 am by Bentfx
i feel like i,m turning around the solution without finding it.... i'm trying so many thing, and always coming back to 2 problems:
- system is runing prefectly except that up/down start from last manual change of state
- up/down change from the state save/recall by PC#, but i lost the up button....

Bentfx

FINALLY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I've made some change and it's totally working perfectly!!!!

A very big thank you Grumpy_Mike for your precious help!!!!!! Woud'nt make it without you!!!!

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 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));
       }
                 
}

buttonStateUp = digitalRead(buttonPinUp);
buttonStateDn = digitalRead(buttonPinDn);
buttonStateUp1 = digitalRead(buttonPinUp1);
buttonStateDn1 = digitalRead(buttonPinDn1);

if (thisChannel>15){thisChannel=15;}
if (thisChannel<0){thisChannel=0;}

if (thisChannel < 16 && thisChannel > -1) {
  if ( buttonStateUp == HIGH && value1 == LOW ) {
    thisChannel++;
    if (thisChannel>15){thisChannel=15;}
  }
  if ( buttonStateDn == HIGH && value2 == LOW ) {
    thisChannel--;
    if (thisChannel<0){thisChannel=0;}
  }
  muxWrite(thisChannel);
}

if (thisChannel1>15){thisChannel1=15;}
if (thisChannel1<0){thisChannel1=0;}

if (thisChannel1 < 16 && thisChannel1 > -1) {
  if ( buttonStateUp1 == HIGH && value3 == LOW ) {
    thisChannel1++;
    if (thisChannel1>15){thisChannel1=15;}
  }
  if ( buttonStateDn1 == HIGH && 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);
}

Bentfx

trying to add a momentary switch that control a toggle led with memory recall from PC#

Bentfx

#25
Dec 14, 2016, 04:42 am Last Edit: Dec 14, 2016, 07:01 am by Bentfx
so far; but not saving ...
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();
Bounce debouncer6 = Bounce();


byte number = 0;

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

int saveButton = 14;
int savebuttonState = 0;

const int toggleButton = A8;
int toggleState = 0;
const int toggleLed = A9;
int stateLed = 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
debouncer6.attach(toggleButton);
debouncer6.interval(5);

     pinMode(A0, OUTPUT);
     pinMode(A1, OUTPUT);
     pinMode(toggleButton, INPUT);
     pinMode(toggleLed, OUTPUT);

     
     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();
debouncer6.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();
int value6 = debouncer6.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));
       stateLed = (EEPROM.read(number+256));
         digitalWrite(toggleLed, stateLed);
       }
                 
}




buttonStateUp = digitalRead(buttonPinUp);
buttonStateDn = digitalRead(buttonPinDn);
buttonStateUp1 = digitalRead(buttonPinUp1);
buttonStateDn1 = digitalRead(buttonPinDn1);

if (thisChannel>31){thisChannel=31;}
if (thisChannel<0){thisChannel=0;}

if (thisChannel < 32 && thisChannel > -1) {
  if ( buttonStateUp == HIGH && value1 == LOW ) {
    thisChannel++;
    if (thisChannel>31){thisChannel=31;}
  }
  if ( buttonStateDn == HIGH && value2 == LOW ) {
    thisChannel--;
    if (thisChannel<0){thisChannel=0;}
  }
  muxWrite(thisChannel);
  if (thisChannel >= 16 && thisChannel <= 31 ){digitalWrite(A0, HIGH);}
  if (thisChannel <= 15 && thisChannel >= 0 ) {digitalWrite(A0, LOW);}
 
}

if (thisChannel1>31){thisChannel1=31;}
if (thisChannel1<0){thisChannel1=0;}

if (thisChannel1 < 32 && thisChannel1 > -1) {
  if ( buttonStateUp1 == HIGH && value3 == LOW ) {
    thisChannel1++;
    if (thisChannel1>31){thisChannel1=31;}
  }
  if ( buttonStateDn1 == HIGH && value4 == LOW ) {
    thisChannel1--;
    if (thisChannel1<0){thisChannel1=0;}
  }
  muxWrite1(thisChannel1);
  if (thisChannel1 >= 16 && thisChannel1 <= 31 ){digitalWrite(A1, HIGH);}
  if (thisChannel1 <= 15 && thisChannel1 >= 0 ) {digitalWrite(A1, LOW);}
}

toggleState = digitalRead(toggleButton);   //  momentary switch to control a led
stateLed = digitalRead(toggleLed);
if (toggleState == HIGH && stateLed == HIGH && value6 == LOW){
     digitalWrite(toggleLed, LOW);
    }
if (toggleState == HIGH && stateLed == LOW && value6 == LOW){
    digitalWrite(toggleLed, HIGH);
    }





// write the new values to EEPROM
savebuttonState = digitalRead(saveButton);
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    EEPROM.update(number+256, stateLed);
    }
delay(20);
}



Bentfx

new try but no more result...

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();
Bounce debouncer6 = Bounce();


byte number = 0;

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

int saveButton = 14;
int savebuttonState = 0;

const int toggleButton = A8;
int toggleState;
const int toggleLed = A9;
int stateLed;

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
debouncer6.attach(toggleButton);
debouncer6.interval(5);

     pinMode(A0, OUTPUT);
     pinMode(A1, OUTPUT);
     pinMode(A8, INPUT);
     pinMode(A9, OUTPUT);

     
     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();
debouncer6.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();
int value6 = debouncer6.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));
       stateLed = (EEPROM.read(number+256));
         if (stateLed == 0){digitalWrite(A9, LOW);}
         if (stateLed == 1){digitalWrite(A9, HIGH);}
       }
                 
}




buttonStateUp = digitalRead(buttonPinUp);
buttonStateDn = digitalRead(buttonPinDn);
buttonStateUp1 = digitalRead(buttonPinUp1);
buttonStateDn1 = digitalRead(buttonPinDn1);

if (thisChannel>31){thisChannel=31;}
if (thisChannel<0){thisChannel=0;}

if (thisChannel < 32 && thisChannel > -1) {
  if ( buttonStateUp == HIGH && value1 == LOW ) {
    thisChannel++;
    if (thisChannel>31){thisChannel=31;}
  }
  if ( buttonStateDn == HIGH && value2 == LOW ) {
    thisChannel--;
    if (thisChannel<0){thisChannel=0;}
  }
  muxWrite(thisChannel);
  if (thisChannel >= 16 && thisChannel <= 31 ){digitalWrite(A0, HIGH);}
  if (thisChannel <= 15 && thisChannel >= 0 ) {digitalWrite(A0, LOW);}
 
}

if (thisChannel1>31){thisChannel1=31;}
if (thisChannel1<0){thisChannel1=0;}

if (thisChannel1 < 32 && thisChannel1 > -1) {
  if ( buttonStateUp1 == HIGH && value3 == LOW ) {
    thisChannel1++;
    if (thisChannel1>31){thisChannel1=31;}
  }
  if ( buttonStateDn1 == HIGH && value4 == LOW ) {
    thisChannel1--;
    if (thisChannel1<0){thisChannel1=0;}
  }
  muxWrite1(thisChannel1);
  if (thisChannel1 >= 16 && thisChannel1 <= 31 ){digitalWrite(A1, HIGH);}
  if (thisChannel1 <= 15 && thisChannel1 >= 0 ) {digitalWrite(A1, LOW);}
}

toggleState = digitalRead(toggleButton);   //  momentary switch to control a led

if (toggleState == HIGH && value6 == LOW){
    stateLed++;
    if (stateLed > 1){stateLed = 0;}

if (stateLed == 0){digitalWrite(A9, LOW);}
if (stateLed == 1){digitalWrite(A9, HIGH);}
}

// write the new values to EEPROM
savebuttonState = digitalRead(saveButton);
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    EEPROM.update(number+256, stateLed);
    }
delay(50);
}


Bentfx

with all testing, i've found that it seem's to save the state at only 1 place and apply it to all PC# for toggleLed.....doesn't make the difference from one memory to another...

Bentfx

another try without success...
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();
Bounce debouncer6 = Bounce();


byte number = 0;
byte Led = 0;

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

int saveButton = 14;
int savebuttonState = 0;

const int toggleButton = A8;
int toggleState;
const int toggleLed = A9;
byte stateLed = 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
debouncer6.attach(toggleButton);
debouncer6.interval(5);

     pinMode(A0, OUTPUT);
     pinMode(A1, OUTPUT);
     pinMode(A8, INPUT);
     pinMode(A9, OUTPUT);

     
     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();
debouncer6.update();

int value1 = debouncer1.read();
int value2 = debouncer2.read();
int value3 = debouncer3.read();
int value4 = debouncer4.read();
int value5 = debouncer5.read();
int value6 = debouncer6.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));
       Led = (EEPROM.read(number+256));
         if (Led == 0){digitalWrite(A9, LOW);}
         if (Led == 1){digitalWrite(A9, HIGH);}
       }
                 
}




buttonStateUp = digitalRead(buttonPinUp);
buttonStateDn = digitalRead(buttonPinDn);
buttonStateUp1 = digitalRead(buttonPinUp1);
buttonStateDn1 = digitalRead(buttonPinDn1);

if (thisChannel>31){thisChannel=31;}
if (thisChannel<0){thisChannel=0;}

if (thisChannel < 32 && thisChannel > -1) {
  if ( buttonStateUp == HIGH && value1 == LOW ) {
    thisChannel++;
    if (thisChannel>31){thisChannel=31;}
  }
  if ( buttonStateDn == HIGH && value2 == LOW ) {
    thisChannel--;
    if (thisChannel<0){thisChannel=0;}
  }
  muxWrite(thisChannel);
  if (thisChannel >= 16 && thisChannel <= 31 ){digitalWrite(A0, HIGH);}
  if (thisChannel <= 15 && thisChannel >= 0 ) {digitalWrite(A0, LOW);}
 
}

if (thisChannel1>31){thisChannel1=31;}
if (thisChannel1<0){thisChannel1=0;}

if (thisChannel1 < 32 && thisChannel1 > -1) {
  if ( buttonStateUp1 == HIGH && value3 == LOW ) {
    thisChannel1++;
    if (thisChannel1>31){thisChannel1=31;}
  }
  if ( buttonStateDn1 == HIGH && value4 == LOW ) {
    thisChannel1--;
    if (thisChannel1<0){thisChannel1=0;}
  }
  muxWrite1(thisChannel1);
  if (thisChannel1 >= 16 && thisChannel1 <= 31 ){digitalWrite(A1, HIGH);}
  if (thisChannel1 <= 15 && thisChannel1 >= 0 ) {digitalWrite(A1, LOW);}
}

toggleState = digitalRead(toggleButton);   //  momentary switch to control a led
stateLed = digitalRead(toggleLed);
if (toggleState == HIGH && value6 == LOW){
    if (stateLed == HIGH){
        digitalWrite(A9, LOW);
        Led = 0;
    }
    if (stateLed == LOW){
      digitalWrite(A9, HIGH);
      Led = 1;
    }
}
// write the new values to EEPROM
savebuttonState = digitalRead(saveButton);
  if (savebuttonState == HIGH && value5 == LOW) {
    EEPROM.update(number, thisChannel);
    EEPROM.update(number+128, thisChannel1);
    EEPROM.update(number+256, Led);
    }
delay(50);
}


Grumpy_Mike

Not sure what you are trying to achieve here. You seem to have a lack of EEPROM reading going on.
First I would expect something in the setup function to set the initial value.
Then I would expect to see the same sort of thing on the push button code.

You are still passing that useless value to the writeMux functions.

Go Up