Footswitch with 9 channels and using 4 banks

Hello everyone, this is my first post in this forum and also my first arduino project, a couple of months ago I was interested in making a footswitch to make changes in tones and pedals in bias fx and decided to buy an arduino and use previous electronic material that I had around, so far so good and guiding me with a couple of videos I have managed to make the physical operation that I would like to make my pedalboard, although I still need to see how I can implement the midi library in my code.
My question here is if someone can help me with an error that does not affect the operation of my pedal but I want to understand why it happens, according to me my design should start on channel 1 but when I connect it sends me to two, it is until I restart the arduino that starts on one, anyone has a solution or any idea of what I'm doing wrong?
Thank you very much in advance, I attach the code and a video :slight_smile:

//Pulsadores 
#define canalup 2
#define banco1 10
#define banco2 11
#define banco3 12 
#define banco4 A0

//Display de 7 segmentos 
#define displayA 3
#define displayB 4
#define displayC 5
#define displayD 6
#define displayE 7
#define displayF 8
#define displayG 9

//Led
#define led1 A1
#define led2 A2
#define led3 A3 
#define led4 A4

int contador = 1;

void setup(){
  Serial.begin(9600);
  Serial.println(contador);
 
  pinMode(canalup,INPUT);
  pinMode(banco1,INPUT);
  pinMode(banco2,INPUT);
  pinMode(banco3,INPUT);
  pinMode(banco4,INPUT);
  
  pinMode(displayA,OUTPUT);
  pinMode(displayB,OUTPUT);
  pinMode(displayC,OUTPUT);
  pinMode(displayD,OUTPUT);
  pinMode(displayE,OUTPUT);
  pinMode(displayF,OUTPUT);
  pinMode(displayG,OUTPUT);
  
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  
}

void loop(){
int estado = digitalRead(canalup);
if(estado == 0){
  while(digitalRead(canalup) == 0){
  }
  contador++;
  if (contador>9){
    contador=1;}
  Serial.println(contador);
}

switch (contador){
  case 1:
      uno();
      break;  
  case 2:
      dos();
      break; 
  case 3:
      tres();
      break;    
  case 4:
      cuatro();
      break;            
  case 5:
      cinco();
      break;
  case 6:
      seis();
      break;
  case 7:
      siete();
      break; 
  case 8:
      ocho();
      break;
  case 9:
      nueve();
      break;                         
  }
}

////////////////////////////////////////////////////
void uno(){
  digitalWrite(displayA,LOW);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,LOW);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,LOW);
  digitalWrite(displayG,LOW);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void dos(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,LOW);
  digitalWrite(displayD,HIGH);
  digitalWrite(displayE,HIGH);
  digitalWrite(displayF,LOW);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void tres(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,HIGH);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,LOW);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void cuatro(){
  digitalWrite(displayA,LOW);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,LOW);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,HIGH);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void cinco(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,LOW);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,HIGH);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,HIGH);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void seis(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,LOW);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,HIGH);
  digitalWrite(displayE,HIGH);
  digitalWrite(displayF,HIGH);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void siete(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,LOW);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,LOW);
  digitalWrite(displayG,LOW);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

////////////////////////////////////////////////////
void ocho(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,HIGH);
  digitalWrite(displayE,HIGH);
  digitalWrite(displayF,HIGH);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

void nueve(){
  digitalWrite(displayA,HIGH);
  digitalWrite(displayB,HIGH);
  digitalWrite(displayC,HIGH);
  digitalWrite(displayD,LOW);
  digitalWrite(displayE,LOW);
  digitalWrite(displayF,HIGH);
  digitalWrite(displayG,HIGH);

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);
  
if(lectura1 == 0){
  while(digitalRead(banco1) == 0){
  }
  digitalWrite(led1,HIGH);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura2 == 0){
  while(digitalRead(banco2) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,HIGH);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW);
}

if(lectura3 == 0){
  while(digitalRead(banco3) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,HIGH);
  digitalWrite(led4,LOW);
}

if(lectura4 == 0){
  while(digitalRead(banco4) == 0){
  }
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,HIGH);
}

 if(estado == 0){
  digitalWrite(led1,LOW);
  digitalWrite(led2,LOW);
  digitalWrite(led3,LOW);
  digitalWrite(led4,LOW); 
}
}

That is a really good attempt for your first project! most beginner projects are hard to read but you have organised your code quite well.

Interesting problem, it could be that, for some reason, the input is being pulled low on your canalup pin. The only way you're cantador value increments is when canalup is LOW, so it could be some noise or something else that only occurs when it is initially powered via USB.

I've had a look at your code and added some new functions to reduce the overall size, could you tell me if it works?

//Pulsadores
#define canalup 2
#define banco1 10
#define banco2 11
#define banco3 12
#define banco4 A0

//Display de 7 segmentos
#define displayA 3
#define displayB 4
#define displayC 5
#define displayD 6
#define displayE 7
#define displayF 8
#define displayG 9

//Led
#define led1 A1
#define led2 A2
#define led3 A3
#define led4 A4

