5 Button Marshall DSL40CR Footswtich Controller

Hey folks,
newbie here.

I am building a 5 way footswitch pedal for a Marshall 40CR amplifier using Arduino and a MIDI out.
I have included my code and a schematic(Created with DIYLC software (http://diylc.org)

My code has been made by watching various YouTube like “notesandvolts.com” thanks Dave (LIVE from the Lab: MIDI Program Change - Arduino - YouTube)

I still have a lot to learn. I’m off to a decent start.

The good news is I got the Momentary footswitches to do the following;-

Footswitch 1 sends program change 0 on MIDI chnl 1 (variable button Pin1=2)
Footswitch 2 sends program change 1 on MIDI chnl 1 (variable button Pin2=3)
Footswitch 3 sends program change 2 on MIDI chnl 1 (variable button Pin3=4)
Footswitch 4 sends program change 3 on MIDI chnl 1 (variable button Pin4=5)

Footswitch 5 toggles a CC message (control change 14 data 1 , then data 0) on a second press on MIDI Channel 1 (variable button Pin5=6)

The Program change LED’s need to behave in the following matter. . .
E.G Press footswitch 1, Program Change 0 is sent. LED 1(Green turns on)

However;

Currently when I press footswitch 1, Program Change 0 is sent. LED 1(Green turns on, then off and even flashes for 1/2 a second if held sometimes catches LED off status )

Ideally I would like each program Change LED to go off if another footswitch is pressed, and have the newly pressed program change footswitch to light showing its corresponding status LED on in conjunction with the Amp LEDs.

So basically one LED ON at a time using the 4 Program change buttons.


for the CC 14 Footswitch 5 that doesn’t light the LED on pin 12 at all? it does operate the marshall DSL 40 Amps’ master 1 and master 2 feature using CC 14.

If someone could take a look I’d appreciate it. there is a Marshall footswitch apparently available (PEDL-91016) there aren’t available yet, so I’m working on my own here.

Thanks folks,
Wayne

next post is the code
_

Code for marshall DSL 40CR 5-way momentary footswitch

#include <MIDI.h>
#include "Controller.h"

//Notes & Volts  https://www.youtube.com/watch?v=PlCY_ELYAfs
//control change notes at 46:01: 
// module command at 55:30:

  MIDI_CREATE_DEFAULT_INSTANCE();
  byte patchNum = 0;
  byte cc14Num = 0;

//initialize and declare variables
int buttonPin1 =2;  //push button attached to this pin
int ledPin1 =8; //classic gain GREEN led attached to this pin
int buttonPin2 =3;  //push button attached to this pin
int ledPin2 =9; //classic gain RED led attached to this pin
int buttonPin3 =4;  //push button attached to this pin
int ledPin3 =10; //ULTRA GAIN GREEN led attached to this pin
int buttonPin4 =5;  //push button attached to this pin
int ledPin4 =11; //ULTRA GAIN RED led attached to this pin
int buttonPin5 =6;  //push button attached to this pin
int ledPin5 =12; //CC 14 BLUE led attached to this pin
 
 //this variable tracks the state of the button, low if not pressed, high if pressed
//int ledState = -1; //this variable tracks the state of the LED, negative if off, positive if on
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
int buttonState5 = LOW;
 
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(2, INPUT_PULLUP) ; //Pgm Chg 0
  pinMode(3, INPUT_PULLUP) ; //Pgm Chg 1
  pinMode(3, INPUT_PULLUP) ; //Pgm Chg 2
  pinMode(4, INPUT_PULLUP) ; //Pgm Chg 3
  pinMode(5, INPUT_PULLUP) ; //Pgm Chg 4
  pinMode(6, INPUT_PULLUP) ; //CC14 
  
  MIDI.begin(MIDI_CHANNEL_OMNI) ;
  
//led*********************************  
  pinMode(ledPin1, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin1, INPUT_PULLUP);// initialize the pushbutton pin as an input:
  pinMode(ledPin2, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin2, INPUT_PULLUP);// initialize the pushbutton pin as an input:
  pinMode(ledPin3, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin3, INPUT_PULLUP);// initialize the pushbutton pin as an input:
  pinMode(ledPin4, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin4, INPUT_PULLUP);// initialize the pushbutton pin as an input:
  pinMode(ledPin5, OUTPUT);// initialize the LED pin as an output:
  pinMode(buttonPin5, INPUT_PULLUP);// initialize the pushbutton pin as an input:
}

void loop() {
  /*double == check if this is equal to this( compare)
  //this increases the patch by 1
  // MIDI.sendProgramChange(22,1); //goes to patch 22, on chnl 1, this looks good
  if (digitalRead(3) == LOW && patchNum < 127) {
  patchNum++ ;
  MIDI.sendProgramChange(patchNum,1); //goes to patchNum ++ and increases pgm  by 1, on chnl 1
  delay(200);
  }
  if (digitalRead(2) == LOW && patchNum < 1) {
  patchNum-- ;
  MIDI.sendProgramChange(patchNum,1); //goes to patchNum -- and decreases pgm  by 1, on chnl 1
  delay(200);
  }
}
*/

  if (digitalRead(2) == LOW) {
 // patchNum++;
 MIDI.sendProgramChange(0,1);
  delay(200);
  }
  if (digitalRead(3) == LOW) {
 // patchNum ;
 MIDI.sendProgramChange(1,1);
  delay(200);
  }
  if (digitalRead(4) == LOW) {
  //patchNum ;
 MIDI.sendProgramChange(2,1);
  delay(200);
  }
  if (digitalRead(5) == LOW) {
  //patchNum ;
 MIDI.sendProgramChange(3,1);
  delay(200);
  }
  
  // Control Change *****************************
      if (digitalRead(6) == LOW && cc14Num < 2) {
  cc14Num ++ ;
 MIDI.sendControlChange(14, cc14Num, 1);
  delay(400);
  }
      if (digitalRead(6) == LOW && cc14Num >= 1) {
  cc14Num -- ;
 MIDI.sendControlChange(14, cc14Num, 1);
  delay(400);
  }
//******************************************************
  // LED 1 read the state of the pushbutton value:*****
  buttonState1 = digitalRead(buttonPin1);
    // check if the pushbutton is pressed.
    // if it is, the Button State is HIGH:
  if (buttonState1 == LOW) //toggle the LED State;
  {digitalWrite(ledPin1, !digitalRead(ledPin1));
   delay(250);
  }
  
    // LED 2 read the state of the pushbutton value:*****
  buttonState2 = digitalRead(buttonPin2);
    // check if the pushbutton is pressed.
    // if it is, the Button State is HIGH:
  if (buttonState2 == LOW) {
    //toggle the LED State;
  digitalWrite(ledPin2, !digitalRead(ledPin2));
      //debounce:
  delay(250);
  }
     // LED 3 read the state of the pushbutton value:*****
  buttonState3 = digitalRead(buttonPin3);
    // check if the pushbutton is pressed.
    // if it is, the Button State is HIGH:
  if (buttonState3 == LOW) {
    //toggle the LED State;
  digitalWrite(ledPin3, !digitalRead(ledPin3));
      //debounce:
  delay(250);
  }
     // LED 4 read the state of the pushbutton value:*****
  buttonState4 = digitalRead(buttonPin4);
    // check if the pushbutton is pressed.
    // if it is, the Button State is HIGH:
  if (buttonState4 == LOW) {
    //toggle the LED State;
  digitalWrite(ledPin4, !digitalRead(ledPin4));
      //debounce:
  delay(250);
  //CC14 LED *******************************************
       // LED 5 read the state of the pushbutton value:*****
  buttonState5 = digitalRead(buttonPin5);
    // check if the pushbutton is pressed.
    // if it is, the Button State is HIGH:
  if (buttonState5 == LOW) {
    //toggle the LED State;
  digitalWrite(ledPin5, !digitalRead(ledPin5));
      //debounce:
  delay(250);
  }
}
}
//*************
 /* buttonState1 = digitalRead(buttonPin1);  // read current states of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);  // read current states of the pushbutton value:
  buttonState3 = digitalRead(buttonPin3);  // read current states of the pushbutton value:
  buttonState4 = digitalRead(buttonPin4);  // read current states of the pushbutton value:
*/


// check if the pushbutton is pressed buttonState# == HIGH/LOW
// if pressed change buttonState == HIGH to turn on ledPin# 
// else if buttonState == LOW then digitalWrite(ledPin#, LOW) Keeps Led off.


/*
if (buttonState1 == LOW) {    //check buttonState
digitalWrite(ledPin1, HIGH);  //if HIGH turn LED on:
} else {
digitalWrite(ledPin1, LOW);   // turn LED off:
}
Serial.println(buttonState1); //Print buttonState to serial
if (buttonState2 == LOW) {   //check buttonState
digitalWrite(ledPin2, HIGH);  //if HIGH turn LED on:
} else {
digitalWrite(ledPin2, LOW);   // turn LED off:
delay(10);
}
Serial.println(buttonState2); //Print buttonState to serial
if (buttonState3 == LOW) {   //check buttonState
digitalWrite(ledPin3, HIGH);  //if HIGH turn LED on:
} else {
digitalWrite(ledPin3, LOW);   // turn LED off:
delay(10);
Serial.println(buttonState3); //Print buttonState to serial
}
if (buttonState4 == LOW) {   //check buttonState
digitalWrite(ledPin4, HIGH);  //if HIGH turn LED on:
} else {
digitalWrite(ledPin4, LOW);   // turn LED off:
delay(10);
Serial.println(buttonState4); //Print buttonState to serial
}
*/
#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE();

const uint8_t patchButtonPins[] = {2, 3, 4, 5};
const uint8_t patchLEDPins[] = {8, 9, 10, 11};
const uint8_t patches[] = {0, 1, 2, 3};

const size_t numberOfButtons = sizeof(patchButtonPins) / sizeof(patchButtonPins[0]);
const size_t numberOfLEDs = sizeof(patchLEDPins) / sizeof(patchLEDPins[0]);
const size_t numberOfPatches = sizeof(patches) / sizeof(patches[0]);

static_assert(numberOfButtons == numberOfLEDs, "The number of buttons doesn't match the number of LEDs");
static_assert(numberOfButtons == numberOfPatches, "The number of buttons doesn't match the number of patches");

const uint8_t CCButtonPin = 6;
const uint8_t CCLEDPin = 12;
const uint8_t controller = 14;
const uint8_t ccValueOff = 0;
const uint8_t ccValueOn = 1;

const uint8_t channel = 1;
 
void setup() {
  for (const uint8_t &pin : patchButtonPins)
    pinMode(pin, INPUT_PULLUP);
  pinMode(CCButtonPin, INPUT_PULLUP);
  for (const uint8_t &pin : patchLEDPins)
    pinMode(pin, OUTPUT);
  pinMode(CCLEDPin, OUTPUT);

  MIDI.begin(MIDI_CHANNEL_OMNI) ;
}

int8_t getPressedPatchButton() {
  for (int8_t i = 0; i < numberOfButtons; i++)
    if (digitalRead(patchButtonPins[i]) == LOW)
      return i;
  return -1;
}

void setPatchLED(int8_t led, bool state) {
  if (led >= 0) 
    digitalWrite(patchLEDPins[led], state);
}

void updatePatchSelect() {
  static int8_t activeSelection = -1;
  int8_t pressedButton = getPressedPatchButton();
  if (pressedButton >= 0 && pressedButton != activeSelection) {
    setPatchLED(activeSelection, LOW);
    activeSelection = pressedButton;
    setPatchLED(activeSelection, HIGH);
    MIDI.sendProgramChange(patches[activeSelection], channel);
  }
}

bool controllerButtonIsPressed() { // Read and debounce button, return true on falling edge
  const static unsigned long debounceTime = 25;
  const static int8_t rising = HIGH - LOW;
  const static int8_t falling = LOW - HIGH;
  static bool previousState = HIGH;
  static unsigned long previousBounceTime = 0;
  bool pressed = false;
  bool state = digitalRead(CCButtonPin);               // read the button's state
  int8_t stateChange = state - previousState;  // calculate the state change since last time

  if (stateChange == falling) { // If the button is pressed (went from high to low)
    if (millis() - previousBounceTime > debounceTime) { // check if the time since the last bounce is higher than the threshold
      pressed = true; // the button is pressed
    }
  } else if (stateChange == rising) { // if the button is released or bounces
    previousBounceTime = millis();      // remember when this happened
  }

  previousState = state; // remember the current state
  return pressed; // return true if the button was pressed and didn't bounce
}

void updateController() {
  static bool controllerState = false;
  if (controllerButtonIsPressed()) {
    controllerState = !controllerState;
    digitalWrite(CCLEDPin, controllerState);
    MIDI.sendControlChange(controller, controllerState ? ccValueOn : ccValueOff, channel);
  }
}

void loop() {
  updatePatchSelect();
  updateController();
}