Sync two physical buttons

HI,
i was trying to sync a physical button and RF receiver with blynk app
i want to turn the relay on or off either by APP or a physical Switch or by a RF Remote. and i want the status to be synced with the app.
i m using nodemcu esp8266
4 channel relay
4 push buttons
4 channel learning RF receiver.
when i compile the below code… it compiled without error. but when i run it and connect all the components. the relay keeps toggling automatically…i cant control it either by app RF remote or physical switch.
please help me to find the error in the coding and help me to control the relay either by app or switch or by remote

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

void checkswitch();
void checkRF();

int Virtual1 = LOW;
int Virtual2 = LOW;
int Virtual3 = LOW;
int Virtual4 = LOW;

int Switch1 = LOW;
int Switch2 = LOW;
int Switch3 = LOW;
int Switch4 = LOW;

int RF1 = LOW;
int RF2 = LOW;
int RF3 = LOW;
int RF4 = LOW;


#define AUTH ""  
#define WIFI_SSID ""                   
#define WIFI_PASS ""                   


#define Relay_1    5   
#define Relay_2    4   
#define Relay_3    0  
#define Relay_4    2   

#define switch_1    14   
#define switch_2    12   
#define switch_3    13   
#define switch_4    16   

#define RF_1    15  
#define RF_2     3   
#define RF_3     1  
#define RF_4     9   

#define V_1    V12 
#define V_2    V13
#define V_3    V14
#define V_4    V15

BLYNK_CONNECTED() {

  
  Blynk.virtualWrite(V_1, Virtual1);
  Blynk.virtualWrite(V_2, Virtual2);
  Blynk.virtualWrite(V_3, Virtual3);
  Blynk.virtualWrite(V_4, Virtual4);

}


BLYNK_WRITE(V_1) {
 Virtual1 = param.asInt();
 digitalWrite(Relay_1,Virtual1);
}

BLYNK_WRITE(V_2) {
 Virtual2 = param.asInt();
 digitalWrite(Relay_2,Virtual2);
}
BLYNK_WRITE(V_3) {
 Virtual3 = param.asInt();
 digitalWrite(Relay_3,Virtual3);
}
BLYNK_WRITE(V_4) {
 Virtual4 = param.asInt();
 digitalWrite(Relay_4,Virtual4);
}

