While Loop in BLYNK_WRITE(V?) function

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();
}

use the blynk timer instead of the while loop, otherwise you will never get out of the loop and blynk will also disconnect!

1 Like

I assume that V3 and V4 are in push mode ?

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 ? :wink:
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 :upside_down_face:
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?

1 Like

Give it a try :wink:

```
#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";

BlynkTimer timer; //<--- don't forget !

int motorSpeed;
int motorDirection;
int value3 , value4; // init button value3&4


void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(10, stepperControl); // updates stepper control every 10ms
}


void loop() {
  Blynk.run();
  timer.run();
}

BLYNK_WRITE(V3)            //CONVEYOR FORWARD
{
  value3 = param.asInt();
}

BLYNK_WRITE(V4)            //CONVEYOR REVERSE
{
  value4 = param.asInt();
}

void stepperControl() {
  if (value3 == 0 || value4 == 0) {  //motor STOP
    ss.setSpeed(0);
    ss.step(0);
  } else if (value3 == 1 || value4 == 0) { //motor FORWARD
    ss.setSpeed(160);
    ss.step(1);
  } else if (value3 == 0 || value4 == 1) {//motor REVERSE
    ss.setSpeed(160);
    ss.step(-1);
  } else if (value3 == 1 || value4 == 1) { //motor STOP
    ss.setSpeed(0);
    ss.step(0);
  }
}

I guess @Blynk_Coeur means

void stepperControl() {
  if (value3 == 0 && value4 == 0) {  //motor STOP
    ss.setSpeed(0);
    ss.step(0);
  } else if (value3 == 1 && value4 == 0) { //motor FORWARD
    ss.setSpeed(160);
    ss.step(1);
  } else if (value3 == 0 && value4 == 1) {//motor REVERSE
    ss.setSpeed(160);
    ss.step(-1);
  } else if (value3 == 1 && value4 == 1) { //motor STOP
    ss.setSpeed(0);
    ss.step(0);
  }
}

You have to try with longer timer if getting issues with Blynk.

But it’s always better to to interrupt with debouncing.

1 Like

Oops
Yes && !

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!

You’re welcome.
About timers, you have to stage them , else you’ll flood blynk.