A little problem

Hello.

I have got a little problem with my code.
When Arduino Mega 2560 starts the light2 is ON when it should be OFF and
I can turn on light1 only when light2 is OFF and I can turn off light1 only when light2 is ON.
With light2 is the opposite:
I can turn on light2 only when light1 is ON and turn off light2 when light1 is OFF.
Tactile switches are connected to the ground and pins 50 and 51.

// constants won't change. They're used here to
// set pin numbers:
const int button1 = 50; // the number of the pushbutton pin
const int button2 = 51; // the number of the pushbutton pin

const int light1 = 24; // the number of the LED pin
const int light2 = 25; // the number of the LED pin

void setup() {
// initialize the LED pin as an output:
pinMode(light1, OUTPUT);
pinMode(light2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);

}
boolean state1=0;
boolean state2=0;

void loop(){

if(digitalRead(button1) == HIGH)
{
delay(20);
state1=!state1;
digitalWrite (light1, state1);
while (digitalRead(button1) == HIGH);
delay(20);
}

{
if(digitalRead(button2) == HIGH)
{
delay(20);
state2=!state2;
digitalWrite (light2, state2);
while (digitalRead(button2) == HIGH);
delay(20);
}
}}

Try this
Immediately after the pinMode line set the output low with a digital write.

I have added that but still there is the same problem on light2

void setup() {
  // initialize the LED pin as an output:
  pinMode(light1, OUTPUT); 
  pinMode(light2, OUTPUT); 
  digitalWrite(light1, LOW);
  digitalWrite(light2, LOW);  
  // initialize the pushbutton pin as an input:
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  
  
}

boolean state1=0; 
boolean state2=0;

When I changed this to

digitalWrite(light1, HIGH);
digitalWrite(light2, HIGH);

the relays are off...

Ok look at your wiring. The code looks OK appart from the braces round the second if statement and the fact that lack of code tags has turned part of the linking into italics so that might hide a fault.

hey what relays?
How are you driving them.
It is important to describe your hardware whe. Asking for help.

I have got 4 module relay but only 2 modules are connected.
light1 = relay module 1
light2 = relay module 2

Wiring looks fine to me
GND to GND
5V to VCC
and
IN1 = PIN 50
IN2= PIN51

// constants won't change. They're used here to
// set pin numbers:
const int button1 = 50; // the number of the pushbutton pin
const int button2 = 51; // the number of the pushbutton pin

const int light1 = 24; // the number of the LED pin
const int light2 = 25; // the number of the LED pin

void setup() {
// initialize the LED pin as an output:
pinMode(light1, OUTPUT);
pinMode(light2, OUTPUT);
digitalWrite(light1, HIGH);
digitalWrite(light2, HIGH);
// initialize the pushbutton pin as an input:
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);

}

boolean state1=0;
boolean state2=0;

void loop(){

if(digitalRead(button1) == HIGH)
{
delay(20);
state1=!state1;
digitalWrite (light1, state1);
while (digitalRead(button1) == HIGH);
delay(20);
}

{
if(digitalRead(button2) == HIGH)
{
delay(20);
state2=!state2;
digitalWrite (light2, state2);
while (digitalRead(button2) == HIGH);
delay(20);
}
}}

Please stop using the quote icon when posting code use the hash next to it.
What relay module?

Songle

Each of the code by itself works fine but two of them together cause problems :frowning:

if(digitalRead(button1) == HIGH)
  {
    delay(20);
    state1=!state1;
    digitalWrite (light1, state1);
    while (digitalRead(button1) == HIGH);
    delay(20); 
  }
if(digitalRead(button2) == HIGH)
  {
    delay(20);
    state2=!state2;
    digitalWrite (light2, state2);
    while (digitalRead(button2) == HIGH);
    delay(20);
    }

Now I think it is time for you to read the how to use this forum stick post at the top.
It will tell you all about posing questions and the information you have to provide.
Why have you not removed the superfluous braces around the second if like I said?

// constants won't change. They're used here to 
// set pin numbers:
const int button1 = 50;     // the number of the pushbutton pin
const int button2 = 51;     // the number of the pushbutton pin

const int light1 =  24;      // the number of the LED pin
const int light2 =  25;      // the number of the LED pin



