Problem driving motors with the adafruit motor shield

Hi, I am having a problem using the Adafruit motor shield. I am working on an autonomous robot that turns 180 degrees when it is 14 inches from the wall. The sonar sensor that I am using is the Maxbotix Ez0. When I upload my code, nothing happens. Motors don't turn, but the sonar does register accurate inch values.

here is my code:

#include <AFMotor.h>

AF_DCMotor motor1 (1);
AF_DCMotor motor2(2);

int SonarVal;

void setup() {
Serial.begin(9600);
motor1.setSpeed(255);
motor1.run(RELEASE);
motor2.setSpeed(255);
motor2.run(RELEASE);
}

void gather() {
SonarVal = analogRead(0);
}

void loop() {
gather();

if(SonarVal = 14) {
motor1.run(FORWARD);
motor1.setSpeed(200);
motor2.run(RELEASE);
motor2.setSpeed(200);
}

else {
motor1.run(FORWARD);
motor1.setSpeed(200);

motor2.run(FORWARD);
motor2.setSpeed(200);
}
}

any help is appreciated!!

Thanks, ke0aer

    if(SonarVal = 14) {

You probably mean:

    if(SonarVal == 14) {

correct.

do you think that the batteries may be the problem?

ke0aer:
do you think that the batteries may be the problem?

I can't tell that from the code and you have not mentioned anything about your batteries, motors, or wiring.

ke0aer:
correct.

do you think that the batteries may be the problem?

See this thread,
http://forum.arduino.cc/index.php?topic=255068.0

So basically power the arduino seperately from the motor shield
and don't use 9v batteries. Use larger nimh packs

Hi, can you post a circuit diagram of your project please, CAD or picture in jpg, png or pdf format.
Also a picture of your project please.
Have you tried just a simple test program that runs your motors forward, to see if your wiring and control procedures work/

Tom...... :slight_smile: