Newbie question! need help with simple code for a guitarpedal switchingsystem

Newbie question! need help with simple code for a guitarpedal switchingsystem

Hi!

I am new to the arduino forum and asking for some help with a new project I have started a month ago. I am still new with the arduino coding, so bare with me :wink:

The gadget that I want to build is a guitarpedal switchingsystem. Easily explained the arduino takes in information from trigger buttons and opens diffrent outputports, sending electrical current to relays. When a relay is on it lets the guitarsignal go through a guitarpedal, this is the basic idea!. For future development I want to be able to receive and send midi, change amp channels.

My theory has been that I would be able to solve it with an “if-statement” and a “switch case-statement”. The “if-statement” would work so that depending on which button is pushed it would change an integer value. The “switch case-statements” would then have a case of open/closed ports for each value that the integer would give it. But for some reason I don’t get it to work and I believe it is and programming error. Any feedback is welcome!

Here is my code:
(The ports that control the relays are namned after lights)

//portnumbers for the lights 

const int lightRed = 13;
const int lightBlue = 12;
const int lightYellow = 11;

//portnumber to buttons 

const int switchButton1 = 2;
const int switchButton2 = 3;
int patchnumber = 0;

void setup() {

pinMode(lightRed, OUTPUT);
pinMode(lightBlue, OUTPUT);
pinMode(lightYellow, OUTPUT);
pinMode(switchButton1, INPUT);
pinMode(switchButton2, INPUT);
digitalWrite(lightRed, LOW);
digitalWrite(lightBlue, LOW);
digitalWrite(lightYellow, LOW);

}
//if-statement
void loop() {
int state1 = digitalRead(switchButton1);
if(state1 == HIGH){
patchnumber = 1;
}
int state2 = digitalRead(switchButton2);
if(state2 == HIGH){
patchnumber = 2;
}

//switch-case satement


switch (patchnumber) {

case 0:
digitalWrite(lightRed, HIGH);
digitalWrite(lightBlue, LOW);
digitalWrite(lightYellow, LOW);

break;

case 1:
digitalWrite(lightRed, HIGH);
digitalWrite(lightBlue, LOW);
digitalWrite(lightYellow, LOW);

break;

case 2:
digitalWrite(lightRed, LOW);
digitalWrite(lightBlue, LOW);
digitalWrite(lightYellow, HIGH);

break;
}
}

The code you posted does something. You haven't defined what it actually does. Without knowing what it actually does, we can't determine how that differs from what you want. So, we can't tell you how to change it to do what you want.

You haven't told us anything about how the switches are wired, so we can not conclude that they are wired correctly.

Hi!

I am away from my arduino right now and it's hard to tell when I will see it next. I sketched a picture of what the wiring would look like in real life. For prototyping purposes I used diodes instead of real relays. What happens now when I push a button is that only one diod lights up and when I lift my finger off the button the diod turns off.

The point was that one or multiple diods/relays would be engaged at the same time and would not die when one lifts the finger off the button.

Is this a programming fault, wiring fault or component fault?

You're on the right track with this, but the way you're reading the digital inputs is incorrect.

int state1 = digitalRead(switchButton1);
if(state1 == HIGH){
patchnumber = 1;
}

should be:

if (digitalRead(switchButton1) == HIGH) {
  patchnumber = 1;
}

You also need a way to set patchNumber back to zero.

There is nothing wrong with reading the state of a button, putting the state a variable then testing the value of the variable. In fact it can be useful if the value is needed again in the code.

Only one LED is lighting when you press a button because that is what you tell it to do

case 1:
digitalWrite(lightRed, HIGH);
digitalWrite(lightBlue, LOW);
digitalWrite(lightYellow, LOW);

Only one pin is set HIGH so only that LED lights. It goes off when you release the button because the condition that makes it light
patchnumber == 1is no longer true. You need to look at switching the state of a variable each time the button is pressed. Look at the StateChangeDetection example in the IDE to see how it can be done.