/*
Blynk example
You should get Auth Token in the Blynk App.
You need to write the right wifiCredentials.
*/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLqW0-X6PZ"
#define BLYNK_DEVICE_NAME "Quickstart Template"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
Servo servoMotor;
int pos = 0;
const int trigPin = 4;
const int echoPin = 25;
const int trigPin2 = 15;
const int echoPin2 = 2;
long duration;
int distance;
long duration2;
int distance2;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "iaXK9MfYNQQ-2Y8KQL4vYzMm9jTW8iex";
// Your WiFi credentials.
// Set password to "" for open networks.
const char *ssid = "Wokwi-GUEST";
const char *pass = "";
WidgetLED led(V9);
void blinkLedWidget()
{
if (distance <30) {
led.off();
Serial.println("LED on V9: off");
} else {
led.on();
Serial.println("LED on V9: on");
}
}
void servo()
{
if(distance2 > 100){
for (pos = 0; pos <= 180; pos += 1) {
servoMotor.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servoMotor.write(pos);
delay(15);
}
}
}
void setup() {
// Debug console
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
servoMotor.attach(18);
Serial.begin(115200);
delay(10);
Blynk.begin(auth, ssid, pass);
}
void loop() {
ultrasonic1();
delay(200);
ultrasonic2();
delay(200);
blinkLedWidget();
servo();
Blynk.run();
}
void ultrasonic1()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
}
void ultrasonic2()
{
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2 * 0.034 / 2;
Serial.print("Distance2: ");
Serial.println(distance2);
}