I can control the robot to go forward and backwards with both motors running fine, and I can get the ultrasonic sensor to display accurate readings; however, when I incorporate the ultrasonic sensor with the motors, only one motor works.
Here is my source code broken down into the most basic form (with troubleshooting in comments):
Code: Select all
#include <AFMotor.h>
#define trigPin 3
#define echoPin 2
AF_DCMotor left_motor(2, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor right_motor(1, MOTOR12_64KHZ); // motor #2\
int maxDistance = 30;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
left_motor.setSpeed(255);
right_motor.setSpeed(255);
}
void forward(int delayTime) {
left_motor.run(FORWARD);
right_motor.run(FORWARD);
delay(delayTime);
}
void right(int delayTime) {
left_motor.run(FORWARD);
right_motor.run(BACKWARD);
delay(delayTime);
}
int checkDistance(){
long duration, distance;
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/59.1;
return distance;
}
void loop() {
forward(500); //both motors work fine (for first iteration through loop)
delay(200); //using extra delays for troubleshooting purposes..doesn't help
backward(500); //both motors work
delay(200);
if (checkDistance() >= maxDistance){
Serial.println(" cm. No immediate obstacles detected. Moving forward.");
delay(200);
forward(2000); //problem starts here... only left motor works
delay(200);
stop(1000);
delay(200);
}
else {
delay(200);
Serial.println(" cm. Obstacle detected. Changing direction.");
delay(200);
right(1500); //problem here as well.. as only one motor turns (both are supposed to turn)
delay(200);
stop(1000);
delay(200);
}
}
Steps I've taken to troubleshoot issue:
1. Run on both external and USB power. External I have 4xAA power source for motors & 9V for Arduino.
2. Add two 100nF capacitors to motors
3. Isolate the issue.. Both motors work properly before checkDistance()--ultrasonic sensor--is used
4. Check serial output to see if ultrasonic sensor is working properly -- verified. Sample output:
Questions:41 cm. No immediate obstacles detected. Moving forward.
21 cm. Obstacle detected. Changing direction.
3 cm. Obstacle detected. Changing direction.
19 cm. Obstacle detected. Changing direction.
41 cm. No immediate obstacles detected. Moving forward.
1. For the motors I used a thin single strand wire, could this cause a problem?
2. After soldering connections for the ultrasonic sensor I used hot glue to support the joints, is this a problem?
Also, here's a picture of my motor shield:
I kind of hit a wall here, and not sure what else to do. I appreciate any thoughts you guys have. Thanks!