MIDI Mode Button

Hello,

This is my first post here. I'm excited to start learning more about Arduino and Coding in general! I started learning this out of necessity and I'm finding that the learning curve is a little steeper that I initially thought. I'll start by explaining what I want my project to do, followed by what I've done so far, and then finish with what I'm stuck on.

I'm building a MIDI controller similar to the one used by Ed Sheeran known as the "Chewie Monsta." This pedal will have 7 sustain pedals (momentary switches) across the front and 2 buttons (momentary switches) on the top of the pedal. These buttons will be used to control a software VST by sending a MIDI signal via USB to the computer. The controller is to have 2 "modes" which will allow the pedals to do different things based on whether it's in "record mode" or "play mode" which will require me to program a "mode button" that will toggle between mode 1 and mode 2. Each mode will cause the other buttons to send a different midi note. For example, the 4th button, while in record mode (mode 1), will act as a record button for channel 1 of the software by sending midi note (0x04) but in "play mode" (mode 2) it should send midi note (0x05)

So far I have established each switch to its correct digital pin on the Arduino Mega 2560 and set the pinMode to INPUT_PULLUP based on my basic understanding of how buttons work with the Arduino.

What I'm currently stuck on is how to set up that mode button, and how to code each button to react differently based on which mode the pedal is currently in.

If my understanding of what I need for code is incorrect I will happily accept guidance and suggestions to help me get started in the right direction.

Thank you in advance :)

-Aaron P

Just set a variable to hold the mode and then you can make decisions based on that mode. For example:

int mode = 0;

void setup(){
   // the normal setup stuff, you know
}

void loop(){
   if (digitalRead(modeButton) == LOW) {  // mode button is pressed
        // some code to set mode
        //  maybe something like

        if (mode == 1) mode = 0;
        else if (mode == 0) mode = 1;

        // or something like that
        // there are better ways but this gets you the picture
   }

   ///  then later you can have
  
   if (mode == 0) {
      
         // read your buttons and do the mode 0 stuff

   }
    else if (mode == 1) {
  
         // read the other buttons and do mode 1 stuff
   }

}

That's a pretty ugly example and isn't quite what you'd actually use. But without the specifics it's about all I can do. Either way, I think you can get the picture of using a variable to decide the state or mode of the program and write two different codes which run depending on what mode you are in.

This is incredibly helpful in understanding the concept of what I'm doing! I will be testing some code out tonight and I will post my results on here. Thank you!

Okay, I’m on my lunch break at work and doing my best to understand this code without having my Arduino near me to test, This is the code I’ve been able to come up with based on my understanding so far.

#include <SoftwareSerial.h>
SoftwareSerial midi(8,7); //RX TX

//NAMING/ASSIGNING MY BUTTONS
//all buttons are assigned to digital pins 2-10. Naming based on actual midi function.
int recButton = 2;
int stopButton = 3;
int modeButton = 4;
int track1Button = 5;
int track2Button = 6;
int track3Button = 7;
int track4Button = 8;
int undoButton = 9;
int clearButton = 10;

//NAMING/ASSIGNING MY LEDS
//all leds are assigned to digital pins 22-31. Naming based on buttons
int recLED = 22;
int stopLED = 23;
int modeLED = 24;
int track1LED = 25;
int track2LED = 26;
int track3LED = 27;
int track4LED = 28;
int undoLED = 29;
int clearLED = 30;
int logoLED = 31;

int mode = 1;

//program
void setup() {
  // put your setup code here, to run once:
  Serial.begin(31250);
}

void loop() {
  // put your main code here, to run repeatedly:

  //mode button and LED setup
if (digitalRead(modeButton) == HIGH) { //upon button press
  if (mode == 1) mode = 2;            //mode will change
  if else (mode == 2) mode = 1;
  }

if (mode == 1) modeLED = LOW; //if mode 1 is active the LED will be off
if else (mode == 2) modeLED = HIGH; //if mode 2 is active the LED will be on

  //recButton and LED setup
  (digitalRead(recButton) == HIGH) {              //when recButton is pressed
    if (mode == 1) MIDI.sendNoteOn(a, b, c);      //button will send midi note
    else if (mode == 2) MIDI.sendNoteOn(a, d, c); //based on active mode
    }
 }

I only coded the recButton so far but plan to do the same kind of coding for the other buttons as well, I just want to make sure I’m on the right track before I do that. Any help or advice would be greatly appreciated :slight_smile:

Okay… so I’m finally getting somewhere!
I got some code from an instructable that does pretty much exactly what I would like to do. I copied the code and edited it as needed to add additional buttons and LEDs. This seemed easy enough but when I went to compile, I got the following error:

   if(digitalRead(stopButton) == LOW && toggle2 == 0){
   ^
exit status 1
expected unqualified-id before 'if'

The full code is here:

#include <SoftwareSerial.h>
SoftwareSerial midi(8,7); ///RX TX

int recButton = 2;
int stopButton = 3;
int modeButton = 4; 
int track1Button = 5;
int track2Button = 6;
int track3Button = 7;
int track4Button = 8;
int undoButton = 9; 
int clearButton = 10;
int mode1LED = 11;
int mode2LED = 12;
int logoLED = 13;

int toggle1 = 0;
int toggle2 = 0;
int toggle3 = 0;
int toggle4 = 0;
int toggle5 = 0;
int toggle6 = 0;
int toggle7 = 0;
int toggle8 = 0;
int toggle9 = 0;

int mode = 0;

void setup(){
  pinMode (logoLED, OUTPUT);
  digitalWrite(logoLED, HIGH);
  pinMode (clearButton, INPUT_PULLUP);
  pinMode (undoButton, INPUT_PULLUP);
  pinMode (recButton, INPUT_PULLUP);
  pinMode (stopButton, INPUT_PULLUP);
  pinMode (track1Button, INPUT_PULLUP);
  pinMode (track2Button, INPUT_PULLUP);
  pinMode (track3Button, INPUT_PULLUP);
  pinMode (track4Button, INPUT_PULLUP);
  pinMode (modeButton, INPUT_PULLUP);
  pinMode (undoButton, INPUT_PULLUP);
  pinMode (mode1LED, OUTPUT);
  pinMode (mode2LED, OUTPUT);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, HIGH);
  delay(250);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, HIGH);
  delay(250);
  digitalWrite(mode1LED, HIGH);
  digitalWrite(mode2LED, LOW);
  delay(250);
  digitalWrite(mode1LED, LOW);
  digitalWrite(mode2LED, LOW);
  delay(250);

  midi.begin(31250);
}

