#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6zgR0qA7a"
#define BLYNK_TEMPLATE_NAME "Servo control on day and weight"
#define BLYNK_AUTH_TOKEN "tY2bkQ12UefMQ4iXzzNV0tihYFR5nOoc"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN; // Your Blynk authentication token
char ssid[] = "Wokwi-GUEST"; // Your Wi-Fi SSID
char pass[] = ""; // Your Wi-Fi password
Servo servo1;
unsigned long previousMillis = 0;
const long interval = 3000; // 30 seconds interval
bool isServoOn = false;
int minServoValue = 90; // Minimum servo value
int maxServoValue = 180; // Maximum servo value
BLYNK_WRITE(V1) {
int value = param.asInt();
if (value == 90) {
isServoOn = false;
previousMillis = millis();
servo1.write(minServoValue); // Turn off the servo
Blynk.virtualWrite(V1, 90); // Update the Blynk switch state to off
} else {
isServoOn = true;
servo1.write(maxServoValue); // Turn on the servo
Blynk.virtualWrite(V1, 180); // Update the Blynk switch state to on
}
}
void setup() {
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
servo1.attach(23); // Change the pin number as per your setup
}
void loop() {
Blynk.run();
if (!isServoOn) {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
servo1.write(maxServoValue); // Turn on the servo
isServoOn = true;
Blynk.virtualWrite(V1, 180); // Update the Blynk switch state to on
}
}
}