2 buttons controlling 1 relay

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);

}

Schematic please.


Uploading: 20220915_193754[1].jpg...

Does "go on" mean "go on and then go off when the button is released", or "go on and stay on"?

Where is the schematic? I see only photos.

it means to go on and then goes off. Which is what I want it to do.

Sorry for the vague explanation:)

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

Extending your own logic:

if(buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && buttonState4 == LOW)
  digitalWrite(relaypin1,HIGH);
else
 digitalWrite(relaypin1,LOW);

which will only activate relay 1 if button 1 is HIGH, all the others are LOW.

Always start your projects by making a good schematic.

Let’s see the bottom of the switch showing the leads.

When dealing with switches, look at when a switch changes state not what state the switch is in.

Thanks for your suggestion. But this keeps relay one on even though the button isn't pressed. Which isn't what I want. But thanks anyway

It shouldn't. Why would it do that?

No idea. But that is whats happening

You're commenting on code you haven't posted. How can I guess what you wrote?

If I press all the other buttons simultaneously, then the first relay turns off.

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);  

Serial.begin(9600);

}
void loop()
{
int buttonState1 = digitalRead(inpin1);
int buttonState2 = digitalRead(inpin2);
int buttonState3 = digitalRead(inpin3);
int buttonState4 = digitalRead(inpin4);

if(buttonState1 == HIGH && buttonState2 == LOW && buttonState3 == LOW && buttonState4 == LOW)
  {digitalWrite(relaypin1,HIGH);}
else{
 digitalWrite(relaypin1,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);

}

You only changed one clause. You are supposed to take the idea and think about what it does, and apply the logic to your entire sketch.

okay I'll try

As your learning progresses, you’ll realise there are much more elegant ways to do this.

Please, please make up your mind whether you want to pretty it up with brackets. Normally you would either

digitalWrite(relaypin2,HIGH);

or

{
  digitalWrite(relaypin2,HIGH);
}

otherwise it just looks fugly.