Hey I am working on a project in which I have to run several stepper motors using ESP32 Devkit module and Blynk via WIFI.
Every stepper motor is triggered by a virtual pin and I am using a while loop to run the motor as long as the button on the Blynk app is pressed and as soon as the button is released the motor comes to a stop.
The problem being faced is that once a button on the app is pressed and the control enters the while loop, it does not seem to break out of the loop when the button is released.
I have attached a code for one stepper motor below, which contains two BLYNK_WRITE(V?) functions to move the motor in either direction.
Let me know if there are any more questions.
#define BLYNK_PRINT Serial
#include <StepperDriver.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// You should get Auth Token in the Blynk App.
char auth[] = "Your auth token";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Your SSID";
char pass[] = "Your Password";
int motor_steps = 200;
int step_divisition = 16;
int en1 = 34; int cw1 = 35; int clk1 = 32;
StepperDriver ss(motor_steps, step_divisition, en1, cw1, clk1); //CONVEYOR MOTOR
void setup()
{
// Debug console
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
BLYNK_WRITE(V3) //CONVEYOR FORWARD
{
int value3 = param.asInt();
while (value3 == 1)
{
ss.setSpeed(160);
ss.step(1);
}
}
BLYNK_WRITE(V4) //CONVEYOR REVERSE
{
int value4 = param.asInt();
while (value4 == 1)
{
ss.setSpeed(160);
ss.step(-1);
}
}
void loop()
{
Blynk.run();
}
motor run forward until V3 is release
motor run reverse until V4 is release
So what will happen if you press V3 and V4 simultaneously ?
I think, you missed a step , there is no stop condition.
if V3 and V4 are release, ss.setSpeed(0);
But I never used motor stepper
So I don’t know if you need to set speed to zero to stop the motor stepper
Hey Blynk_Coeur, thanks for looking into this.
The motor will only move when the ss.step(x) function is called. Otherwise, the motor will remain stationary. And ye you identifies correctly, V3 and V4 are in push mode and I cannot use the blynk timer as I do not want the motor to run for a specific amount of time but as long as the button is pressed. When the button is released the motor will stop by itself, but for stopping it needs to come out of the while loop.
Is there any way?
Thanks Blynk_Coeur and khoih, it really worked for me. I hope one esp32 board can handle 7 timers simultaneously because I have 7 motors attached to one board.
I managed to run 3 timers at a time, I guess 7 would be no problem either.
Thanks again!