Like what:)
Let's not get sidetracked. Finish the job first.
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 == LOW && buttonState3 == LOW && buttonState4 == LOW)
{digitalWrite(relaypin1,HIGH);}
else{
digitalWrite(relaypin1,LOW);
}
if(buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && buttonState4 == LOW)
{ 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);
}
I want buttons 3 and 4 to be able to be pressed simultaneously. But not buttons 1 and 2. If I press button 1 I should not be able to press button 2 and if I press button 2 I should not be able to press button 1
My problem now is that relay 1 and 2 stays on even though I haven't pressed any buttons
Really? You are only telling us that now? After 23 posts? Come on, we can't read your mind. Please post a complete, clear explanation of exactly how the machine is supposed to operate.
Since you held that back, there may be more we need to know. Give us all of it.
Including the schematic.
In my first post, I stated that I don't want the 1st and 2nd relay to be able to be on at the same time. But anyway, I want buttons 3 and 4 to be able to be pressed simultaneously. But not buttons 1 and 2. If I press button 1 I should not be able to press button 2 and if I press button 2 I should not be able to press button 1.
Sorry for the inconvenience, this is the first time I'm using the forum and is very inexperienced.
I'm waiting for updated code and a schematic. Actually, everyone is. It's a forum standard procedure.
...and it's just common sense because nobody can troubleshoot an unknown system.
I just replaced the relays with LED's. Seeing that the program I used to make the schematic doesn't have the relays that I need. I am also using an Arduino Mega 2560 board. I don't know if that will in any way effect my code?
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 == LOW && buttonState3 == LOW && buttonState4 == LOW)
{digitalWrite(relaypin1,HIGH);}
else{
digitalWrite(relaypin1,LOW);
}
if(buttonState1 == LOW && buttonState2 == HIGH && buttonState3 == LOW && buttonState4 == LOW)
{ 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);
}
Please download the pdf and post it as an image, a screen shot is next to useless. I will look at the code.
Yeah, that is so fuzzy I can't tell how button 3 and 4 are connected. Don't answer, please just post it correctly.
At least, it looks like your switches are wired correctly. But that is only on the diagram. Your physical wiring might have errors.
Does it work in Tinkertoy (in simulation)?
1 Input controlling multiple outputs.pdf (7.0 KB)
Sorry bit i cant seem to convert the pdf to an image. It does work on tinkercad in the simulation but I am very certain about my wiring. I followed a keystudio tutorial. But I still wonder if the fact that i am using an arduino mega 2560 has any role to play?
Yet it doesn't work in reality? Are you suggesting there is some black magic or other supernatural cause?
My wiring is exactly the same as that in tinkercad. Thats why I'm on the forum. Something doesn't add up
Sorry I missed your question. The relays are turned on when the button is pressed and turned off when the button is released.
Obviously not! If it was, it would work!
Once you sort the hardware out you might want to tidy up your code a bit.
A tradition is to write relayPin rather than relaypin. The capital on each word helps reading. Also it is best to choose a good name that describes a pin such as buttonPin rather than inPin.
When you initialise many pins that are the same it is often good to use an array. Look at the reference for syntax
This allows you to use a for loop for initialising all the pins
This means you can easily add more relays and more buttons or change their pins
for (int i = 0; i < numButs; i++) {
pinMode (buttonPin [i], input_pullup);
}
Note above not tested I’m on phone and can see errors but can’t be bothered to fix. Just for conceptual purposes
Thanks!
You need to solve that problem. We're sure it's a hardware problem now, it's needed for reference.
BTW
Your schematic shows 9 thru 12, not 8 thru 11.
Is your Arduino powering the relay board ? It should not.
Confirm if the relay board energizes a relay with a HIGH or LOW on an input ?
The diagram doesn't show the relay board. So we can't tell from that, how you have it connected.
Yes everything is powered. In the simulation I am using pins 9 to 12 and in reality I'm using pins 8 thru 11. It energizes ant HIGH