void loop(){
//record button
    if(digitalRead(recButton) == LOW && toggle1 == 0){
    toggle1 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write(0x45);
      // delay(300);
    }
  }

  if(digitalRead(recButton) == HIGH && toggle1 == 1){
   toggle1 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x10);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x35);
      midi.write((byte)0x00);
      delay(300);

    }
  }
}
//stop button
  if(digitalRead(stopButton) == LOW && toggle2 == 0){
    toggle2 = 1;
    ///Serial.println("mute/stop");  
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write(0x45); 
      //delay(300); 
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write(0x45); 
      //delay(300); 
    }
  }


  if(digitalRead(stopButton) == HIGH && toggle2 == 1){
    toggle2 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x07);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x34);
      midi.write((byte)0x00);
      delay(300);

    }
  }
//mode button
  if (digitalRead(modeButton) == LOW && toggle3 == 0){
    toggle3 = 1; 
    if(mode == 0){


      midi.write(0x90);
      midi.write(0x17);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){


      midi.write(0x90);
      midi.write(0x30);
      midi.write(0x01);
      //  delay(300);
    }


  } 

  if (digitalRead(modeButton) == HIGH && toggle3 == 1){
    toggle3 = 0;
    if(mode == 0){
      mode = 1;
      midi.write(0x90);
      midi.write(0x17);
      midi.write((byte)0x00);  
      digitalWrite(mode1LED, HIGH);
      digitalWrite(mode2LED, LOW);
      delay(300);
    }
    else if(mode == 1){
      mode = 0;
      midi.write(0x90);
      midi.write(0x30);
      midi.write((byte)0x00);  
      digitalWrite(mode1LED, LOW);
      digitalWrite(mode2LED, HIGH);
      delay(300);
    }

  }
//track 1 button
  if (digitalRead(track1Button) == LOW && toggle4 == 0){
    toggle4 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write(0x01);
      // delay(300);
    } 
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write(0x01);
      //  delay(300);
    }
  } 

  if (digitalRead(track1Button) == HIGH && toggle4 == 1){
    toggle4 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x14);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x33);
      midi.write((byte)0x00); 
      delay(300);
    }
  }
//track 2 button
  if (digitalRead(track2Button) == LOW && toggle5 == 0){
    toggle5 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write(0x01);
      // delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write(0x01);
      // delay(300);
    }

  } 

  if (digitalRead(track2Button) == HIGH && toggle5 == 1){
    toggle5 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x15);
      midi.write((byte)0x00);  
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x32);
      midi.write((byte)0x00);  
      delay(300);
    }
  }
//track 3 button
  if (digitalRead(track3Button) == LOW && toggle6 == 0){
    toggle6 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write(0x01);
      //  delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x31);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track3Button) == HIGH && toggle6 == 1){
    toggle6 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x16);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x31);
      midi.write((byte)0x00); 
      delay(300);

    }
  }
//track 4 button
    if (digitalRead(track4Button) == LOW && toggle7 == 0){
    toggle7 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x18);
      midi.write(0x01);
      //  delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x29);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(track4Button) == HIGH && toggle7 == 1){
    toggle7 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x18);
      midi.write((byte)0x00); 
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x29);
      midi.write((byte)0x00); 
      delay(300);

    }
  }
//undo button
  if (digitalRead(undoButton) == LOW && toggle8 == 0){
    toggle8 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write(0x01);
      // delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write(0x01);
      // delay(300);
    }
  } 

  if (digitalRead(undoButton) == HIGH && toggle8 == 1){
    toggle8 = 0;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x41);
      midi.write((byte)0x00);  
      delay(300);
    }

    if(mode == 1){
      midi.write(0x90);
      midi.write(0x36);
      midi.write((byte)0x00);  
      delay(300);
    }
  }
//clear button
  if(digitalRead(clearButton) == LOW && toggle9 == 0){
    toggle9 = 1;
    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write(0x45);
      //delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write(0x45);
      //delay(300);  
    }
  }


  if(digitalRead(clearButton) == HIGH && toggle9 == 1){ 
    toggle9 = 0;

    if(mode == 0){
      midi.write(0x90);
      midi.write(0x01);
      midi.write((byte)0x00);
      delay(300);
    }
    if(mode == 1){
      midi.write(0x90);
      midi.write(0x37);
      midi.write((byte)0x00);
      delay(300);
    }

  }

The strangest thing about this is that this code was copied directly from the source code that DID compile successfully so I'm not sure why it's no longer able to compile once copied.

Check your braces. That line you marked is not inside the loop function.

and now I feel like a dummy... :P That was it! Thank you!!

This is my first project and so you can imagine my excitement and joy the first time I tested each button and they reacted as expected!!! Happy day! :)