Arduino motor shield help

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
360is2piRad
 
Posts: 2
Joined: Thu Feb 04, 2010 8:34 pm

Arduino motor shield help

Post by 360is2piRad »

Hey, im fairly new to rebotics/electronics and am having troubles building a obsticle avoiding robot.
I have the adafruit motor shield and can't get the servos motors and sensors to work together.
When i run the motors everything gets voltage and runs. I have two power sources, a 9v batery for the arduino and a 9.6V Ni-MH batery for the motor shield.
Im using pin 10 for the servo, and pin 9 for the ping sensor. also i read pin 13 is not used so i cut it off the shield and flipped it around for use by sensors.
I've bridged the motor outputs to provide the motors more current aswell as soldered on annother set of L239D motor drivers piggybacked onto the first set.
The led light on the shield lights up when i give the arduino, power but no power to the shield, no jumper aswell. Is something burntout?
Also when i use code that only uses the servo/sensor everything is fine. Add motors to the code and i can only get a small 100 millisecond jolt out of them, even though the code says to run longer.
Is there a way to use a pulldown resistor with the servo and ping when the motors are inuse?

Any help would be greatly appreciated!

P.S.
This is my code.


#include <ServoTimer1.h>
#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2
AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #3
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #4

const int numOfReadings = 180;
int distance[numOfReadings];
int arrayIndex = 0;
double totalL = 0;
double totalR = 0;
double averageL = 0;
double averageR = 0;
const int pingPin = 9;

ServoTimer1 servo;

void setup()
{
servo.attach(10);
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);

for(int thisReading = 0; thisReading < numOfReadings; thisReading++)
{
distance[thisReading] = 0;
}
}

void loop()
{

for(int pos = 0; pos < 180; pos = pos + 30)
{
servo.write(pos);
delay(350);
distance[pos] = ping();
delay(350);
}
servo.write(90);

for(int i = 0; i<3 ; i++)
{
int pos = 0;
totalL = totalL + distance[pos];
pos = pos + 30;
}

for(int i = 0; i<3 ; i++)
{
int pos = 120;
totalR = totalR + distance[pos];
pos = pos + 30;
}

averageL = totalL / 3;
averageR = totalR / 3;


if(distance[90] < 15)
{
go();
delay(distance[90]/2);
}
else if(distance[90] <= 15 && averageL > averageR)
{
turnL();
}
else if(distance[90] < 15 && averageL < averageR)
{
turnR();
}

}

void stp()
{
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}


void go()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

void turnR()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(2000);
}

void turnL()
{
motor4.run(FORWARD);
motor3.run(FORWARD);
motor2.run(BACKWARD);
motor1.run(BACKWARD);
delay(2000);
}


long ping()
{

long duration, inches, cm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);


pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);


cm = duration / 29 / 2;
Serial.print(cm);
Serial.print("cm");
Serial.println();
return cm;
}

User avatar
adafruit_support_bill
 
Posts: 88574
Joined: Sat Feb 07, 2009 10:11 am

Re: Arduino motor shield help

Post by adafruit_support_bill »

If motors & servos work separately, the problem is likely in the code. A good way to debug things like this is to put some Serial.println() statements in so that you can see what code path is actually executing. I'd start with one in eachj of your move subroutines and the value of distance[90]/2 as well.

If you are going through the following bit of code, your motors will run for 7 milliseconds or less before you stop and do another sensor scan.

Code: Select all

	if(distance[90] < 15)
	{
		go();
		delay(distance[90]/2);
	}

GeorgeFrick
 
Posts: 19
Joined: Thu Jan 14, 2010 2:56 pm

Re: Arduino motor shield help

Post by GeorgeFrick »

In addition to what Arduwino said, I noticed:

1.

Code: Select all

for(int thisReading = 0; thisReading < numOfReadings; thisReading++)
{ 
distance[thisReading] = 0; 
}
}
are you meaning to store 180 integers when only using 6 of them?

2. as Arduwino said, you are running your motors for no more than 7 milliseconds, but you are scanning for at least 4.2 seconds.

3. I would really test your ping function (move it to an app that just pings and make sure you are getting the type of numbers you are looking for). If you are getting distances <= 2; you'll just see a stutter in the motors. Also there seems to be a common problem of having to read from a sensor twice due to Arduino's timer. (Read, delay(5), read). It has fixed my sensor problems.

4. Program design wise, you are delaying for your tuns in your turn functions, but not delaying for your go forward in the forward function; instead doing it from the calling function. You might consider making this consistent.

Don't see much else...

360is2piRad
 
Posts: 2
Joined: Thu Feb 04, 2010 8:34 pm

Re: Arduino motor shield help

Post by 360is2piRad »

Hey this is awesome thanks! Yeah those were some definite mistakes in the code. And i have fixed them but still have some problems with simple code, like running the motors forward and thats it. When i do this my servo and ping sensor friek out, servo friezes and ping sensor runs constantly, atleast the led is lit constantly. Is this noise from the motors? I dont have capacitors across the motor terminals, will this solve my problem? Maybe my batery is too big and causing brownouts? Seems to work better at low voltage from the arduino. By this i mean only the arduino recieving power via its DC jack and no power jumper.

I greatly apreciate the help guys! And any more advice would be greatly recieved. Thanks.


Ps. My simple code is

#include <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_1KHZ); // create motor #1
AF_DCMotor motor2(2, MOTOR12_1KHZ); // create motor #2
AF_DCMotor motor3(3, MOTOR12_1KHZ); // create motor #3
AF_DCMotor motor4(4, MOTOR12_1KHZ); // create motor #4

void setup()
{
motor1.setSpeed(200);
motor2.setSpeed(200);
motor3.setSpeed(200);
motor4.setSpeed(200);
}

void loop()
{
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}

GeorgeFrick
 
Posts: 19
Joined: Thu Jan 14, 2010 2:56 pm

Re: Arduino motor shield help

Post by GeorgeFrick »

If you are running that simple code and the sensors are freaking out; I would suggest trying the caps. Beyond that there may be a problem with the motor shield? I did notice you said

> DC Jack and no power jumper

There would be no power to the motors this way? Are you otherwise only powering from your pack? The arduino should always receive it's own 9v dc power.

In my bot I'm runing a 9v to the arduino and 4 AA pack to the motor shield. No jumper.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”