Blynk (legacy) button stop DC motor

I have a DC motor connected to a shield and a wemos d1 mini.
I made a force for the motor to rotate clockwise and counterclockwise.
I would like this loop to only work when the button widget is on.

But it’s not working because it stops checking the state of the button the moment it enters the FOR loop

i am using wemos d1 mini + wifi with Motor Shield

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

int pwm = 0;

//infos de conexão ao app
char auth[] = "xxxxxxx";
const char* ssid = "xxxxxxx";
const char* pass = "xxxxxxxxxx";

//Motor shiled I2C Address: 0x30
//PWM frequency: 1000Hz(1kHz)
Motor M1(0x30,_MOTOR_A, 1000);//Motor A

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();
}

int pinData;

WidgetLED led1(4); //virtual led 

BLYNK_WRITE(V1){
  pinData = param.asInt();  
  if(pinData == HIGH){
    led1.on();
    pwm = 50;
      }
    else{
      led1.off();
      pwm = 0;
      M1.setmotor(_STOP);
      }
  }

  
void setup() {
   Blynk.begin(auth, ssid, pass);
}

void loop() {
  Blynk.run();
  for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor( _CCW, pwm);      
    }
    
    M1.setmotor(_STOP);
  
    delay(5000);
    
    for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor(_CW, pwm);
    }
    
    M1.setmotor(_STOP);
  
    delay(5000);
}  

Can you help me how i do this?

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

1 Like

@Madhukesh thx for the link.

I changed the code according to the link sent but it still doesn’t work.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer; // Announcing the timer

int pwm = 0;

//infos de conexão ao app
char auth[] = "pw-vLctjiGIXOHubMcgAnIhiQaOktApS";
const char* ssid = "bazzinga_ext";
const char* pass = "jaqueline1801";

//Motor shiled I2C Address: 0x30
//PWM frequency: 1000Hz(1kHz)
Motor M1(0x30,_MOTOR_A, 1000);//Motor A

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();
}

int pinData;

WidgetLED led1(4); //virtual led 

BLYNK_WRITE(V1){
  pinData = param.asInt();  
  if(pinData == HIGH){
    led1.on();
    pwm = 50;
      }
    else{
      led1.off();
      pwm = 0;
      M1.setmotor(_STOP);
      }
  }

void controlmotor()
{
  for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor( _CCW, pwm);      
    }
    
    M1.setmotor(_STOP);
  
    delay(5000);
    
    for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor(_CW, pwm);
    }
    
    M1.setmotor(_STOP);
  
    delay(5000);
  }

void setup() {
   Blynk.begin(auth, ssid, pass);
   timer.setInterval(100L, controlmotor);
}

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

@Rubens_Junior you can search the forum, there’s a lots of examples

@John93 i search some examples but from what I saw none apply because I can’t get out of for to do the validation.

Because in my case I need the motor to run a specific routine and not just turn on and turn.

You cannot use delays in your code. Not only inside loop, but outside too. Because that will block the blynk from connecting to the cloud and will disconnect from the server and also the code may crash.

@Madhukesh in this case, how would i do it then, since i need the engine to be off for a while?

Timeout Timers…

Pete.

1 Like

I changed the code but it didn’t work

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer; // Announcing the timer

int pwm = 0;

//infos de conexão ao app
char auth[] = "xxxx";
const char* ssid = "xxxx";
const char* pass = "xxxx";

//Motor shiled I2C Address: 0x30
//PWM frequency: 1000Hz(1kHz)
Motor M1(0x30,_MOTOR_A, 1000);//Motor A

BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncAll();
}

int pinData;

WidgetLED led1(4); //virtual led 

BLYNK_WRITE(V1){
  pinData = param.asInt();  
  if(pinData == HIGH){
    led1.on();
    pwm = 50;
      }
    else{
      led1.off();
      pwm = 0;
      M1.setmotor(_STOP);
      }
  }

void controlmotor_ccw()
{
  for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor( _CCW, pwm);      
    }
    
    M1.setmotor(_STOP);
}

void controlmotor_cw(){
  for (pwm <= 98 && pinData == HIGH; pwm+=0.1;)
    {
      M1.setmotor(_CW, pwm);
    }
    
    M1.setmotor(_STOP);
  }

void setup() {
   Blynk.begin(auth, ssid, pass);
   timer.setInterval(5000L, controlmotor_ccw);
   timer.setInterval(5000L, controlmotor_cw);
}

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

That’s because you’re using interval timers declared in your void setup instead of a timeout timer in a lambda function within the function that previously contained your delays.

You didn’t read the correct part of the link.

Pete.

1 Like