0
私は、シリアルポート(AccelStepperライブラリを使用して)でステッピングモータを制御する短いテストコードを用意しました。コードは3つのコマンドM-を使用して定義された位置にモータを移動し、H - 操作を中止し、P-を使用して現在のモータステップの存在をチェックします。それぞれの表彰は "#"で終わらなければなりません。だから私はM500#を書いているときは、モーターが動くまで移動が始まることを意味する500. 私の問題/疑問はこの操作を中止することですか?シリアルポートは、RunToPosiotion機能が終了するまでブロックされます。機能を中止する方法RunToPosition - AccelStepper/Arduino
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper stepper = AccelStepper(AccelStepper::HALF4WIRE, 4, 5, 6, 7);
String MyString;
String Command;
int Value;
void setup() {
Serial.begin(57600);
stepper.setMaxSpeed(400);
stepper.setAcceleration(600);
stepper.setCurrentPosition(100);
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '#') {
SerialCommand(MyString);
MyString = "";
}
else {
MyString += inChar;
}
}
}
void loop() {
}
void SerialCommand(String ASCOM_Command) {
Command = String(ASCOM_Command.charAt(0));
String Txt_Value = ASCOM_Command.substring(1);
Value = Txt_Value.toInt();
/*Commands, an example: M500# - start motor to move until get 500 position
P - check actual focuser position
M - Move to specific position called by client application
H - Halt/Abort
*/
switch (ASCOM_Command.charAt(0)) {
case 'P':
Serial.println(stepper.currentPosition());
break;
case 'M':
stepper.moveTo(Value);
stepper.runToPosition();
break;
case 'H':
stepper.stop();
Serial.println("H#");
break;
}
}