Using virtual pin to start the instructions (activate motor controller)

When using a virtual pin to start the instructions
Switching the push / switch button mode does not occur.
I understand that I rewrite the registers and understand that this should be so.
but what to do ?
There are no assignments for a single button of several pins in BLYNK, there is no merging of the buttons either.
And you need to control the engines in push mode.
Bring a piece of sketch that I use.

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   servoX.attach(12);
   servoY.attach(13);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
}
BLYNK_WRITE(V1)
{
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
}

Welcome to the Blynk Forum.

I had to edit your post to correct the posted code for proper viewing, as shown in the Forum’s Welcome Topic…

As for you question… well… I didn’t understand your question :confused: But it seems you are unsure of what a virtual pin is, and how to use it. So here is a link to the Blynk Help Pages; It and other great resources are available at the upper right of this page.

http://help.blynk.cc/blynk-basics/what-is-virtual-pins

Question.
How to make 2 engines (4 driven pins) work on a virtual button in push mode
(Hold the button, the engines spin in the right direction, release-stop).

Rather simple

Add a button widget, tie it to a virtual pin, say V1.
Then add the corresponding blynk write function that reacts to your button press.

BLYNK_WRITE(V1) 
{
  // 1 while held 0 on release 
  int btnValue = param.asInt();
   // do if statements 
  //add your digitalWrites to the pins connected to your peripherals. 
} 

It’s all done.
Only in PUSH mode this does not work.


// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
Servo servoX;
Servo servoY;
int IN1 = 0;
int IN2 = 2;
int EN1 = 5;
int EN2 = 4;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   servoX.attach(12);
   servoY.attach(13);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
}
BLYNK_WRITE(V1)
{
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
}
BLYNK_WRITE(V20)
{
digitalWrite(EN1, HIGH);
digitalWrite(EN2, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
BLYNK_WRITE(V21)
{
digitalWrite(EN1, LOW);
digitalWrite(EN2, HIGH);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
BLYNK_WRITE(V22)
{
digitalWrite(EN1, HIGH);
digitalWrite(EN2, LOW);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
BLYNK_WRITE(V23)
{
digitalWrite(EN1, LOW);
digitalWrite(EN2, LOW);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
}
BLYNK_WRITE(V3)
{
  servoX.write(param.asInt());
}
BLYNK_WRITE(V4)
{
  servoY.write(param.asInt());
}
void loop()
{
  Blynk.run();
}

Since I love to shortcode things… here is a suggestion to help write less lines of code :slight_smile:

Add this function below setup()

// function to change digitalWrites easily
void doWrites(bool varIN1, bool varIN2, bool varEN1, bool varEN2){
  digitalWrite(IN1, varIN1);
  digitalWrite(IN2, varIN2);
  digitalWrite(EN1, varEN1);
  digitalWrite(EN2, varEN2);
}

then in your code, replace all those digitalwrites with a single line of code:

BLYNK_WRITE(V20){
  doWrites(LOW, LOW, HIGH, HIGH);
}

Hope this is handy!

1 Like

Thank you.
But this does not solve the problem :joy:

It does not work because you are missing logic in your writes.

When you push a button, no matter the buttons state, you will always do the same digital writes. You need an if statement that takes care of your button release that stops everything.

Push mode works like this, when you push and hold a button, BLYNK_WRITE is called and executed once, when you release said button it is called again.

So

First you need a function that stops your motors.

void stopAll() 
{
   digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, LOW);
} 

Then edit your writes to this, I’ll just take one for an example:

BLYNK_WRITE(V1)
{
   if (param.asInt() == 1)
   {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, HIGH);
   } 
    else
       stopAll();
}

Now edit the rest to achieve your wish

1 Like

And I recommend putting those BLYNK_WRITE() commands in a timed loop, so that you don’t block Blynk’s internal function, Blynk.run(), and end up disconnecting your device by holding a button down for more that a second or two.

http://docs.blynk.cc/#troubleshooting-flood-error

http://docs.blynk.cc/#blynk-firmware-blynktimer

Fettkeewl
Thank you.
Push earned as it should.

The following problem arose.
When the first button is operated in push mode, pressing the second button disrupts the operation of the first button until it is pressed again
That is, the button one remains pressed but the command does not pass from it, you need to release it and press the button again.

You would need to add more logic and commands to make your motor control offer widget “feedback”, state change, etc.

e.g. If you are using the buttons in switch mode, then when one button is pressed “on” you could add commands for the other buttons to change their state to “off” via Blynk.virtualWrite(vPin, 0). Or just set the buttons to momentary mode. either way you might also want to include logic that prevents conflicting actions when multiple buttons are pressed.

http://docs.blynk.cc/#blynk-firmware-virtual-pins-control-blynkvirtualwritevpin-value

I pressed the button 1-completed the set of commands.
Pressed button 2 (button 1 did not release) performed the command set of button 2-released button 2 (button 1 pressed).
Why is not reading and executing the buttons 1? Why in order to make it work I need to release it and press again?
There are no conflicts, one of the four pins from 0 to 1 and vice versa.
How to fix it.
Thanks for the help.

Show your updated code, otherwise we are just guessing.

Excuse me.
Here is the code.

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
Servo servoX;
Servo servoY;
int IN1 = 0;
int IN2 = 2;
int EN1 = 5;
int EN2 = 4;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
   servoX.attach(12);
   servoY.attach(13);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(EN2, OUTPUT);
}
void stopAll() 
{
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, LOW);
}
BLYNK_WRITE(V1)
{
   if (param.asInt() == 1)
   {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, HIGH);
   } 
    else
       stopAll();
}
BLYNK_WRITE(V20)
{
   if (param.asInt() == 1)
   {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, LOW);
   } 
    else
       stopAll();
}
BLYNK_WRITE(V21)
{
   if (param.asInt() == 1)
   {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, LOW);
      digitalWrite(IN2, HIGH);
   } 
    else
       stopAll();
}
BLYNK_WRITE(V22)
{
   if (param.asInt() == 1)
   {
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, LOW);
   } 
    else
       stopAll();
}
BLYNK_WRITE(V3)
{
  servoX.write(param.asInt());
}
BLYNK_WRITE(V4)
{
  servoY.write(param.asInt());
}
void loop()
{
  Blynk.run();
}

