#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6HsxBBb6D"
#define BLYNK_TEMPLATE_NAME "Servo"
#define BLYNK_AUTH_TOKEN "e-G1qQaxF7Z0cPQ8W4dgiS-wyNcOWhMa"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
WidgetLED led1(V4);
WidgetLED led2(V6);
WidgetLED led3(V7);
Servo myservo; // create servo object to control a servo
int angle = 0;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
void servoControl(int ang)
{
Serial.print("Angle:");
Serial.println(ang);
ang = map(ang,-90,90, 0,180);
myservo.write(ang);
}
BLYNK_WRITE(V0)
{
angle = param.asInt();
servoControl(angle);
//for LED widget indicator
if ((angle <=45) &&(angle>=-45)) { //stop
led1.off();
led2.on();
led3.off();
}
else if (angle < -45) {
led1.on();
led2.off();
led3.off();
}
else if (angle > 45){
led1.off();
led2.off();
led3.on();
}
}
void setup() {
// Debug console
Serial.begin(115200);
myservo.attach(18); // attaches the servo on pin 9 to the servo object
angle = map(angle,-90,90, 0,180); //adjust the range of servo
myservo.write(angle); //init servo angle at 0
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
void loop() {
Blynk.run();
}