Hi guys. I'm trying to control 4 relays with 4 buttons. I want to be able to press the first button then the first relay should go on. I want to press the second button and the second relay should go on etc. But I also don't want the first and second relays to be on at the same time. Thus, if I press the first and second button just the first relay should go on. But that doesn't happen. When I press the second button both the first and second relays turn on, which I don't want.
Can someone help me, please?
int relaypin1 = 8;
int relaypin2 = 9;
int relaypin3 = 10;
int relaypin4 = 11;
int inpin1 = 2;
int inpin2 = 3;
int inpin3 = 4;
int inpin4 = 5;
void setup()
{
pinMode(relaypin1,OUTPUT);
pinMode(relaypin2,OUTPUT);
pinMode(relaypin3,OUTPUT);
pinMode(relaypin4,OUTPUT);
pinMode(inpin1,INPUT_PULLUP);
pinMode(inpin2,INPUT_PULLUP);
pinMode(inpin3,INPUT_PULLUP);
pinMode(inpin4,INPUT_PULLUP);
}
void loop()
{
int buttonState1 = digitalRead(inpin1);
int buttonState2 = digitalRead(inpin2);
int buttonState3 = digitalRead(inpin3);
int buttonState4 = digitalRead(inpin4);
if(buttonState1 == HIGH && buttonState2 == HIGH)
{ digitalWrite(relaypin1,HIGH);
digitalWrite(relaypin2,LOW);
}
else
{ digitalWrite(relaypin1,LOW);
digitalWrite(relaypin2,LOW);
}
if(buttonState2 == HIGH)
{ digitalWrite(relaypin2,HIGH);}
else
{ digitalWrite(relaypin2,LOW);}
if(buttonState3 == HIGH)
{ digitalWrite(relaypin3,HIGH);}
else
{ digitalWrite(relaypin3,LOW);}
if(buttonState4 == HIGH)
{ digitalWrite(relaypin4,HIGH);}
else
{ digitalWrite(relaypin4,LOW);}
delay(50);
}
A schematic is a diagram with symbols to represent your wiring. Look at easyeda and kicad for online free examples. Otherwise draw on paper and take a photo.
You need to clearly define your projects states and the conditions that lead to a change in state. It must not be ambiguous as code can not leave room for ambiguity
Are you turning the relays on only while the button is held down or is the button acting as a switch press on press off