Arduino 只执行一组 if-else

Arduino only executing one set of if-else

此代码适用于简单的机器人汽车。 我正在尝试使用 4 个减速电机和 L289 驱动器以及标准 RC Tx/Rx.

来控制机器人

我使用了一些打印语句来调试任何错误。

当我尝试移动机器人 forward/backward 时,我可以看到串行监视器打印 froward/backward,但机器人没有移动。

当我尝试移动时 left/right 它工作正常。在注释代码中的左右移动语句时,机器人确实向前和向后移动,但在所有 if else 语句未注释的情况下无法这样做。 这是代码。

//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");
  }


}

问题是您有两个 if-else 条件 - 都改变了相同的输出。所以第二个 if-else 条件将始终覆盖第一个条件所做的。

例如。如果您希望电机向前移动,代码会将电机设置为同时向前移动 - 然而,紧接着,代码决定没有 left/right 输入,因此将电机设置为停止。这太快了,你看不到电机有任何运动。

首先,我将更改代码,以便关于 left/right 输入的决定在 forward/backward 条件的 else 条件内。这将使 forward/backward 输入优先于 left/right 输入。

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 {
    // 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");
    }
}