I'm quite new to Arduino and am making my first proper project, an obstacle avoiding robot.
I'm using an Arduino Uno and a motor shield compatible with the Adafruit motor driver library. I'm trying to run the example code from here: https://learn.adafruit.com/adafruit-motor-shield/using-dc-motors, but with both motors running at once. This should run the motors forward for 1 sec, backward for 1 sec and then pause for 1 sec.
I'm getting inconsistent behaviour when I run the code. When I only connect 1 motor, it works fine. When the Arduino is connected to my laptop via USB, it works fine. However, when I use the 6V external power supply and both motors, they will only move in 1 direction and remain still during the second movement period. When I set them to move forward-backward-stationary, they only move forwards. When I set them to backward-forward-stationary, they only move backwards.
Any ideas what might be causing this / how I could fix it? Like I said, I'm pretty new to this so might be making a simple mistake.
I have 2 DC motors, one attached to port 1 and the other to port 4 on the motor shield. The power supply is 4 AA batteries (which I think is 6V?), which is also connected to the motor shield.
And here's my code:
#include <AFMotor.h>
AF_DCMotor leftMotor(1, MOTOR12_1KHZ);
AF_DCMotor rightMotor(4, MOTOR12_1KHZ);
void setup() {
leftMotor.setSpeed(255);
rightMotor.setSpeed(255);
}
void loop() {
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
delay(1000);
leftMotor.run(BACKWARD);
rightMotor.run(BACKWARD);
delay(1000);
leftMotor.run(RELEASE);
rightMotor.run(RELEASE);
delay(1000);
}
Any help is much appreciated!