dk electronics motor shield and AFMotor

Hi

Im using this motor shield to control 2 dc motors (for wheels)

I wired them up and tested them using this code which worked as expected.

#include <AFMotor.h>
AF_DCMotor RHS(1);
AF_DCMotor LHS(2);

int spd=0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

}

void go(){
RHS.setSpeed(spd);
LHS.setSpeed(spd);
RHS.run(FORWARD);
LHS.run(FORWARD);
}

void loop() {
// put your main code here, to run repeatedly:

while(spd<=255){

Serial.println(spd);
go();
delay(500);
RHS.run(RELEASE);
LHS.run(RELEASE);
delay(250);
spd++;
}
spd=0;
}

I then wrote the code for the vehicle and put in a go() routine that starts the motors.

i used the go() routime in the setup() to test motor rotation. this worked as expected.
Hower any subsequent calls to the go() routine do not start the motors. Theres a serialprintln in the routine which shows that its called.

so the motor.run commands seem to work first time they are called only. I cant for the life of me see why the test sketch worked and the final one doesn't

anyone?

#include "Ultrasonic.h";
#include "Servo.h";
#include "AFMotor.h";

//define motors and servo
AF_DCMotor RHS(1);
AF_DCMotor LHS(3);
Servo head;
Ultrasonic ultrasonic(2,8);

// Define variables
int spd=65;
int RUNNING=false;
int turntime=500;
int cycles=0;
//distance toobject limits
const int Dlimit=30;
const int Slimit=15;
int Dist=0;
int LDiag=0;
int LDist=0;
int RDiag=0;
int RDist=0;
int Avg[3];
int Direction=0;

void setup(){
Serial.begin(9600);
head.attach(9);
head.write(75);
pinMode(2,OUTPUT);
pinMode(A0,OUTPUT);
pinMode(8,INPUT);
RHS.setSpeed(spd);
LHS.setSpeed(spd);

go();
delay(5000);
allstop();

digitalWrite(A0,HIGH); delay(5000);digitalWrite(A0,LOW);
RHS.run(FORWARD);
LHS.run(FORWARD);
delay(5000);allstop();
}

void go(){
RHS.setSpeed(spd);
LHS.setSpeed(spd);
LHS.run(FORWARD);
RHS.run(FORWARD);
Serial.println("GoGoGo");Serial.print(spd);
}

void rev(int t){
LHS.run(BACKWARD);
RHS.run(BACKWARD);
delay(t);
allstop();
}

void allstop(){
//LHS.run(RELEASE);
//RHS.run(RELEASE);
RHS.setSpeed(0);
LHS.setSpeed(0);

}

void left(){
allstop();
RHS.run(FORWARD);
delay(turntime);
allstop();
}

void right(){
allstop();
LHS.run(FORWARD);
delay(turntime);
allstop;
}

int watch(){
int howfar;
int cnt=0;
digitalWrite(A0,HIGH);
while(cnt<3){
Avg[cnt]=(ultrasonic.Ranging(CM));
++cnt;
}
howfar=((Avg[0]+Avg[1]+Avg[2])/3);
laser(150);
delay(150);
digitalWrite(A0,LOW);
return (howfar);
}

void laser(int t){
digitalWrite(A0,HIGH);delay(t);digitalWrite(A0,LOW);
}

void scan(){
head.write(150);
RDist=watch();
if(RDist>Slimit){allstop();}
head.write(110);
RDiag=watch();
if(RDiag>Dlimit){allstop();}
head.write(75);
Dist=watch();
if(Dist>Dlimit){allstop();}
head.write(37);
LDiag=watch();
if(LDiag>Dlimit){allstop();}
head.write(0);
LDist=watch();
if(LDist>Slimit){allstop();}
head.write(75);
Serial.print(RDist);Serial.print("-");Serial.print(RDiag);
Serial.print("-");Serial.print(Dist);Serial.print("-");
Serial.print(LDiag);Serial.print("-");Serial.print(LDist);
Serial.println("");

}

void loop(){

while(RUNNING==true){// if we are moving then lookout
while(cycles<=10){//while moving scan ahead
Dist=watch();Serial.println(Dist);
++cycles;}
cycles=0;
scan();//every 25 cycles do a look around

}

//test the motors

go();
delay(1000);
allstop();
delay(1000);

}

How are you powering the motors? If you are using USB power, you may be resetting the arduino once the motors are under load. Add an initial serial.println in Setup to see if you are resetting.

Also, wrap your code within code /code markers to make your post easier to read:

 Your Code Here

-transfinite