#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6-KboXDZF"
#define BLYNK_TEMPLATE_NAME "Control LED with motor"
#define BLYNK_AUTH_TOKEN "dKoETf00HbjFuELiZM1EoNOrlgui4cw9"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
Servo myServo; // Create a Servo object
#define LED1 2 //Hijau
#define LED2 4// Merah
int SW_State_M = 0;
int SW_State_H = 0;
int servoPos = 0; // Variable to store servo position
BLYNK_WRITE(2)
{
SW_State_H = param.asInt();
if (SW_State_H == 1)
{
digitalWrite(LED1, HIGH);
Blynk.virtualWrite(2, HIGH);
}
else
{
digitalWrite(LED1, LOW);
Blynk.virtualWrite(2, LOW);
}
}
BLYNK_WRITE(D4)
{
SW_State_M = param.asInt();
if (SW_State_M== 1)
{
digitalWrite(LED2, HIGH);
Blynk.virtualWrite(4, HIGH);
}
else
{
digitalWrite(LED2, LOW);
Blynk.virtualWrite(4, LOW);
}
}
BLYNK_WRITE(V0) // Virtual pin for controlling the servo
{
servoPos = param.asInt(); // Get the servo position from the app
myServo.write(servoPos); // Move the servo to the specified position
Serial.print("Servo moved to position: ");
Serial.println(servoPos);
}
void setup()
{
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
myServo.attach(5); // Attach the servo to GPIO 5 (change as needed)
}
void loop()
{
Blynk.run();
timer.run();
}