Go Down

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


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]



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


 void upmove(){
upstate = digitalRead(2);
if(upstate != lastupstate){
  lastupstate = upstate;

void downmove(){
downstate = digitalRead(3);
if(downstate != lastdownstate){
  lastdownstate = downstate;

void loop() {
case 1 :

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

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


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