Search code examples
arduinoroboticsmotordriver

Arduino only executing one set of if-else


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


}

Solution

  • The issue is that you have two if-else conditions - both changing the same outputs. So the 2nd if-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.

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