Hi,
I'm building a robot car using an Arduino Uno R3, an L293d motor driver shield, and 4 DC motors. I'm using the Adafruit library AFMotor.h to control my motors. They all work perfectly fine moving forward. However, when I upload the backward command to the motors, only one motor (the motor in the M3 slot) fails to receive the code and move. It can receive the forward command and move forward, yet cannot move backward, while the other motors can do both.
Attached is my code to just get the robot to move backward and an image of my setup. Any help would be appreciated! Thank you!
#include <AFMotor.h>
AF_DCMotor leftBack(1, MOTOR12_64KHZ);
AF_DCMotor rightBack(2, MOTOR12_64KHZ);
AF_DCMotor rightFront(3, MOTOR12_64KHZ);
AF_DCMotor leftFront(4, MOTOR12_64KHZ);
byte motorSpeed = 100;
int motorOffset = 15; //Factor to account for one side being more powerful
void setup() {
rightBack.setSpeed(motorSpeed);
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(RELEASE); //Ensure all motors are stopped
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
}
void loop() {
moveBackward(7000);
}
void moveForward(int time) //Set all motors to run forward
{
rightBack.setSpeed(motorSpeed); //Set the motors to the motor speed
rightFront.setSpeed(motorSpeed);
leftFront.setSpeed(motorSpeed+motorOffset);
leftBack.setSpeed(motorSpeed+motorOffset);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftFront.run(FORWARD);
leftBack.run(FORWARD);
delay(time);
}
void moveBackward(int time) {
rightBack.setSpeed(100); //Set the motors to the motor speed
rightFront.setSpeed(100);
leftFront.setSpeed(100);
leftBack.setSpeed(100);
rightBack.run(BACKWARD);
rightFront.run(BACKWARD);
leftBack.run(BACKWARD);
leftFront.run(BACKWARD);
delay(time);
}
void stopMove() //Set all motors to stop
{
rightBack.run(RELEASE);
rightFront.run(RELEASE);
leftFront.run(RELEASE);
leftBack.run(RELEASE);
delay(1000);
}
void turnLeft(int time) //Set motors to turn left for the specified duration then stop
{
rightBack.setSpeed(150);
rightFront.setSpeed(150);
rightBack.run(FORWARD);
rightFront.run(FORWARD);
leftBack.run(RELEASE);
leftFront.run(RELEASE);
delay(time); //950 seconds
}
void turnRight(int time) //Set motors to turn left for the specified duration then stop
{
leftBack.setSpeed(170);
leftFront.setSpeed(170);
leftBack.run(FORWARD);
leftFront.run(FORWARD);
rightBack.run(RELEASE);
rightFront.run(RELEASE);
delay(time); //950
}
void stopProgram() {
while(1);
}