• ESP32 + WiFi
• Android 9
• Blynk server
• Blynk Library 1.0.1
Hi, I am doing a project with both automatic and manual control on Blynk IoT. By automatic control, the sensor gives out readings and the output (servo) automatically spins according to the sketch. In manual, regardless of the sensor’s reading, the servo only spins if buttons on Blynk is pressed (sort of like a joystick controller).
I used boolean to check the state of the button, but it seems the button do not update each time it was pressed. What seems to be the problem? The button to change the mode of Blynk is at V7 (boolean modeESP), then it checks for servoState button (V6). If both were turned on, the the servo can spins according to the joystick.
Thank you in advance, below is a portion of my code to control the servo automatically or manually.
boolean modeESP; // false = 0 = automatic control, true = 1 = manual control
boolean servoState; // false = 0 = off, true = 1 = on
boolean servoForward; // false = 0 = off, true = 1 = on
boolean servoBackward; // false = 0 = off, true = 1 = on
boolean servoLeft; // false = 0 = off, true = 1 = on
boolean servoRight; // false = 0 = off, true = 1 = on
boolean servoStop; // false = 0 = off, true = 1 = on
#define echoPin 18
#define trigPin 19
#include "esp32-hal-ledc.h"
#define COUNT_LOW 1638
#define COUNT_HIGH 7864
#define CLOCK_HERTZ 50
#define TIMER_WIDTH 16
#define fleftPin 22
#define frightPin 23
#define bleftPin 17
#define brightPin 5
#define fleftServo 1
#define frightServo 2
#define bleftServo 3
#define brightServo 4
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"
#define BLYNK_AUTH_TOKEN "xxx"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "xxx";
char pass[] = "xxx";
BlynkTimer timer;
void setup()
{
Serial.begin(115200);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
ledcSetup(fleftServo, CLOCK_HERTZ, TIMER_WIDTH);
ledcAttachPin(fleftPin, fleftServo);
ledcSetup(frightServo, CLOCK_HERTZ, TIMER_WIDTH);
ledcAttachPin(frightPin, frightServo);
ledcSetup(bleftServo, CLOCK_HERTZ, TIMER_WIDTH);
ledcAttachPin(bleftPin, bleftServo);
ledcSetup(brightServo, CLOCK_HERTZ, TIMER_WIDTH);
ledcAttachPin(brightPin, brightServo);
Blynk.begin(auth, ssid, pass);
timer.setInterval(1000L, sendDist);
}
void loop()
{
Blynk.run();
timer.run();
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V6, V7, V8, V9, V10, V11, V12);
}
BLYNK_WRITE(V6)
{
int value6 = param.asInt();
if (value6 == 0)
{
servoState == false;
}
else if (value6 == 1)
{
servoState == true;
}
}
BLYNK_WRITE(V7)
{
int value7 = param.asInt();
if (value7 == 0)
{
modeESP == false;
}
else if (value7 == 1)
{
modeESP == true;
}
}
BLYNK_WRITE(V8)
{
int value8 = param.asInt();
if (value8 == 0)
{
servoForward == false;
}
else if (value8 == 1)
{
servoForward == true;
}
}
BLYNK_WRITE(V9)
{
int value9 = param.asInt();
if (value9 == 0)
{
servoBackward == false;
}
else if (value9 == 1)
{
servoBackward == true;
}
}
BLYNK_WRITE(V10)
{
int value10 = param.asInt();
if (value10 == 0)
{
servoLeft == false;
}
else if (value10 == 1)
{
servoLeft == true;
}
}
BLYNK_WRITE(V11)
{
int value11 = param.asInt();
if (value11 == 0)
{
servoRight == false;
}
else if (value11 == 1)
{
servoRight == true;
}
}
BLYNK_WRITE(V12)
{
int value12 = param.asInt();
if (value12 == 0)
{
servoStop == false;
}
else if (value12 == 1)
{
servoStop == true;
}
}
void sendDist()
{
float dist = calDist();
Blynk.virtualWrite(V4, dist);
}
float calDist()
{
long duration;
float dist;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
dist = duration * 0.034 / 2;
controlServo(dist);
return dist;
}
void controlServo(float dist)
{
if (modeESP == false)
{
if (dist <= 10)
{
left();
}
else if (dist > 10)
{
forward();
}
}
else if (modeESP == true)
{
stop();
if (servoState == true)
{
if (servoForward == true)
{
forward();
}
else if (servoBackward == true)
{
backward();
}
else if (servoLeft == true)
{
left();
}
else if (servoRight == true)
{
right();
}
else if (servoStop == true)
{
stop();
}
}
else if (servoState == false)
{
stop();
}
}
}
void forward()
{
for (int i = COUNT_LOW; i < COUNT_HIGH; i = i + 100)
{
ledcWrite(fleftServo, i);
ledcWrite(bleftServo, i);
}
for (int i = COUNT_HIGH; i > COUNT_LOW; i = i - 100)
{
ledcWrite(frightServo, i);
ledcWrite(brightServo, i);
}
}
void backward()
{
for (int i = COUNT_HIGH; i > COUNT_LOW; i = i - 100)
{
ledcWrite(fleftServo, i);
ledcWrite(bleftServo, i);
}
for (int i = COUNT_LOW; i < COUNT_HIGH; i = i + 100)
{
ledcWrite(frightServo, i);
ledcWrite(brightServo, i);
}
}
void left()
{
for (int i = COUNT_HIGH; i > COUNT_LOW; i = i - 100)
{
ledcWrite(fleftServo, i);
ledcWrite(bleftServo, i);
}
for (int i = COUNT_HIGH; i > COUNT_LOW; i = i - 100)
{
ledcWrite(frightServo, i);
ledcWrite(brightServo, i);
}
}
void right()
{
for (int i = COUNT_LOW; i < COUNT_HIGH; i = i + 100)
{
ledcWrite(frightServo, i);
ledcWrite(brightServo, i);
}
for (int i = COUNT_LOW; i < COUNT_HIGH; i = i + 100)
{
ledcWrite(fleftServo, i);
ledcWrite(bleftServo, i);
}
}
void stop()
{
ledcWrite(frightServo, 0);
ledcWrite(brightServo, 0);
ledcWrite(fleftServo, 0);
ledcWrite(bleftServo, 0);
}