This is just a suggestion… messy code and untested, but should give you something to work with…

BLYNK_WRITE(V1)
{
   if (param.asInt() == 1)
   {
      Blynk.virtualWrite(V20, 0);  // sets other button V20 to OFF
      Blynk.virtualWrite(V21, 0);  // sets other button V21 to OFF
      Blynk.virtualWrite(V22, 0);  // sets other button V22 to OFF 
      digitalWrite(EN1, HIGH);
      digitalWrite(EN2, HIGH);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, HIGH);
   } 
    else
       stopAll();
}

It will be even worse.
I need to steer caterpillars when moving forward or backward without stopping traffic.

So… research and experiment until it works. You are not providing any commenting in your code, so I just randomly picked a function to show how you might do something.

We (other Blynk users) are generally not here to write each others code, more like offer suggestions, tips and tricks, share ideas, guidance, etc.

Have you searched this forum for other topics using robot, car, motor control and other key words? If not, try it… there are lots of them, many with code snippets :wink:

PS. Instead of only using buttons with hard stops and starts, try sliders or the joystick widget, and appropriate motor controller code, to vary the Left/Right motor speeds to accomplish rolling turns.

Change the speed and smoothly manage it easily.
In the code for this there are 2 servo to them are connected esc and
Everything works in two directions and at different speeds.
On two virtual pins with different widgets.
I want to deal with L293D.
Searches unfortunately do not give anything.
Thanks for the advice.

Yup, absolutely nothing :wink: