1
我有以下代碼,但只能在通電時一次發送m1 on
。之後,我的arduino忽略了發送給它的串行數據。只能讀取一次串行數據
謝謝你的幫助。
#include <AccelStepper.h>
AccelStepper stepper(1, 3, 2);
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup()
{
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(1000);
stepper.setCurrentPosition(0);
Serial.begin(9600); // Begin serial communiation at 9600.
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0;
return(0);
}
else {
return(1);
}
}
void loop()
{
if (Comp("m1 on")==0) {
Serial.write("Motor 1 -> Online\n");
}
if (Comp("m1 off")==0) {
Serial.write("Motor 1 -> Offline\n");
}
}
什麼是您的串行輸出? –
輸入'm1 on'或'm1 off'會產生相應的'Motor 1 - > Online \ n'或'Motor 1 - > Offline \ n'響應,但這隻能執行一次。 – mh00h
然後你陷入無限循環。強制你的while循環在幾次迭代之後中斷以驗證這一點。 –