void setup() {
  // initialize the LED pin as an output:
  pinMode(light1, OUTPUT);
  pinMode(light2, OUTPUT);
  digitalWrite(light1, HIGH);
  digitalWrite(light2, HIGH); 
  // initialize the pushbutton pin as an input:
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  
  
}

boolean state1=0;
boolean state2=0;

void loop(){
  
 if(digitalRead(button1) == HIGH)
  {
    delay(20);
    state1=!state1;
    digitalWrite (light1, state1);
    while (digitalRead(button1) == HIGH);
    delay(20);
    }
       
 if(digitalRead(button2) == HIGH)
  {
    delay(20);
    state2=!state2;
    digitalWrite (light2, state2);
    while (digitalRead(button2) == HIGH);
    delay(20);
    }
}

Songle

Wow how useful! I am trying to help you, please try to help me help you. A link to the data sheet would have been appropriate here.

Each of the code by itself works fine but two of them together cause problems

There is nothing I can see wrong with that code. Therefore I think the problem is that the way it is actually wired up is not the way you think it is wired up.

Just click on those photos you posted. They are too big to get much sense out of. And any way they just show a bunch of wires going into a board, they do not show where the wires go to.

How are the lights wired to the relay? Schematic backed up by a clear photo showing the wiring not more than 1000 pixels wide would fit the bill here.

you should consider detecting a state change on either of the two switches;

you can use code like this. It compiles but not tested:

const int button1 = 50;     // the number of the pushbutton pin
const int button2 = 51;     // the number of the pushbutton pin
const int light1 =  24;      // the number of the LED pin
const int light2 =  25;      // the number of the LED pin
boolean state1=0; 
boolean state2=0; 
int lastButtonState1;
int lastButtonState2;

void setup() 
{
  pinMode(light1, OUTPUT); 
  pinMode(light2, OUTPUT);   
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
}

void loop()
{
  int buttonState1 = digitalRead(button1);
  if(buttonState1 == LOW && lastButtonState1 == HIGH)
  {
    state1=!state1;
    digitalWrite (light1, state1);
  }
  lastButtonState1 = buttonState1; 
  int buttonState2 = digitalRead(button2);
  if(buttonState2 == LOW && lastButtonState2 == HIGH)
  {
    state2=!state2;
    digitalWrite (light2, state2);
  }
  lastButtonState2 = buttonState2;
  delay(50)// a little debounce you may need to tweak
}

it should allow you to switch either relay regardless of the state that the buttonPin of the other is in.

Thank you Grumpy_Mike and BulldogLowell.

The code works only one
; was missing at the end.

I have added

digitalWrite(light1, HIGH);
digitalWrite(light2, HIGH);

so my relays are off at the beginning.

Here is the working code for anyone.

const int button1 = 50;     // the number of the pushbutton pin
const int button2 = 51;     // the number of the pushbutton pin
const int light1 =  24;      // the number of the LED pin
const int light2 =  25;      // the number of the LED pin
boolean state1=0; 
boolean state2=0; 
int lastButtonState1;
int lastButtonState2;

void setup() 
{
  pinMode(light1, OUTPUT); 
  pinMode(light2, OUTPUT);   
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  digitalWrite(light1, HIGH);
  digitalWrite(light2, HIGH);
}

void loop()
{
  int buttonState1 = digitalRead(button1);
  if(buttonState1 == LOW && lastButtonState1 == HIGH)
  {
    state1=!state1;
    digitalWrite (light1, state1);
  }
  lastButtonState1 = buttonState1; 
  int buttonState2 = digitalRead(button2);
  if(buttonState2 == LOW && lastButtonState2 == HIGH)
  {
    state2=!state2;
    digitalWrite (light2, state2);
  }
  lastButtonState2 = buttonState2;
  delay(50);// a little debounce you may need to tweak
}

One more time thank you guys.

I have got a little problem with my code.
When Arduino Mega 2560 starts the light2 is ON when it should be OFF and
I can turn on light1 only when light2 is OFF and I can turn off light1 only when light2 is ON.
With light2 is the opposite:
I can turn on light2 only when light1 is ON and turn off light2 when light1 is OFF.

Make sure you're using a separate 5V supply for your relay board as it can only provide enough current to drive 1 relay ON at a time. Solution: Use a separate 5V power source for your relay board.