Arduino仅执行一组If-Else
此代码如果用于简单的机器人车。 我正在尝试使用4个齿轮电动机和L289驱动器和标准RC TX/RX控制机器人。
我已经使用了一些打印语句来调试任何错误。
当我尝试向前/向后移动机器人时,我可以看到串行监视器打印froward/向后移动,但机器人不会移动。
当我尝试左/右移动时,它可以正常工作。在评论代码中的左右移动语句时,机器人确实向前和向后移动,但没有使用其他语句的所有内容。 这是代码。
//Receiver pin
byte CH1_PIN = 9;
byte CH2_PIN = 10;
//Motor driver pins
int left_motor_pin1 = 4;
int left_motor_pin2 = 5;
int right_motor_pin1 = 6;
int right_motor_pin2 = 7;
void setup() {
// put your setup code here, to run once:
pinMode(CH1_PIN, INPUT);
pinMode(CH2_PIN, INPUT);
pinMode(left_motor_pin1, OUTPUT);
pinMode(left_motor_pin2, OUTPUT);
pinMode(right_motor_pin1, OUTPUT);
pinMode(right_motor_pin2, OUTPUT);
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int ch_1 = pulseIn(CH1_PIN, HIGH);
int ch_2 = pulseIn(CH2_PIN, HIGH);
drive(ch_1, ch_2);
delay(5);
}
void drive(int move_left_right, int move_fwd_back) {
// Set direction for moving forward
if ( move_fwd_back > 1700 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("forward");
}
// Set direction for moving backwards.
else if (move_fwd_back < 1300) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("reverse");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
// Set direction for moving left
if ( move_left_right < 1300 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("left");
}
//set directionfor moving right
else if (move_left_right > 1700) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("right");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
}
This code if for a simple robot car.
I'm trying to control the robot with 4 geared motors and L289 driver and standard RC Tx/Rx.
I have used some print statements to debug any errors.
When I try to move the robot forward/backward, I can see serial monitor printing froward/backward, but the robot doesn't move.
When I try to move if left/right it works fine. On commenting the left-right moving statements in code the robot does move forward and backward but fails to do so with all the if else statements uncommented.
Here's the code.
//Receiver pin
byte CH1_PIN = 9;
byte CH2_PIN = 10;
//Motor driver pins
int left_motor_pin1 = 4;
int left_motor_pin2 = 5;
int right_motor_pin1 = 6;
int right_motor_pin2 = 7;
void setup() {
// put your setup code here, to run once:
pinMode(CH1_PIN, INPUT);
pinMode(CH2_PIN, INPUT);
pinMode(left_motor_pin1, OUTPUT);
pinMode(left_motor_pin2, OUTPUT);
pinMode(right_motor_pin1, OUTPUT);
pinMode(right_motor_pin2, OUTPUT);
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int ch_1 = pulseIn(CH1_PIN, HIGH);
int ch_2 = pulseIn(CH2_PIN, HIGH);
drive(ch_1, ch_2);
delay(5);
}
void drive(int move_left_right, int move_fwd_back) {
// Set direction for moving forward
if ( move_fwd_back > 1700 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("forward");
}
// Set direction for moving backwards.
else if (move_fwd_back < 1300) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("reverse");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
// Set direction for moving left
if ( move_left_right < 1300 ) {
digitalWrite(left_motor_pin1, HIGH);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, HIGH);
Serial.println("left");
}
//set directionfor moving right
else if (move_left_right > 1700) {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, HIGH);
digitalWrite(right_motor_pin1, HIGH);
digitalWrite(right_motor_pin2, LOW);
Serial.println("right");
}
else {
digitalWrite(left_motor_pin1, LOW);
digitalWrite(left_motor_pin2, LOW);
digitalWrite(right_motor_pin1, LOW);
digitalWrite(right_motor_pin2, LOW);
Serial.println("NONE");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您有两个
if -else
条件 - 都更改相同的输出。因此,第二个if-else
条件将始终覆盖第一章所做的事情。例如。如果您希望电动机仅向前移动,则代码将将电动机设置为两者都向前移动 - 但是,此后,代码立即决定没有左/右输入,因此将电动机设置为停止。这太快了,您看不到电动机中的任何动作。
首先,我将更改代码,以便有关左/右输入的决定在前/向后条件的其他条件内。这将使向前/向后输入优先级在左/右输入上。
IE
The issue is that you have two
if-else
conditions - both changing the same outputs. So the 2ndif-else
condition will always override what the 1st one has done.eg. if you want the motor to just move forward, the code would set the motors to both move forward - however, immediately afterwards, the code decides there is no left/right input so sets the motors to stop. This is so fast you don't see any movement in the motors.
To start with, I would change the code so that the decision regarding the left/right input is inside the else condition of the forward/backward condition. This would give the forward/backward input priority over the left/right input.
i.e.