1

I was trying this code to run stepper motors and print Blynk app joystick's X and Y coordinates. But the code is only getting the joystick values once. But it works fine when I use if condition instead of while(). I needed while condition to run stepper motors continuously but with if condition they turn off and on very quickly decrease the speed of stepper motors.

Please help me deal this situation

Code

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <AccelStepper.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "LRTCZUnCI06P-pqh5rlPXRbuOUgQ_uGH";

AccelStepper stepper_1(1,D2,D1);
AccelStepper stepper_2(1,D5,D6);

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

int prev_x = 0;
int prev_y = 0;

BLYNK_WRITE(V1) {
  int x = param[0].asInt();
  int y = param[1].asInt();

  // Do something with x and y
  Serial.print("X = ");
  Serial.print(x);
  Serial.print("; Y = ");
  Serial.println(y);

  MoveControls(x , y);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  stepper_1.setAcceleration(1000);
  stepper_2.setAcceleration(1000);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
// ESP.wdtDisable();
// ESP.wdtEnable(WDTO_8S);
}

void loop()
{// ESP.wdtFeed();


  Blynk.run();
 // Serial.println("A");
}

void MoveControls(int x, int y) {

 ///////////////////////////////////////////Move Forward////////////////////////////////////////////////////

  if(y >= 150 && x <= 150 && x >= -150){
    stepper_1.enableOutputs();
    stepper_2.enableOutputs();

    stepper_1.setMaxSpeed(1000);
    stepper_2.setMaxSpeed(1000);

    stepper_1.move(1000);
    stepper_2.move(1000);

    while(x != prev_x && y != prev_y){

    stepper_1.run();
    stepper_2.run();

    Blynk.syncVirtual(V1);
    Blynk.run();

    Serial.print("X = ");
    Serial.print(x);
    Serial.print("; Y = ");
    Serial.println(y);
    }

    prev_x = x;
    prev_y = y;

  }


 ///////////////////////////////////////////Neutral Zone////////////////////////////////////////////////////  
 if(y <= 150 && x <= 150 && x >= -150 && y >= -150){
    stepper_1.disableOutputs();
    stepper_2.disableOutputs();

  prev_x = x;
  prev_y = y;  
  }


  yield();
  }

This is that while loop

while(x != prev_x && y != prev_y){

    stepper_1.run();
    stepper_2.run();

    Blynk.syncVirtual(V1);
    Blynk.run();

    Serial.print("X = ");
    Serial.print(x);
    Serial.print("; Y = ");
    Serial.println(y);
    }

    prev_x = x;
    prev_y = y;

  }

I know that the code is very much wrong and doesn't make much sense but I need to fix only the Blynk.syncVirtual() issue.

I also tried to add Blynk.run() after Blynk.syncVirtual() because someone told in Blynk Community to do that https://community.blynk.cc/t/blynk-syncvirtual-doesnt-work-as-expected/40047/4

  • don't call Blynk.syncVirtual(V1); . Blynk will call BLYNK_WRITE(V1) without it, if you move the joystick. with syncVirtual in loop you flood the server and it blocks you out – Juraj May 29 '20 at 12:45
  • But if I don't use Blynk.syncVirtual then Blynk track the joystick values only once the whole loop that causes the stepper motors to stop, that's why I need to see the values of Joystick again and again in the while loop – Electro Tech May 29 '20 at 12:47
  • you have `Blynk.syncVirtual(V1);` in loop() snippet in the question – Juraj May 29 '20 at 12:55
  • I have added it while loop so that it could check again and again the value and leave the while loop if they cause condition to be false – Electro Tech May 29 '20 at 12:58
  • I see. that is even worse. Blynk ignores nested run(). BLYNK_WRITE is called from run() in lop() – Juraj May 29 '20 at 13:41
  • So is there any way to achieve what a I want? – Electro Tech May 29 '20 at 13:47
  • I don't know. Start again with the joystick example and use the joystick position in loop to control the motors without blocking the loop() – Juraj May 29 '20 at 14:02
  • Can you please elaborate – Electro Tech May 29 '20 at 14:04
  • I can't. I know Blynk but I never controlled motors with Arduino – Juraj May 29 '20 at 14:06
  • the code that I have added in the question above is the example code for joystick by Blynk only that I edited and expanded to our need – Electro Tech May 29 '20 at 14:07
  • Is there any other way through which I can control stepper motors using mobile? – Electro Tech May 29 '20 at 14:07
  • I know that you expanded the example. but you did it a wrong way. you shouldn't call Blynk.run() or stay in a while loop in BLYNK_WRITE(V1). in BLYNK_WRITE(V1) you should only store the values and then use them in loop() or in a function called from loop() – Juraj May 29 '20 at 14:14
  • @Juraj please help me run the stepper motors, they are very necessary and I am not getting anything on Google to operate them, if you know anyone who can help then please tell him too, it will be a great help for me – Electro Tech May 29 '20 at 14:17
  • My main objective is to run them using Mobile Joysticks instead of real Joysticks – Electro Tech May 29 '20 at 14:17
  • do you have a not blocking example code to run a motor with a real joystick? "not blocking" doesn't have delays or nested loops. it lets the loop() loop – Juraj May 29 '20 at 16:49
  • drive.google.com/file/d/19f63MZwvLdy55UaKmNaILDVRS_cs2MPL/… – Electro Tech May 29 '20 at 18:18

2 Answers2

0

In your while loop you're setting prev_x = x && prev_y = y which is satisfying the condition of the while loop

Megarev
  • 50
  • 7
  • Thank You for responding. I wanted to read the Virtual Pins inside this while loop so that the values of x and y change . Please tell me how can I read virtual pin of a joystick inside this while loop – Electro Tech May 29 '20 at 11:17
  • Why do you need a `prev_x` and `prev_y` variable? – Megarev May 29 '20 at 11:23
  • so that when prev_x and prev_y are not needed specificly. I will use these variable for defining the neutral zone in the joystick so whenever we leave the joystick to 0,0 the stepper motors come to rest – Electro Tech May 29 '20 at 11:25
  • The check for the joystick that you have done afterwards, try making that as the condition for your while loop and see if that works, also what is `loop()` method? – Megarev May 29 '20 at 11:28
  • Sorry I didn't get you, can you please elaborate more or send the small code – Electro Tech May 29 '20 at 11:29
0

Does this work?

while(y <= 150 && x <= 150 && x >= -150 && y >= -150) {

stepper_1.run();
stepper_2.run();

Blynk.syncVirtual(V1);
Blynk.run();

Serial.print("X = ");
Serial.print(x);
Serial.print("; Y = ");
Serial.println(y);
}

//prev_x = x;
//prev_y = y;
Megarev
  • 50
  • 7