#define BLYNK_TEMPLATE_ID "TMPxxxxxx"
#define BLYNK_TEMPLATE_NAME "Device"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <ESP32Servo.h>
// ================= PIN =================
// Motor A
#define IN1 2
#define IN2 3
#define ENA 4
// Motor B
#define IN3 5
#define IN4 6
#define ENB 7
// Servo
#define SERVO_PIN 8
// Digital Output
#define V0_PIN 9
#define V3_PIN 10
#define V4_PIN 18
#define V6_PIN1 19
#define V6_PIN2 20
#define V7_PIN 21
#define V8_PIN 0
// ================= WIFI =================
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// ================= OBJECT =================
Servo servo;
BlynkTimer timer;
// ================= PWM =================
#define PWM_FREQ 1000
#define PWM_RES 8
#define CH_A 0
#define CH_B 1
// ================= STATE =================
bool v6State = false;
bool v7State = false;
bool v8State = false;
bool v6Enable = false;
bool v7Enable = false;
bool v8Enable = false;
// ================= MOTOR =================
void motorForward(int speedVal) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
ledcWrite(CH_A, speedVal);
ledcWrite(CH_B, speedVal);
}
void motorBackward(int speedVal) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
ledcWrite(CH_A, speedVal);
ledcWrite(CH_B, speedVal);
}
void motorStop() {
ledcWrite(CH_A, 0);
ledcWrite(CH_B, 0);
}
// ================= BLYNK =================
BLYNK_WRITE(V0) {
digitalWrite(V0_PIN, param.asInt());
}
BLYNK_WRITE(V1) {
int x = param.asInt();
int speedVal = map(abs(x), 0, 255, 0, 255);
if (x > 20) {
motorForward(speedVal);
} else if (x < -20) {
motorBackward(speedVal);
} else {
motorStop();
}
}
BLYNK_WRITE(V2) {
int y = param.asInt();
int angle = map(y, -255, 255, 0, 180);
servo.write(angle);
}
BLYNK_WRITE(V3) {
digitalWrite(V3_PIN, param.asInt());
}
BLYNK_WRITE(V4) {
digitalWrite(V4_PIN, param.asInt());
}
BLYNK_WRITE(V5) {
// kosong sesuai permintaan
}
void v6Task() {
if (v6Enable) {
v6State = !v6State;
digitalWrite(V6_PIN1, v6State);
digitalWrite(V6_PIN2, !v6State);
}
}
BLYNK_WRITE(V6) {
v6Enable = param.asInt();
if (!v6Enable) {
digitalWrite(V6_PIN1, LOW);
digitalWrite(V6_PIN2, LOW);
}
}
void v7Task() {
if (v7Enable) {
v7State = !v7State;
digitalWrite(V7_PIN, v7State);
}
}
void v8Task() {
if (v8Enable) {
v8State = !v8State;
digitalWrite(V8_PIN, v8State);
}
}
BLYNK_WRITE(V7) {
v7Enable = param.asInt();
if (!v7Enable) digitalWrite(V7_PIN, LOW);
}
BLYNK_WRITE(V8) {
v8Enable = param.asInt();
if (!v8Enable) digitalWrite(V8_PIN, LOW);
}
BLYNK_WRITE(V9) {
int y = param.asInt();
int mappedVal;
if (y >= 0) {
mappedVal = map(y, 0, 255, 0, 40000);
} else {
mappedVal = map(y, -255, 0, 40000, 0);
}
Blynk.virtualWrite(V9, mappedVal);
}
// ================= SETUP =================
void setup() {
Serial.begin(115200);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(V0_PIN, OUTPUT);
pinMode(V3_PIN, OUTPUT);
pinMode(V4_PIN, OUTPUT);
pinMode(V6_PIN1, OUTPUT);
pinMode(V6_PIN2, OUTPUT);
pinMode(V7_PIN, OUTPUT);
pinMode(V8_PIN, OUTPUT);
// PWM
ledcSetup(CH_A, PWM_FREQ, PWM_RES);
ledcSetup(CH_B, PWM_FREQ, PWM_RES);
ledcAttachPin(ENA, CH_A);
ledcAttachPin(ENB, CH_B);
// Servo
servo.attach(SERVO_PIN);
// Timer
timer.setInterval(500L, v6Task);
timer.setInterval(500L, v7Task);
timer.setInterval(500L, v8Task);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
// ================= LOOP =================
void loop() {
Blynk.run();
timer.run();
}Loading
xiao-esp32-c3
xiao-esp32-c3