#define BLYNK_TEMPLATE_ID "TMPL6TSIyJk1f"
#define BLYNK_TEMPLATE_NAME "2"
#define BLYNK_AUTH_TOKEN "t-FkD7JDC_QFp48_42NLuoZWDfm4eIfh"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h> // ใช้สำหรับ ESP32
Servo servo;
char auth[] = "t-FkD7JDC_QFp48_42NLuoZWDfm4eIfh";
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
int currentPos = 90; // เริ่มต้นที่ 90 องศา
void setup()
{
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
servo.attach(15); // ใช้ GPIO5 (ขา D5 ของ ESP8266 เทียบเป็น GPIO5 ใน ESP32)
servo.write(currentPos);
}
void loop()
{
Blynk.run();
}
// ควบคุมจากสไลเดอร์ V0
BLYNK_WRITE(V0)
{
currentPos = param.asInt();
currentPos = constrain(currentPos, 0, 180);
servo.write(currentPos);
Serial.print("Slider set to: ");
Serial.println(currentPos);
}
// ปุ่ม V1 → หมุนซ้าย 45°
BLYNK_WRITE(V1)
{
if (param.asInt() == 1) {
currentPos -= 45;
currentPos = constrain(currentPos, 0, 180);
servo.write(currentPos);
Serial.print("Left 45 -> ");
Serial.println(currentPos);
}
}
// ปุ่ม V2 → หมุนขวา 45°
BLYNK_WRITE(V2)
{
if (param.asInt() == 1) {
currentPos += 45;
currentPos = constrain(currentPos, 0, 180);
servo.write(currentPos);
Serial.print("Right 45 -> ");
Serial.println(currentPos);
}
}