【发布时间】:2015-11-19 17:43:17
【问题描述】:
我正在尝试等待用户输入以启动程序操作,然后当用户发送停止命令时,循环停止运行。在循环运行时,我无法让串行端口继续读取输入。
所以我希望用户按 1,然后它会进入循环并显示来自中断的数据。但我希望它继续监控串行输入,所以当我输入 2 时,我将退出循环并停止打印到串行端口。
串口没有注册我的第二个输入。
我省略了一些代码,但重要的东西应该在那里。
int userStart = 0; // Holder for user input to start program
int userStop = 0; // Holder for user input to stop program
void setup() {
Serial.begin(115200);
pinMode(motorEncoderA, INPUT);
digitalWrite(motorEncoderA, HIGH); // Pull up resistor
pinMode(motorEncoderB, INPUT);
digitalWrite(motorEncoderB,HIGH); // Pull up resistor
// Interrupt on change of Pin A
attachInterrupt(digitalPinToInterrupt(2), encoderFunc, CHANGE);
Serial.print("Press 1 to start the Process & 2 to Stop");
}
void loop() {
if (Serial.available() > 0)
{
userStart = Serial.read();
if (userStart = 1) {
Serial.print('\n');
while(userStop != 2) {
unsigned long timee = millis();
// Only update if the shaft has moved
if (encoderPositionLast != rotationCounter) {
Serial.print("Time: ");
Serial.print(timee);
Serial.print(" Count: ");
Serial.print (rotationCounter);
Serial.print('\n');
encoderPositionLast = rotationCounter;
Serial.print(userStart);
}
if (Serial.available() > 0) {
userStop = Serial.read();
Serial.print(userStop);
}
}
}
}
【问题讨论】:
-
也许 Arduino beta 是更好的提问地点。 arduino.stackexchange.com
标签: arduino serial-port