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);
}
}
pulseIn()
is an exceptionally bad, blocking way to read in RC signals, and performs very poorly, while also slowing your loop speed down a lot. Reading in RC signals properly is a very advanced task. Here's one example I made to show how to do it properly on a single RC channel: github.com/ElectricRCAircraftGuy/eRCaGuy_TimerCounter/blob/…. Doing it for many channels is far more complicated still, as jitter and interrupt time must be minimized.