1

我正在尝试使用此代码来运行步进电机并打印 Blynk 应用程序操纵杆的 X 和 Y 坐标。但是代码只获取一次操纵杆值。但是当我使用 if 条件而不是 while() 时它工作正常。我需要 while 条件来连续运行步进电机,但在 if 条件下它们会关闭并很快降低步进电机的速度。

请帮我处理这种情况

代码

#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();
  }

这就是while循环

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;

  }

我知道代码非常错误并且没有多大意义,但我只需要修复 Blynk.syncVirtual() 问题。

我还尝试在 Blynk.syncVirtual() 之后添加 Blynk.run() 因为有人在 Blynk 社区告诉这样做 https://community.blynk.cc/t/blynk-syncvirtual-doesnt-work-as-expected/40047 /4

4

2 回答 2

0

这行得通吗?

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;
于 2020-05-29T11:31:31.637 回答
0

在您的 while 循环中,您正在设置prev_x = x&&prev_y = y满足 while 循环的条件

于 2020-05-29T11:14:56.403 回答