I am trying to make 5 relays work from 5 switches it works but the problem i'm having is all 5 outputs activate at the same time with any one of the 5 inputs i need each one to work separately. Would appreciate any help with what i'm doing wrong.
Arduino Nano
8 channel relay board
int R1 = 12; //Relay1
int S1 = 6; //Switch1
int R2 = 11; //Relay2
int S2 = 5; //Switch2
int R3 = 10; //Relay3
int S3 = 4; //Switch3
int R4 = 9; //Relay4
int S4 = 3; //Switch4
int R5 = 8; //Relay5
int S5 = 2; //Switch5
int state = HIGH;
int reading;
int previous = LOW;
long time = 0;
long debounce = 200;
void setup() {
pinMode(R1, OUTPUT);
pinMode(S1, INPUT_PULLUP);
pinMode(R2, OUTPUT);
pinMode(S2, INPUT_PULLUP);
pinMode(R3, OUTPUT);
pinMode(S3, INPUT_PULLUP);
pinMode(R4, OUTPUT);
pinMode(S4, INPUT_PULLUP);
pinMode(R5, OUTPUT);
pinMode(S5, INPUT_PULLUP);
}
void loop() {
reading = digitalRead(S1);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(R1, state);
previous = reading;
reading = digitalRead(S2);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(R2, state);
previous = reading;
reading = digitalRead(S3);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(R3, state);
previous = reading;
reading = digitalRead(S4);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(R4, state);
previous = reading;
{ reading = digitalRead(S5);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(R5, state);
previous = reading;
}
}
Latching1.txt (2 KB)