Override main function using BLYNK APP

Hi! Can you please help me with my code? I attach a copy below. I’m still new to Arduino, currently doing a project where the main function is using LDR sensor to control the switching a light bulb. But what i really want is for the virtual button from the Blynk application to be able to override/ignore the main function and control the switching of the light bulb while using the BLYNK app (if light bulb is on, virtual button can turn off and vice versa), and the program will go back to main function when the Blynk app is turned off.

const int ledPin = 13;   //the number of the LED pin
const int ldrPin = A0;  //the number of the LDR pin


int relay1State = 0;

void setup() {
  // put your setup code here, to run once:
// Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
  
}

 BLYNK_WRITE(V1) {
  relay1State = param.asInt();
  digitalWrite(ledPin, relay1State);
   }




void checkLDR() {
  int relay1Status = digitalRead(ledPin);
  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value

  //check if the LDR status is <= 300
  //if it is, the LED is HIGH
  
 if (ldrStatus <=300 ) {
    digitalWrite(ledPin, HIGH);               //Lights on
    Serial.println("Light Bulb is ON");
    Blynk.virtualWrite(V1, relay1Status);
   } 
  else {
    digitalWrite(ledPin, LOW);               //Lights off
    Serial.println("Light Bulb is OFF");
    // Update Button Widget
      Blynk.virtualWrite(V1, relay1Status);
 }
}



void loop() {
 Blynk.run();
 checkLDR();
}

I’m planning on using the attachinterrupt() function but do not fully understand on how to use it for this program.

So how would you check to see if the Blynk App is trying to tell the main program something ? (how do you receive msg from Blynk ?)

I dont quite understand what it is you are after but there are a couple of other issues with code and its connection with the Blynk app. First off

//Include BlynkTimer and create timer object 
BlynkTimer timer;

void setup()
{
   timer.setInterval(500L, checkLDR);
}

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

This is literally all that should be be in your loop. Very important in Blynk to keep you void loop clean. If you dont you will flood the server and return Blynk will disconnect you.

Already tried this, if i use this, the priority will always be given to the LDR and not the virtual button on BLYNK. My main goal is to make the virtual button a priority for switching on/off the light bulb.

Deva_Rishi:
So how would you check to see if the Blynk App is trying to tell the main program something ? (how do you receive msg from Blynk ?)

The blynk is connected to my board using Wi-Fi. It means whenever i'm using my BLYNK app, the program would not read the function checkLDR()

whenever i'm using my BLYNK app

so what would be the test for this ?