Blynk_Write produces strange behavior with motor control

Hello,
I try to control a small robor with 2 motors with blynk. Its working basicly. But when I change direction, it always wants to continue the last direction. For example if I drive forward then press left, it still drives forward. Just when I press left once more (push mode button) it will drive to the left. I tried working with some delays no luck so far. Thank you :slight_smile:

#define BLYNK_DEVICE_NAME "XXX"
#define BLYNK_AUTH_TOKEN "XXX"

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

#define RightMotorSpeed       D5   //14 
#define RightMotorDir         D6   //12 
#define LeftMotorSpeed        D7   //13 
#define LeftMotorDir          D8   //15 


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "XXX";
char pass[] = "XXX";



// FORWARD
BLYNK_WRITE(V1) {
int button1 = param.asInt(); // read button
if (button1 == 1) {Serial.println("Moving forward"); digitalWrite(RightMotorDir,HIGH); digitalWrite(RightMotorSpeed,HIGH); digitalWrite(LeftMotorDir,HIGH); digitalWrite(LeftMotorSpeed,HIGH);

}  else 

{    Serial.println("Stop forward"); digitalWrite(RightMotorSpeed,LOW); digitalWrite(LeftMotorSpeed,LOW);
}
}

// BACKWARD
BLYNK_WRITE(V2) {
int button2 = param.asInt(); // read button
if (button2 == 2) {Serial.println("Moving backward"); digitalWrite(RightMotorDir,LOW); digitalWrite(RightMotorSpeed,HIGH); digitalWrite(LeftMotorDir,LOW); digitalWrite(LeftMotorSpeed,HIGH); 

}  else 

{    Serial.println("Stop backward"); digitalWrite(RightMotorSpeed,LOW); digitalWrite(LeftMotorSpeed,LOW);
}
}

// LEFT
BLYNK_WRITE(V3) {
int button3 = param.asInt(); // read button
if (button3 == 3) {Serial.println("Moving left"); digitalWrite(LeftMotorSpeed,LOW); digitalWrite(LeftMotorDir,LOW); digitalWrite(RightMotorDir,HIGH); digitalWrite(RightMotorSpeed,HIGH);  
 
}  else 

{    Serial.println("Stop left"); digitalWrite(RightMotorSpeed,LOW); digitalWrite(LeftMotorSpeed,LOW);
}
}

// RIGHT
BLYNK_WRITE(V4) {
int button4 = param.asInt(); // read button
if (button4 == 4) {Serial.println("Moving right"); digitalWrite(RightMotorSpeed,LOW); digitalWrite(RightMotorDir,LOW); digitalWrite(LeftMotorDir,HIGH); digitalWrite(LeftMotorSpeed,HIGH);  

}  else 

{    Serial.println("Stop right"); digitalWrite(RightMotorSpeed,LOW); digitalWrite(LeftMotorSpeed,LOW);

}
}





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

    pinMode(RightMotorSpeed, OUTPUT);
    pinMode(LeftMotorSpeed, OUTPUT);
    pinMode(RightMotorDir, OUTPUT);
    pinMode(LeftMotorDir, OUTPUT);

}


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

Have you reconfigured the values sent by the buttons in BLYNK. You mention push mode button but do not give details on how they are configured.

You have

int button3 = param.asInt(); // read button 
if (button3 == 3)

normally the button would output a 1 when pressed.

Also, probably not related to your issue, but it may be good to add some initial conditions for your pins as well in the setup. Just to eliminate the possibility of the motors running upon boot. I suspect you would only need to do this with the speed parameter, but as no information was given on the controller, this is just an assumption.

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

    pinMode(RightMotorSpeed, OUTPUT);
    pinMode(LeftMotorSpeed, OUTPUT);
    pinMode(RightMotorDir, OUTPUT);
    pinMode(LeftMotorDir, OUTPUT);
    
    digitalWrite(RightMotorSpeed,LOW); 
    digitalWrite(LeftMotorSpeed,LOW);

}

What do you see in your serial monitor?

Pete.