Problem on My coding final year project

#define BLYNK_TEMPLATE_NAME "Security Alarm Notification"
#define BLYNK_AUTH_TOKEN "1giwOjbaLqOjNxn6LSlkg7hKquT2MpdR"

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>

#define BLYNK_PRINT Serial

char auth[] = "1giwOjbaLqOjNxn6LSlkg7hKquT2MpdR"; // Blynk authentication token
char ssid[] = "iPhone"; // WiFi network SSID
char pass[] = "yan12345678"; // WiFi network password

#define PIR_PIN 2 // PIR sensor connected to pin 4
#define BUZZER_PIN 26 // Buzzer connected to pin 5
#define SIREN_PIN 14 // Siren connected to pin 27
#define BUTTON_PIN_V2 V2 // Blynk button connected to virtual pin V2
#define SERVO_PIN 4

bool alarmOn = false; // Flag to track if alarm is on

BlynkTimer timer;
Servo myServo;

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(SIREN_PIN, OUTPUT);

  Blynk.begin(auth, ssid, pass);

  timer.setInterval(500L, checkPIR);
  Serial.begin(9600);
  myServo.attach(SERVO_PIN);
}

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

  rotateServo(0);   // Rotate to 0 degrees
  delay(1000);      // Wait for 8 second
  rotateServo(90);  // Rotate to 90 degrees
  delay(1000);      // Wait for 8 second
  rotateServo(180); // Rotate to 180 degrees
  delay(1000);      // Wait for 8 second
}

void checkPIR() {
  int pirState = digitalRead(PIR_PIN);
  if (pirState == HIGH && !alarmOn) {
    Blynk.virtualWrite(BUTTON_PIN_V2, 255); // Turn on Blynk button when PIR detects motion
    digitalWrite(BUZZER_PIN, HIGH);
    digitalWrite(SIREN_PIN, HIGH);
    alarmOn = true;
    Blynk.logEvent("animal_alert"); // Send notification to Blynk app
  } else if (pirState == LOW && alarmOn && !Blynk.connected()) {
    Blynk.virtualWrite(BUTTON_PIN_V2, 0); // Turn off Blynk button when PIR detects no motion
    digitalWrite(BUZZER_PIN, LOW);
    digitalWrite(SIREN_PIN, LOW);
    alarmOn = false;
  }
}

BLYNK_WRITE(BUTTON_PIN_V2) {
  int buttonState = param.asInt();
  if (buttonState == 0) { // If button is turned off in the Blynk app
    digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
    digitalWrite(SIREN_PIN, LOW); // Turn off the siren
    alarmOn = false;
  }
}

void rotateServo(int angle) {
  myServo.write(angle);
}

why i upload this, then the detection of sensor late??? or the pir sensor have problem

@imran_123 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Done

You’re using blocking delay() commands. When the code reaches one of these, all code execution stops for the duration of the delay period.
You have three, one after another, that last a total of three seconds (down from the 18 seconds you seem to have been using before.
This means that for three seconds nothing happens. You have a BlynkTimer set-up that is supposed to execute every 0.5 seconds, but it’s actually more like 3.5 seconds between execution because of the delays.

I’m guessing that you have the PIR sensor mounted in a servo and it’s being moved around by the servo. Is that correct?

Pete.

Yeah how to solve

Impossible to say without more info on your project.

Pete.

Actually my project i use esp 32 connect with pir sensor and servo motor also have relay for buzzer that have 12 volt. I want that the servo motor just rotate only and pir sensor detect only.

You’ll need to provide more information about your intended functionality if you want suggestions from me.

Pete.

the function of pir sensor to detect the animal at the farm so it will make the buzzer turn on untill i push button turn off in blynk apps like that its function. for servo motor its like cctv just rotate from 0 to 180 degree to help the pir detect

PIR sensors work by “looking for movement” of a heat source. How the movement is detected is quite complex, but the detection is based on the principal that the sensor is static and the heat source is moving.

If you move the sensor relative to any heat source then the sensor will believe that the heat source has moved, and report that it’s detected an intruder.
You might think that it doesn’t matter whether it’s the animal or the sensor that’s moved, because either way you want to know that an animal is there, but heat sources (or differentials in heat levels) can be many things other than animals.
In a typical outdoor scene there will be large temperature differentials caused by the sun, and by other items that radiate different levels of heat. Normally these items don’t trigger the PIR sensor because the sensor and the objects are static.

If you start moving the sensor around then those static objects appear to move relative to the sensor, and can generate false alarms.

It is potentially possible to scan an area for movement by moving the PIR sensor, but you’d need to ignore any items detected during the time that the sensor is moving, and allow a “settle time” after the sensor has finished its movement before reporting any detection events as movement of an animal. How long this settle time would need to be would depend on the detector and the type of environment that is being monitored.

Clearly, it would be better to use multiple static PIR sensors rather than one mobile sensor, but presumably you have reasons for not using this approach.

I’ve tried multiple times to extract more information from you regarding your project, but each time your response is a couple of sentences at most, and you seem reluctant to share any useful information about your project, the testing you’ve done so far, the types of issues that you’ve encountered during the testing, etc etc.

I’m going to step back from this discussion because I’m getting tired of trying to extract information from you, and I’m not going to waste time suggesting potential coding solutions without having all of the appropriate information about your project.

Good luck with your project.

Pete.

1 Like