使用串行接口启动和停止步进电机

时间:2017-02-13 19:39:12

标签: arduino

这是我启动步进电机并尝试停止电机的代码:

#include <BasicStepperDriver.h>

#define DIR 34
#define STEP 36
#define ENBL 30

BasicStepperDriver stepper1(10000, DIR, STEP, ENBL);
BasicStepperDriver stepper2(10000, DIR, STEP, ENBL);

bool enableMotor1 = false;
bool enableMotor2 = false;

void rotate1() {
  Serial.println("Rotate start 1");
  stepper1.rotate(360);
  Serial.println("Rotate end 1");
}

void rotate2() {
  Serial.println("Rotate start 2");
  stepper2.rotate(360);
  Serial.println("Rotate end 2");
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  stepper1.enable();
  stepper1.setMicrostep(1);
  stepper1.setRPM(50);
  stepper2.enable();
  stepper2.setMicrostep(1);
  stepper2.setRPM(50);
}

void loop() {
  if(enableMotor1 == true) {
    rotate1();
  } else if(enableMotor2 == true) {
    rotate2();
  }  
  if (Serial.available ()) {
     String first  = Serial.readStringUntil(','); 
     Serial.read(); 
     String second = Serial.readStringUntil(','); 
     if(first == "start"){
        if(second == "1") {
          Serial.println("Starting motor 1");
          enableMotor1 = true;
        }
        if(second == "2") {
          Serial.println("Starting motor 2");
          enableMotor2 = true;
        }
     } else if(first == "stop") {
        if(second == "1") {
          Serial.println("Stopping motor 1");
          enableMotor1 = false;
          stepper1.disable();
        }
        if(second == "2") {
          enableMotor2 = false;
          Serial.println("Stopping motor 1");
          stepper2.disable();
        }
     }
  }
}

此代码的问题在于无法通过串行接口停止电机。目标是让步进电机运行,直到在串行监视器中输入stop命令。我的代码可能有什么问题?

此图显示我们可以启动电机但无法停止:

enter image description here

0 个答案:

没有答案