I've got a project where I have six momentary buttons where each press turns on / off a corresponding relay depending on its current state. I've done quite a bit of forum research to figure out where I'm going wrong, but I'm stumped at this point.
It works, but it's incredibly slow / non-responsive at times. Can anyone tell me where I'm going wrong?
I've attached my layout and my code. For modeling purposes, the relays have been replaced with LEDs.
// Constants:
#define RELAY2 2 // the pins the relays are attached to
#define RELAY3 3
#define RELAY4 4
#define RELAY5 5
#define RELAY6 6
#define RELAY7 7
#define SwitchPin8 8 // the pins that the switches are attached to
#define SwitchPin9 9
#define SwitchPin10 10
#define SwitchPin11 11
#define SwitchPin12 12
#define SwitchPin13 13
// Variables:
int RelayState2 = 0;
int RelayState3 = 0;
int RelayState4 = 0;
int RelayState5 = 0;
int RelayState6 = 0;
int RelayState7 = 0;
int Button8 = 0; //This is the default "Button Off" and 1 is "Button On"
int Button9 = 0;
int Button10 = 0;
int Button11 = 0;
int Button12 = 0;
int Button13 = 0;
int OldButton8 = 0; //For debouncing purposes
int OldButton9 = 0;
int OldButton10 = 0;
int OldButton11 = 0;
int OldButton12 = 0;
int OldButton13 = 0;
int OldButton14 = 0;
digitalWrite(RELAY2, HIGH); //This is so all of the relays start as OFF
digitalWrite(RELAY3, HIGH);
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
digitalWrite(RELAY6, HIGH);
digitalWrite(RELAY7, HIGH);
void setup() {
pinMode(RELAY2, OUTPUT); //Pin 2 is an Output matched with button 8
pinMode(RELAY3, OUTPUT); //Pin 3 is an Output matched with button 9
pinMode(RELAY4, OUTPUT); //Pin 4 is an Output matched with button 10
pinMode(RELAY5, OUTPUT); //Pin 5 is an Output matched with button 11
pinMode(RELAY6, OUTPUT); //Pin 6 is an Output matched with button 12
pinMode(RELAY7, OUTPUT); //Pin 7 is an Output matched with button 13
pinMode(8, INPUT); //Pin 8 is an Input matched with relay 2
pinMode(9, INPUT); //Pin 9 is an Input matched with relay 3
pinMode(10, INPUT); //Pin 10 is an Input matched with relay 4
pinMode(11, INPUT); //Pin 11 is an Input matched with relay 5
pinMode(12, INPUT); //Pin 12 is an Input matched with relay 6
pinMode(13, INPUT); //Pin 13 is an Input matched with relay 7
}
void loop() {
if(digitalRead(SwitchPin8) == HIGH) {
Button8 = 1 - Button8;
}
if(Button8 == 1 && OldButton8 == 0) {
digitalWrite(RELAY2, LOW);
delay(500);
}
if(Button8 == 0 && OldButton8 == 1) {
digitalWrite(RELAY2, HIGH);
delay(500);
}
OldButton8 = Button8;
if(digitalRead(SwitchPin9) == HIGH); {
Button9 = 1 - Button9;
}
if(Button9 == 1 && OldButton9 == 0) {
digitalWrite(RELAY3, LOW);
delay(500);
}
if(Button9 == 0 && OldButton9 == 1) {
digitalWrite(RELAY3, HIGH);
delay(500);
}
OldButton9 = Button9;
if(digitalRead(SwitchPin10) == HIGH) {
Button10 = 1 - Button10;
}
if(Button10 == 1 && OldButton10 == 0) {
digitalWrite(RELAY4, LOW);
delay(500);
}
if(Button10 == 0 && OldButton10 == 1) {
digitalWrite(RELAY4, HIGH);
delay(500);
}
OldButton10 = Button10;
if(digitalRead(SwitchPin11) == HIGH) {
Button11 = 1 - Button11;
}
if(Button11 == 1 && OldButton11 == 0) {
digitalWrite(RELAY5, LOW);
delay(500);
}
if(Button11 == 0 && OldButton11 == 1) {
digitalWrite(RELAY5, HIGH);
delay(500);
}
OldButton11 = Button11;
if(digitalRead(SwitchPin12) == HIGH) {
Button12 = 1 - Button12;
}
if(Button12 == 1 && OldButton12 == 0) {
digitalWrite(RELAY7, LOW);
delay(500);
}
if(Button12 == 0 && OldButton12 == 1) {
digitalWrite(RELAY6, HIGH);
delay(500);
}
OldButton12 = Button12;
if(digitalRead(SwitchPin13) == HIGH) {
Button13 = 1 - Button13;
}
if(Button13 == 1 && OldButton13 == 0) {
digitalWrite(RELAY7, LOW);
delay(500);
}
if(Button13 == 0 && OldButton13 == 1) {
digitalWrite(RELAY7, HIGH);
delay(500);
}
OldButton13 = Button13;
}
Thanks very much for any insight
