Controlling LED with PIR and blynk switch

Hi guys,

I am trying to control an LED using a PIR sensor and also by a blynk switch. So the way it works is that when the sensor detects someone the light turns ON then OFF, but I also want to turn it ON and OFF using my blynk switch. Here is the code below.

#define BLYNK_USE_DIRECT_CONNECT


#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(0, 1); // RX, TX

#define BLYNK_PRINT DebugSerial
#include <BlynkSimpleSerialBLE.h>

#include <Servo.h>


Servo servo; // Create Servo object to control servo

char auth[] = "RTeAzg01lX0AJMuAxrPK3uRA9vo2HXnQ"; // You should get Auth Token in the Blynk App.

int led = 13;                // the pin that the LED is atteched to
int sensor = 2;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;                 // variable to store the sensor status (value)


void setup() {
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);    // initialize sensor as an input
  Serial.begin(9600);        // initialize serial

  DebugSerial.println("Waiting for connections...");


  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  servo.attach(12); // attaches the servo on pin 9 to the servo object

}



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



void pirSensor(){
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}





// This function is called whenever the assigned Widget changes state
BLYNK_WRITE(V2) {  // V2 is the Virtual Pin assigned to a Button Widget
int pinValue = param.asInt();
  if(pinValue == 1) {
    digitalWrite(led, HIGH);
    state = LOW;
    val = 0;
  }
  else {
    if(pinValue == 0) {
      digitalWrite(led, LOW);
      pirSensor();
    }
  }
}

The issue i have is the the PIR will not work with the LED, only the blynk switch does.

Hi @esidow1994. It's not a good idea to post your Blynk auth token publicly because this will allow everyone to read data from and write data to your Blynk project.

If that's your real Blynk auth token in your sketch, I recommend switching to a new token.

You are not calling the pir function at all. You need to use the Blynk timer create a timer object and use it activate your pir. Look through the Blynk documentation as to how to successfully use the Blynk timer