#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6rxlLX1S3"
#define BLYNK_TEMPLATE_NAME "FESTO FISH Bionics4Education"
#define BLYNK_AUTH_TOKEN "qBywUGmDaFvVdt1SbUSn_JYzhx6rBNUn"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
Servo myservo;
int pos = 0; // variable to store the servo position
// Recommended PWM GPIO pins on the ESP32 include 2,4,12-19,21-23,25-27,32-33
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
// Possible PWM GPIO pins on the ESP32-S3: 0(used by on-board button),1-21,35-45,47,48(used by on-board LED)
// Possible PWM GPIO pins on the ESP32-C3: 0(used by on-board button),1-7,8(used by on-board LED),9-10,18-21
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
int servoPin = 25;
#elif defined(CONFIG_IDF_TARGET_ESP32C3)
int servoPin = 25;
#else
int servoPin = 25;
#endif
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
int swing = 0;
int speed = 3;
int direction=20;
int kanan,kiri;
BLYNK_WRITE(V0)
{ speed = param.asInt();
Serial.println(speed);
}
BLYNK_WRITE(V1)
{ direction = param.asInt();
Serial.println(direction);
if(direction==45) kanan=kiri=0;
if(direction<45) { kanan = 45-direction; kiri=0; }
if(direction>45) { kiri = direction-45; kanan=0; }
}
BLYNK_WRITE(V2)
{ swing = param.asInt();
Serial.println(swing);
}
void moveServo()
{ int jumlahKanan = 180-swing-kanan;
int jumlahKiri = swing+kiri;
Serial.print("Jumlah Kanan = ");
Serial.print(jumlahKanan);
Serial.print(" Jumlah Kiri = ");
Serial.println(jumlahKiri);
for (pos = jumlahKiri; pos <= jumlahKanan; pos += speed)
{ myservo.write(pos);
delay(20+speed);
}
for (pos = jumlahKanan; pos >= jumlahKiri; pos -= speed)
{ myservo.write(pos);
delay(20+speed);
}
}
void setup()
{ ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);
myservo.attach(servoPin, 1000, 2000);
myservo.write(90);
delay(1000);
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1,moveServo);
}
void loop()
{ Blynk.run();
timer.run();
}