#define BLYNK_TEMPLATE_ID "TMPL2AlnR7HDX"
#define BLYNK_TEMPLATE_NAME "Toy project"
#define BLYNK_AUTH_TOKEN "2iRHbojavTwoVNlzMzbDS4UeUSjxaTB3"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// Blynk sa hum toy ko on krden
// Jaisa he koi Banda uska samna aye motor 3 sec k Lia Chala rukjay
// Phr 2 hmara pas push button honga
// Button 1 press krenga servo +90 jay
// Button 2 press kra servo -90
// Blynk sa hum system on krskenga
// Motor ko reverse and forward kr sken ga that's all
#define servo_pin 18
#define forward_dir 25
#define rev_dir 27
#define forward_btn 17
#define rev_btn 19
#define PIR 5
Servo servo;
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
bool forward_blynk;
bool rev_blynk;
bool forward_state;
bool rev_state;
bool active;
//byte ledPin = 13;
int statostart;
//bool forward_state;
//bool rev_state;
int relay1_state = 0;
//#define button1_vpin V9
bool toy_active = false; //to disarm and arm secutiry system
//#define PIR 25 //D5 PIR Motion Sensor
// BLYNK_CONNECTED()
// {
// Blynk.syncVirtual(button1_vpin);
// }
#define timeSeconds 10
// Set GPIOs for LED and PIR Motion Sensor
const int led = 2;
//const int motionSensor = 16;
// Timer: Auxiliary variables
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
boolean motion = false;
// Checks if motion was detected, sets LED HIGH and starts a timer
void IRAM_ATTR detectsMovement() {
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
//forwards control
BLYNK_WRITE(V1) {
forward_blynk = param.asInt();
Serial.print("forward BLYNK:");
Serial.println(forward_blynk);
//digitalWrite(forward,(!forward_state));
}
//rev control
BLYNK_WRITE(V2) {
rev_blynk = param.asInt();
Serial.print("rev BLYNK:");
Serial.println(rev_blynk);
//digitalWrite(rev,(!rev_state));
}
//to get the disarm/arm of toy system use var toy_active
BLYNK_WRITE(V0) {
toy_active = param.asInt();
Serial.print("toy_active: ");
Serial.println(toy_active);
}
void setup()
{
Serial.begin(115200);
// pinMode(PIR, INPUT_PULLUP);
pinMode(forward_btn, INPUT_PULLUP);
pinMode(rev_btn, INPUT_PULLUP);
pinMode(rev_dir, OUTPUT);
pinMode(forward_dir, OUTPUT);
// PIR Motion Sensor mode INPUT_PULLUP
pinMode(PIR, INPUT_PULLUP);
// Set motionSensor pin as interrupt, assign interrupt function and set RISING mode
attachInterrupt(digitalPinToInterrupt(PIR), detectsMovement, RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
servo.attach(servo_pin);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
timer.run();
now = millis();
if ((digitalRead(led) == HIGH) && (motion == false)) {
Serial.println("MOTION DETECTED!!!");
motion = true;
if (toy_active == 1) {
if (forward_blynk & rev_blynk) {
digitalWrite(forward_dir, 0);
digitalWrite(rev_dir, 0);
}
else if (!forward_blynk & rev_blynk) {
digitalWrite(forward_dir, 0);
digitalWrite(rev_dir, 1);
}
else if (forward_blynk & !rev_blynk) {
digitalWrite(forward_dir, 1);
digitalWrite(rev_dir, 0);
}
else if (!forward_blynk & rev_blynk) {
digitalWrite(forward_dir, 0);
digitalWrite(rev_dir, 1);
}
}
}
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if (startTimer && (now - lastTrigger > (timeSeconds * 300))) {
Serial.println("Motion stopped...");
digitalWrite(led, LOW);
digitalWrite(forward_dir, 0);
digitalWrite(rev_dir, 0);
startTimer = false;
motion = false;
}
servo_cntrl();
}
void servo_cntrl()
{
if (digitalRead(forward_btn) == LOW) {
delay(200);
if (toy_active) {
servo.write(90);
Serial.println("SERVO WRITE +++");
}
//Serial.print("forward = ");
//Serial.println();
//delay(50);
}
if (digitalRead(rev_btn) == LOW) {
delay(200);
if (toy_active) {
Serial.println("SERVO WRITE ---");
servo.write(-90);
}
//Serial.print("rev = ");
//Serial.println();
//delay(50);
}
}