我正在做一個工程,我用串行端口從我的電腦發送字符串到arduino,arduino讀取它們並根據字符串移動一個電機或另一個電機,這是驗證碼:用adafruit馬達屏蔽移動2個步進電機
#include <AccelStepper.h>
#include <AFMotor.h>
AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);
String steps= "";
void forwardstep1() {
motor1.onestep(FORWARD, SINGLE);
}
void backwardstep1() {
motor1.onestep(BACKWARD, SINGLE);
}
void forwardstep2() {
motor2.onestep(FORWARD, SINGLE);
}
void backwardstep2() {
motor2.onestep(BACKWARD, SINGLE);
}
AccelStepper stepper1(forwardstep1, backwardstep1);
AccelStepper stepper2(forwardstep2, backwardstep2);
void setup() {
Serial.begin(9600);
Serial.println("Stepper program!");
stepper1.setMaxSpeed(200.0);
stepper2.setMaxSpeed(200.0);
stepper1.setAcceleration(100.0);
stepper2.setAcceleration(100.0);
}
void loop() {
}
void serialEvent() {
while(Serial.available()) {
int inChar = Serial.read();
Serial.print("Echo:");
Serial.write(inChar);
if (isDigit(inChar)) {
steps += (char)inChar;
}
if (inChar == 'u') {
Serial.print("Up:" + steps);
Serial.println();
long POSITION = stepper1.currentPosition() + steps.toInt();
stepper1.runToNewPosition(POSITION);
steps = "";
Serial.println();
}
if (inChar == 'U') {
Serial.print("Up:" + steps);
Serial.println();
long POSITION = stepper2.currentPosition() + steps.toInt();
stepper2.runToNewPosition(POSITION);
steps = "";
Serial.println();
}
}
}
我的問題是,當我移動stepper1,然後嘗試移動,除非我重新設置的Arduino它不動吧的stepper2。
謝謝!