我的目标是让我的机器人根据一系列可以通过连续接收的值进行各种不同的动作。到目前为止,我的代码看起来像这样:
void loop() {
pulseWidth = pulseIn(9, HIGH); // Count how long the pulse is high in microseconds
int Lidar = pulseWidth / 10;
if (Serial.available()) {
int ch = Serial.parseInt();
if (ch >= 340 && ch <= 10) {
if ((Lidar > 50)) {
//trucking for the beacon
Serial.println(Lidar);
driveArdumoto(MOTOR_A, REVERSE, 255);
driveArdumoto(MOTOR_B, FORWARD, 255);
} if (Lidar <= 50) {
//Object detected on left side of patricia
Serial.println(Lidar);
driveArdumoto(MOTOR_A, FORWARD, 255);
driveArdumoto(MOTOR_B, REVERSE, 255);
}
} else if (ch < 340 && ch > 10) {
stopArdumoto(MOTOR_A); // STOP motor B
stopArdumoto(MOTOR_B);
}
}
}
到目前为止,当我向Arduino发送一个值时,它似乎不起作用。我也试过Serial.read()
。我出错的任何想法?