Switching MIDI CC outputs

Hi guys,

I’m building a MIDI controller and I need to be able to use my potentiometers to control different parameters when a button is pressed (i.e switching banks). I’ve set up a counter, the value of which determines what MIDI CC command is sent, but this only works while the button is pressed. I can switch between CC 74 and CC 75 but only while I hold down the modePin button (8). I’ve checked out various tutorials and projects but the different types of values I need to check and write and read to have got me lost. So here’s what I have so far:

#include <Bounce.h>  
const int channel = 1; 
int buttonState;           // The previous state of the button recorded here
int notePin = (7);
int modePin = (8);
int val;                       // the current value of the button
int mode;                   // the mode I want to send out the CC data on (74 or 75, basically)

Bounce button1 = Bounce(7, 5); 
Bounce button2 = Bounce(8, 5);  


void setup() {
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  buttonState = digitalRead(modePin);    
}

void loop() {
  
  val = digitalRead(modePin);      //what state is the button in?
  
  if (val != buttonState)             // is this what it was before?
  {
    if (val == HIGH)                   // if it's ON, then increment the mode value. Otherwise, decrement it.
    {
      mode++;
    }else{
      mode--;
    }
  }
   
   buttonState = val;                // remember the state of the button



{                                          //if mode is 0, the analog value (/8) is sent out as a CC 74, otherwise as a 75
  if (mode == 0){
  
  usbMIDI.sendControlChange(74, analogRead(0) / 8, channel);
  }
  else
  {
    usbMIDI.sendControlChange(75, analogRead(0) / 8, channel);
  }
}

    button1.update();                                    // code to make my other button send out a note on C4. 
    
if (button1.fallingEdge()) {
    usbMIDI.sendNoteOn(60, 99, 1);  // 60 = C4
  }
  

if (button1.risingEdge()) {
    usbMIDI.sendNoteOff(60, 0, 1);  // 60 = C4


}
}

This does sent out MIDI data as CC 75 and I can change it to CC 74 but I can’t get it to stay as 74 or 75 until the button is pressed again.

Any help would be greatly appreciated!

Thanks,

Ben

int buttonState;           // The previous state of the button recorded here
int val;                       // the current value of the button

You might as well start now with meaningful names. Something like currState and prevState would make a whole lot more sense.

int notePin = (7);
int modePin = (8);

Parentheses look silly here. They are not needed.

Bounce button1 = Bounce(7, 5); 
Bounce button2 = Bounce(8, 5);

You gave the pins names. Use them.

  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);

Ditto.

{                                          //if mode is 0, the analog value (/8) is sent out as a CC 74, otherwise as a 75

That curly brace, and one somewhere later are not needed. Get rid of them.

    if (val == HIGH)                   // if it's ON, then increment the mode value. Otherwise, decrement it.
    {
      mode++;
    }else{
      mode--;
    }

You want mode to have one of two values, 0 or 1, what's with the increment and decrement. Lose them, and just assign the value you want mode to have. It actually looks like you want mode to have the value of val, regardless of what val is.

Thanks Paul.

I've gone through and neatened the sketch up a little. Here's what I have now;

void loop() {
  
  currState = digitalRead(modePin);
  
  if (currState != prevState)
  {
    if (currState == HIGH)
    {
     mode = 1;
    }else{
      mode = 0;
    }
  }
   
   prevState = currState;




  if (mode == 0){
  
  usbMIDI.sendControlChange(74, analogRead(0) / 8, channel);
  }
  else
  {
    usbMIDI.sendControlChange(75, analogRead(0) / 8, channel);
  }

This sets 'mode' to 1 only while digitalRead is HIGH. I need it to set 'mode' to 1 if digitalRead has been high, then to 0 if digitalRead is high and 'mode' is 1.

Sorry, I'm aware that this must be a simple issue. I am trying my best to understand.

This sets 'mode' to 1 only while digitalRead is HIGH. I need it to set 'mode' to 1 if digitalRead has been high, then to 0 if digitalRead is high and 'mode' is 1.

That code detects a transition. If the transition is to pressed, mode is set to 1. If the transition is to released, mode is set to 0.

What it sounds like you want to do is:

  currState = digitalRead(modePin);
  if (currState != prevState)
  {
    if (currState == HIGH)
    {
      mode = !mode;
    }
  }
   prevState = currState;

This will set mode to 1, 0, 1, 0, ..., each time the switch is pressed.

Perfect, that works great.

Thanks Paul!