0

I am making a robot for a local robotics competition. One main function I need to do is move it forward, backward, right, left. Just like an RC car. I have programmed it to do that but it seems like the motors are not going the same speed as each other. When I move the stick just a little bit only one motor starts moving then when I go a bit further the only starts as well. I use an L293D and an Arduino UNO to control the 2 12v motors. I use a 6 channel receiver and transmitter for radio control. I think it might be something with the motor, but I might be missing something. Hopefully, someone can help me out. Thanks!

int motor1_in1 = 3; //H bridge direction pins
int motor1_in2 = 2; //H bridge direction pins
int motor2_in3 = 7; //H bridge direction pins
int motor2_in4 = 6; //H bridge direction pins

//the following are all ~PWM capable ports
int motor1_enable1 = 4; //H bridge motor 1 PWM PIN
int motor2_enable1 = 5; //H bridge motor 2 PWM PIN
int rc_channel2 = 10;
int rc_channel4 = 8;

void setup() {
  pinMode(rc_channel2, INPUT);
  pinMode(rc_channel4, INPUT);
  pinMode(motor1_in1, OUTPUT);
  pinMode(motor1_in2, OUTPUT);
  pinMode(motor1_enable1, OUTPUT);

  pinMode(motor2_in3, OUTPUT);
  pinMode(motor2_in4, OUTPUT);
  pinMode(motor2_enable1, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  motorMoveForward();
  motorMoveSide();
  // delay(100);
}


void motorMoveForward()  {
  int pwm = 0;
  int rc2 = pulseIn(rc_channel2, HIGH, 25000);

  if (rc2 == 0) {
    digitalWrite(motor1_in1, LOW);
    digitalWrite(motor1_in2, LOW);
    digitalWrite(motor2_in3, LOW);
    digitalWrite(motor2_in4, LOW);

    analogWrite(motor1_enable1, 0);
    analogWrite(motor2_enable1, 0);
  }
  else if (rc2 > 1520) { //Backwards
    pwm = map(rc2, 1520, 1910, 0, 255); //map our speed to 0-255 range
    digitalWrite(motor1_in1, LOW);
    digitalWrite(motor1_in2, HIGH);
    digitalWrite(motor2_in3, HIGH);
    digitalWrite(motor2_in4, LOW);

    analogWrite(motor1_enable1, pwm);
    analogWrite(motor2_enable1, pwm);

    Serial.print(" DOWN    : ");
    Serial.print(pwm);
    Serial.print("\t");
    Serial.print(rc2);

  }
  else if (rc2 < 1480) { //Forwards
    pwm = map(rc2, 1480, 1090, 0, 255); //map our speed to 0-255 range
    digitalWrite(motor1_in1, HIGH);
    digitalWrite(motor1_in2, LOW);
    digitalWrite(motor2_in3, LOW);
    digitalWrite(motor2_in4, HIGH);

    analogWrite(motor1_enable1, pwm);
    analogWrite(motor2_enable1, pwm);

    Serial.print(" UP     : ");
    Serial.print(pwm);
    Serial.print("\t");
    Serial.print(rc2);
  }
  else {
    digitalWrite(motor1_in1, LOW);
    digitalWrite(motor1_in2, LOW);
    digitalWrite(motor2_in3, LOW);
    digitalWrite(motor2_in4, LOW);

    analogWrite(motor1_enable1, 0);
    analogWrite(motor2_enable1, 0);

    Serial.print(" CENTER  : ");
    Serial.print(pwm);
    Serial.print("\t");
    Serial.print(rc2);
  }

}




void motorMoveSide()  {
  int pwm1 = 0;
  int rc4 = pulseIn(rc_channel4, HIGH, 25000);

  if (rc4 > 1520) { //RIGHT
    pwm1 = map(rc4, 1520, 1905, 0, 255); //map our speed to 0-255 range
    //digitalWrite(motor1_in1, LOW);
    // digitalWrite(motor1_in2, HIGH);
    digitalWrite(motor2_in3, LOW);
    digitalWrite(motor2_in4, HIGH);

    analogWrite(motor1_enable1, pwm1);
    analogWrite(motor2_enable1, pwm1);

    Serial.print(" RIGHT   : ");
    Serial.print(pwm1);
    Serial.print("\t");
    Serial.println(rc4);

  }
  else if (rc4 < 1480) { //LEFT
    pwm1 = map(rc4, 1480, 1090, 0, 255); //map our speed to 0-255 range
    digitalWrite(motor1_in1, HIGH);
    digitalWrite(motor1_in2, LOW);
    //digitalWrite(motor2_in3, HIGH);
    //digitalWrite(motor2_in4, LOW);

    analogWrite(motor1_enable1, pwm1);
    analogWrite(motor2_enable1, pwm1);

    Serial.print(" LEFT    : ");
    Serial.print(pwm1);
    Serial.print("\t");
    Serial.println(rc4);
  }
  else {
    digitalWrite(motor1_in1, LOW);
    digitalWrite(motor1_in2, LOW);
    digitalWrite(motor2_in3, LOW);
    digitalWrite(motor2_in4, LOW);

    analogWrite(motor1_enable1, 0);
    analogWrite(motor2_enable1, 0);

    Serial.print(" CENTER  : ");
    Serial.print(pwm1);
    Serial.print("\t");
    Serial.println(rc4);
  }

}
  • See the many answers which explain how horrible the L293 is. Fundamentally this doesn't seem to be a job for an Arduino - what you probably want is a pair of small hobby ESCs. – Chris Stratton Feb 3 '18 at 7:04
0

From your code I would say, the problem lies within your loop function. The functions motorMoveForward() and motorMoveSide() are both over very quickly. But to actually move your motors need some time, before you change directions. Currently the motors rapidly change between driving forward and sidewards. Since in your motorMoveSide() function you only change direction for one of the motors, this one will rapidly being told to move forward and backwards. That results in the motor to move slowly or to not move at all. You simply cannot move forward and sidewards at the same time.

If you just want to controll the movement of your robot via RC, it may be better to use one channel just for speed value (for example 0 --> stop, 255 --> full power) and one for the direction (for example 0 --> full power left, 127 --> just forward, 255 --> full power right). A normal RC car works the same way. Or you can take one channel for one motor (and mapping speed and direction of the specific motor like you did with both motors). Which way to go depends also on your remote (e.g. what is best for controlling it with the remote).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.