//A two-dimensional seven segment display (SSD) map array
const bool SSDMap[9][7] = {
  {0,1,1,0,0,0,0}, //uno
  {1,1,0,1,1,0,1}, //dos
  {1,1,1,1,0,0,1}, //tres
  {0,1,1,0,0,1,1}, //cuatro
  {1,0,1,1,0,1,1}, //cinco
  {1,0,1,1,1,1,1}, //seis
  {1,1,1,0,0,0,0}, //siete
  {1,1,1,1,1,1,1}, //ocho
  {1,1,1,0,0,1,1}  //nueve
};

int contador = 1;

void setup() {
  Serial.begin(9600);
  Serial.println(contador);

  pinMode(canalup, INPUT);
  pinMode(banco1, INPUT);
  pinMode(banco2, INPUT);
  pinMode(banco3, INPUT);
  pinMode(banco4, INPUT);

  pinMode(displayA, OUTPUT);
  pinMode(displayB, OUTPUT);
  pinMode(displayC, OUTPUT);
  pinMode(displayD, OUTPUT);
  pinMode(displayE, OUTPUT);
  pinMode(displayF, OUTPUT);
  pinMode(displayG, OUTPUT);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

  delay(1000); //  ------  This might fix the problem  ------
  
}

void loop() {
  
  if (digitalRead(canalup) == 0) {
    while (digitalRead(canalup) == 0) {
    }
    contador++;
    if (contador > 9) {
      contador = 1;
    }
    Serial.println(contador);
  }

  number(contador);
  
}

void number(int value)
{
  int index = value - 1;  //Need to take 1 away from 'value' as arrays start at 0
  digitalWrite(displayA, SSDMap[index][0]); 
  digitalWrite(displayB, SSDMap[index][1]);
  digitalWrite(displayC, SSDMap[index][2]);
  digitalWrite(displayD, SSDMap[index][3]);
  digitalWrite(displayE, SSDMap[index][4]);
  digitalWrite(displayF, SSDMap[index][5]);
  digitalWrite(displayG, SSDMap[index][6]);

  changeLeds();  
}

void changeLeds() {

  int lectura1 = digitalRead(banco1);
  int lectura2 = digitalRead(banco2);
  int lectura3 = digitalRead(banco3);
  int lectura4 = digitalRead(banco4);
  int estado = digitalRead(canalup);

  if (lectura1 == 0) {
    while (digitalRead(banco1) == 0) {
    }
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }

  if (lectura2 == 0) {
    while (digitalRead(banco2) == 0) {
    }
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }

  if (lectura3 == 0) {
    while (digitalRead(banco3) == 0) {
    }
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
  }

  if (lectura4 == 0) {
    while (digitalRead(banco4) == 0) {
    }
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }

  if (estado == 0) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
}

To remove the 9 functions uno(), dos(), tres(), ..., I've created a 2D boolean array at the top of the file that tells the Arduino how to light up your seven-segment display. This way, you only need one function which I've created called number()

1 Like

To use channel 1 you have to send channel zero in the MIDI messages, if you send channel 1 the outside world will see it as channel 2.

1 Like

Thank you very much for taking the trouble to take a look at my code, it has helped me a lot the code that you have given me basically simplifies a lot the code that I originally designed and corrected the problem I had, I changed the capacitor of the button arrangement and now it works great, I have a question if it is not too much trouble, I was currently working with an arduino nano, but when trying to use the USB midi library throws an error from what I read this does not support it, is there another way in which I can implement the operation of my program as this but use the midi? , thank you very much in advance.
=)

Thank you very much for the feedback, I added a 0 initialization in my base code but I wanted to hear more options to discover new ways to do it, thanks to bradley's solution now it goes perfect, thanks a lot to both of you :slight_smile:

1 Like

No problem! what do you mean it throws an error? is there a specifc library that you try to add to your code that doesn't work?

could you show us a screenshot of the error?

Yes it will on a Nano, you need a Micro or a Leonardo to make the USB midi library work.

Thank you both very much again, this is the capture. And from what I read I also can only be done with an arduino leonardo, I saw out there that there were a couple of people who managed to leave an arduino one as midi (Hiduino) but to my knowledge seems to be that only allows you to make the midi control with buttons, but I do not know if I can implement that with my code and work at par, you think I should pause this a couple of days in what I do with a leonardo?

No with Hiduino you can make any MIDI device. But a word of warning, first off this only works with a genuine Arduino Uno, and then only one that has a 8U2 chip to act as the serial to USB interface.
Then you reprogram that along with your code. But before you can change that code and try it again, you have to burn the bootloader back into this chip before you can load new code. This makes the development process a PITA.

A Leonardo or better Micro is the way to go, that uses a 16U2 chip and there is enough room in that to hold both the normal bootloader and your USB / MIDI interface.

1 Like

Thank you very much, I think I will suspend this project for a couple of days so I can get another arduino, just two questions when you spoke of arduino micro you meant this one? Arduino Micro | Arduino Official Store and finally I have seen several leonardo arduinos that say rev3 works or I look for the one that does not have it.
Thanks again Grumpy.

Yes, although you can also get the Sparkfun micro Pro and other clones.

Sorry I don’t know what you mean here. The main thing is to look for the processor type which is the 32U4.