I updated the code to have an initial startup so that CH1 is activated and the others are deactivated. Preventing all relays from being active on power up. I changed the digital output pins to 12, 11, 10 & 9 so that when the unit boots relay 1 isn't clicked on and off a few times. On boot up it seems that the LED attached to pin 13 blinks a few times which subsequently clicks the relay. I also added more comments.
I'll try the de bouncing stuff later. For now proof of concept is adequate.
Thanks for all the help. It's most appreciated. 8)
// Guitar amp relay module driven my microcontroller input
// 2018 - Eric Carlson - ECS Audio
int relay1 = 12; //Amp CH1 relay
int relay2 = 11; //Amp CH2 relay
int relay3 = 10; //Amp CH3 relay
int relay4 = 9; //Amp CH4 relay
int button1 = 2; //CH1 Button
int button2 = 3; //CH2 Button
int button3 = 4; //CH3 Button
int button4 = 5; //CH4 Button
void setup()
{
// Initialize digital output pins for relays:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Initialize digital inputs with internal pullup resistors:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
//Set the initial startup to CH1:
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
void loop()
{
//CH1 EN
bool button1State ;
button1State = digitalRead(button1);
if(button1State == LOW)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
//CH2 EN
bool button2State ;
button2State = digitalRead(button2);
if(button2State == LOW)
{
digitalWrite(relay2, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
//CH3 EN
bool button3State ;
button3State = digitalRead(button3);
if(button3State == LOW)
{
digitalWrite(relay3, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay4, HIGH);
}
//CH4 EN
bool button4State ;
button4State = digitalRead(button4);
if(button4State == LOW)
{
digitalWrite(relay4, LOW);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
}
}