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