void checkswitch()
{
  if (digitalRead(switch_1) == LOW) 
  {
       if ((Switch1 == HIGH)||(RF1 == HIGH)||(Virtual1 == HIGH)) 
       {
            digitalWrite(Relay_1,LOW);
            Blynk.virtualWrite(V_1, LOW);
            Switch1 = LOW;
            RF1 = LOW;
            Virtual1 = LOW;
        }
   else 
   {
    digitalWrite(Relay_1, HIGH);
    Blynk.virtualWrite(V_1, HIGH);
        Switch1 = HIGH;
        RF1 = HIGH;
        Virtual1 = HIGH;
    }
  }

 if (digitalRead(switch_2) == LOW) 
  {
       if ((Switch2 == HIGH)||(RF2 == HIGH)||(Virtual2 == HIGH)) 
       {
            digitalWrite(Relay_2,LOW);
            Blynk.virtualWrite(V_2, LOW);
            Switch2 = LOW;
            RF2 = LOW;
            Virtual2 = LOW;
        }
   else 
   {
    digitalWrite(Relay_2, HIGH);
    Blynk.virtualWrite(V_2, HIGH);
        Switch2 = HIGH;
        RF2 = HIGH;
        Virtual2 = HIGH;
    }
  }
  
  if (digitalRead(switch_3) == LOW) 
  {
       if ((Switch3 == HIGH)||(RF3 == HIGH)||(Virtual3 == HIGH)) 
       {
            digitalWrite(Relay_3,LOW);
            Blynk.virtualWrite(V_3, LOW);
            Switch3 = LOW;
            RF3= LOW;
            Virtual3 = LOW;
        }
   else 
   {
    digitalWrite(Relay_3, HIGH);
    Blynk.virtualWrite(V_3, HIGH);
        Switch3 = HIGH;
        RF3 = HIGH;
        Virtual3 = HIGH;
    }
  }
  
  if (digitalRead(switch_4) == LOW) 
  {
       if ((Switch4 == HIGH)||(RF4 == HIGH)||(Virtual4 == HIGH)) 
       {
            digitalWrite(Relay_4,LOW);
            Blynk.virtualWrite(V_4, LOW);
            Switch4 = LOW;
            RF4 = LOW;
            Virtual4 = LOW;
        }
   else 
   {
    digitalWrite(Relay_4, HIGH);
    Blynk.virtualWrite(V_4, HIGH);
        Switch4 = HIGH;
        RF4 = HIGH;
        Virtual4 = HIGH;
    }
  }
}
void checkRF()
{
  if (digitalRead(RF1) == LOW) 
  {
       if ((Switch1 == HIGH)||(RF1 == HIGH)||(Virtual1 == HIGH)) 
       {
            digitalWrite(Relay_1,LOW);
            Blynk.virtualWrite(V_1, LOW);
            Switch1 = LOW;
            RF1 = LOW;
            Virtual1 = LOW;
        }
   else 
   {
    digitalWrite(Relay_1, HIGH);
    Blynk.virtualWrite(V_1, HIGH);
        Switch1 = HIGH;
        RF1 = HIGH;
        Virtual1 = HIGH;
    }
  }

 if (digitalRead(RF2) == LOW) 
  {
       if ((Switch2 == HIGH)||(RF2 == HIGH)||(Virtual2 == HIGH)) 
       {
            digitalWrite(Relay_2,LOW);
            Blynk.virtualWrite(V_2, LOW);
            Switch2 = LOW;
            RF2 = LOW;
            Virtual2 = LOW;
        }
   else 
   {
    digitalWrite(Relay_2, HIGH);
    Blynk.virtualWrite(V_2, HIGH);
        Switch2 = HIGH;
        RF2 = HIGH;
        Virtual2 = HIGH;
    }
  }
  
  if (digitalRead(RF3) == LOW) 
  {
       if ((Switch3 == HIGH)||(RF3 == HIGH)||(Virtual3 == HIGH)) 
       {
            digitalWrite(Relay_3,LOW);
            Blynk.virtualWrite(V_3, LOW);
            Switch3 = LOW;
            RF3= LOW;
            Virtual3 = LOW;
        }
   else 
   {
    digitalWrite(Relay_3, HIGH);
    Blynk.virtualWrite(V_3, HIGH);
        Switch3 = HIGH;
        RF3 = HIGH;
        Virtual3 = HIGH;
    }
  }
  
  if (digitalRead(RF4) == LOW) 
  {
       if ((Switch4 == HIGH)||(RF4 == HIGH)||(Virtual4 == HIGH)) 
       {
            digitalWrite(Relay_4,LOW);
            Blynk.virtualWrite(V_4, LOW);
            Switch4 = LOW;
            RF4 = LOW;
            Virtual4 = LOW;
        }
   else 
   {
    digitalWrite(Relay_4, HIGH);
    Blynk.virtualWrite(V_4, HIGH);
        Switch4 = HIGH;
        RF4 = HIGH;
        Virtual4 = HIGH;
    }
  }
}
void setup()
{

  Serial.begin(115200);
  Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);
  
   pinMode(Relay_1, OUTPUT);
   pinMode(Relay_2, OUTPUT);
   pinMode(Relay_3, OUTPUT);
   pinMode(Relay_4, OUTPUT);

  pinMode(switch_4, INPUT);
  pinMode(switch_1, INPUT);
  pinMode(switch_2, INPUT);
  pinMode(switch_3, INPUT);
  pinMode(RF_1, INPUT);
  pinMode(RF_2, INPUT);
  pinMode(RF_3, INPUT);
  pinMode(RF_4, INPUT);
  
timer.setInterval(500L, checkswitch);
timer.setInterval(600L, checkRF);
}

void loop()
{
  Blynk.run();
  timer.run();

} ```

Why are you tracking the state of the three input types and using these statuses in your if statements?
I would have thought that it would be much simpler to track the current state of each relay and toggle it when commanded by either the App, RF or physical switch.

BTW, it’s much better if you use triple backticks at the beginning and end of your code rather than using block quotes. Triple backticks look like this:
```

Pete.

Thanks PeteKnight,
But How to keep track status of the OUTPUT Pin… could you please help me.
and sorry for the Bockquotes… i couldnt find the triple backticks

thanks i update the code format

With a variable for each relay to track if it’s LOW (0) or HIGH(1).

Are your relays activated when they are LOW or HIGH?

Pete.