#define BLYNK_TEMPLATE_ID "TMPL6KDdHcxfc"
#define BLYNK_TEMPLATE_NAME "ESP32 With 2 LED"
#define BLYNK_AUTH_TOKEN "DEQCWyqtNCtXJJ-1JATUAHHePmKB3b-2"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = "DEQCWyqtNCtXJJ-1JATUAHHePmKB3b-2";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
const int servoPin = 16;
const int servoPin2 = 17; // Pin for the second servo
Servo myServo;
Servo myServo2;
const int ledPin = 2;
Servo myServos[2] = {myServo, myServo2}; // Array of servos
bool servoActive = false;
bool servoMoving = false;
unsigned long startTime = 0;
const unsigned long duration = 30000;
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
for (int i = 0; i < 2; i++) {
myServos[i].attach((i == 0) ? servoPin : servoPin2, 500, 2400);
}
pinMode(ledPin, OUTPUT);
}
void loop() {
Blynk.run();
manageServoMovement();
}
BLYNK_WRITE(V2) {
int buttonState = param.asInt();
if (buttonState == 1) {
startServoMovement();
} else {
stopServoMovement();
}
}
void manageServoMovement() {
if (servoActive) {
unsigned long elapsedTime = millis();
if (elapsedTime - startTime >= duration) {
stopServoMovement();
} else {
int pos = map(elapsedTime % 500, 0, 500, 0, 180);
for (int i = 0; i < 2; i++) {
myServos[i].write(pos);
}
int ledState = (elapsedTime / 250) % 2;
digitalWrite(ledPin, ledState);
}
}
}
void startServoMovement() {
if (!servoActive && !servoMoving) {
servoActive = true;
servoMoving = true;
startTime = millis();
digitalWrite(ledPin, HIGH);
}
}
void stopServoMovement() {
if (servoActive) {
servoActive = false;
for (int i = 0; i < 2; i++) {
myServos[i].write(90);
delay(50);
myServos[i].detach();
}
digitalWrite(ledPin, LOW);
}
servoMoving = false;
}