#define BLYNK_TEMPLATE_ID "TMPL6A8qvgDuH"
#define BLYNK_TEMPLATE_NAME "SERVO WITH ULTRASONIC"
#define BLYNK_AUTH_TOKEN "dRqYC6DzzfoAoo51_NeoEp6BWDSNrn0l"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";

Servo servo;

#define TRIGGER_PIN 12
#define ECHO_PIN 14

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  servo.attach(2); 
  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  Blynk.run();
  long duration, cm;
  
  
  digitalWrite(TRIGGER_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER_PIN, LOW);

  
  duration = pulseIn(ECHO_PIN, HIGH);

  
  cm = (duration / 2) / 29.1;

  
  Blynk.virtualWrite(V1, cm);

  delay(200); 
}


BLYNK_WRITE(V0) {
  int servoPosition = param.asInt(); 
  servo.write(servoPosition); 
}


//write a code in esp32 to control servo motor with the help of blynk console website there is slider in dashboard which is a virtual pin v0 for slider and when in the blynk dashboard the slider is slided to 50 or 60 then in wokwi the servo motor will be rotated to 50 and 60 degree simuntaneously and v1 is a virtual pin for gauge in blynk dashboard it will connected to ultrasonic to show the cm in wokwi
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
servo1:GND
servo1:V+
servo1:PWM