#define BLYNK_TEMPLATE_ID "TMPL6SvxyVLhu"
#define BLYNK_TEMPLATE_NAME "Tombol servo"
#define BLYNK_AUTH_TOKEN "CP-lMIpNpN_8c1MPoCgp2RLwfRHE24oQ"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// Replace these credentials with your own
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pins for the LEDs
#define RED_LED_PIN 2
#define BLUE_LED_PIN 4
// Pin for the servo
#define SERVO_PIN 13
// Blynk virtual pins
#define SERVO_BUTTON_VPIN V0
Servo servo;
bool servoActivated = false;
void setup() {
Serial.begin(115200);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLUE_LED_PIN, OUTPUT);
servo.attach(SERVO_PIN);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
if (servoActivated) {
// Rotate servo to a specific position
servo.write(90); // Change the angle as needed
blinkLeds(); // Blink LEDs
} else {
servo.write(0); // Return servo to initial position
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue LED
}
}
// Callback function for controlling the servo
BLYNK_WRITE(SERVO_BUTTON_VPIN) {
int servoButtonState = param.asInt();
if (servoButtonState == 1) {
servoActivated = true;
} else {
servoActivated = false;
}
}
// Function to blink LEDs
void blinkLeds() {
unsigned long previousMillis = 0;
const long interval = 500; // Blink interval in milliseconds
bool ledState = false;
while (millis() - previousMillis < 30000) { // Blink LEDs for 30 seconds
if (millis() - previousMillis >= interval) {
previousMillis = millis();
if (ledState) {
ledState = false;
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(BLUE_LED_PIN, LOW); // Turn off blue LED
} else {
ledState = true;
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(BLUE_LED_PIN, HIGH); // Turn on blue LED
}
}
}
}