Go Down

Topic: bank change function for midi controller (Read 79 times) previous topic - next topic

Tomatoterry

Jul 15, 2019, 08:43 pm Last Edit: Jul 16, 2019, 02:25 am by Tomatoterry
I am writing a sketch for midi controller which have 4 button, two buttons are used to control the bank up and down, two buttons are used to send ProgramChange Msg to the effect.

here is My plan

Bank1 : press button will send PC 98/99
Bank2 : press button will send PC124/125

The problem is , Bank 1 is function, but when I press the bank up button to change to bank2, those button didn't send PC 124 and 125, and press the bank down button to change back to bank1, those button function again( send PC 98 and 99)

Here is the code, can anyone help me ?

Code: [Select]


#include<MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

int upstate = 0;
int lastupstate = 0;
int downstate = 0;
int lastdownstate = 0;
int BankNumber = 1;

void setup() {

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);

  MIDI.begin(MIDI_CHANNEL_OMNI);
 
}

 void upmove(){
upstate = digitalRead(2);
if(upstate != lastupstate){
  BankNumber++;
  delay(50);
  lastupstate = upstate;
}
}


void downmove(){
downstate = digitalRead(3);
if(downstate != lastdownstate){
  BankNumber--;
  delay(50);
  lastdownstate = downstate;
}
}


void loop() {
   upmove();
   downmove();
   
switch(BankNumber){
case 1 :

 if (digitalRead(4) == LOW) {
    MIDI.sendProgramChange(98, 1);
    delay(200);}
if (digitalRead(5) == LOW) {
    MIDI.sendProgramChange(99, 1);
    delay(200);}
    break;

case 2 :
  if (digitalRead(4) == LOW) {
    MIDI.sendProgramChange(124, 1);
    delay(200);}
    if (digitalRead(5) == LOW) {
    MIDI.sendProgramChange(125, 1);
    delay(200);}
    break;
  }  } 


blh64

Welcome to the fourms.  Please read the sticky post at the top of the forum about how to post your code using code tags.  It helps people help you.

The issue with your code is that the upmove() function increments BankNumber after every transition.  When you press the button, that is a transition (BankNumber++) then you release the button so that is another transition (BankNumber++)

So what value does BankNumber contain?  It is not 1 and it is not 2 so your switch statement doesn't do either of those cases.

Go Up