I'm new to electronics and arduino and I have successfully built the 2wd robot with the pan/tilt sensor. The problem I have is that there is not enough power to drive the wheels. I have a 4 cell battery pack and even tried boosting it with a extra 9v battery but it doesn't make any difference. When I switch it on the sensor moves and the wheels move slightly but after that nothing but a noise from the motors.
Any help would be appreciated, here is the code I've uploaded incase there's something I can do there to help.........
#include <AccelStepper.h>
// Magic03
// This code is designed for the Dagu Magician Chassis
// Using a Pan & Tilt asseply with an HC-SR04 Ultrasonic Sensor
//
// The motors are controlled using the Adafruit Motor shield library
// which must be downloaded and installed first
// copyright Adafruit Industries LLC, 2009
// this code is public domain, enjoy!
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor mLeft(3);
AF_DCMotor mRight(4);
int panPin = 9; // this is Servo 2 on the motor shield
int tiltPin = 10; // This is Servo 1 on the motor shield
Servo panServo, tiltServo;
int pingPin = A0; // Analog pin 0 used for the Ping Pin (Trig)
int inPin = A1; // Analog pin 1 used for the Echo Pin (Echo)
unsigned long duration, inches;
int indec, cmdec;
int inchconv = 147; // ratio between puls width and inches
int cmconv = 59; // ratio between pulse width and cm
String s1, s2, s3;
int panCentre = 110;
void setup()
{
Serial.begin(115200);
pinMode(pingPin, OUTPUT);
pinMode(inPin, INPUT);
panServo.attach(panPin, 1000, 2000);
tiltServo.attach(tiltPin, 1000, 2000);
pointCentre();
tiltServo.write(90);
}
void loop()
{
int cm, lcm, rcm;
forward(200);
delay(100);
cm = getDistance();
if(cm < 30)
{
halt();
pointLeft();
lcm = getDistance();
pointRight();
rcm = getDistance();
pointCentre();
reverse(255);
delay(400);
halt();
if (rcm < lcm)
spinRight(255);
else
spinLeft(255);
delay(200);
halt();
}
}
void pointLeft()
{
panServo.write(panCentre - 60);
delay(150); // wait for servo to get there
}
void pointRight()
{
panServo.write(panCentre + 60);
delay(300); // wait for servo to get there
}
void pointCentre()
{
panServo.write(panCentre);
delay(150); // wait for servo to get there
}
int getDistance()
{
int rval;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(inPin, HIGH, 38000L); // Set timeout to 38mS
rval = microsecondsToCentimeters(duration);
Serial.println(rval);
return rval;
}
void forward (int spd)
{
mLeft.run(FORWARD);
mRight.run(FORWARD);
mLeft.setSpeed(spd);
mRight.setSpeed(spd);
}
void reverse(int spd)
{
mLeft.run(BACKWARD);
mRight.run(BACKWARD);
mLeft.setSpeed(spd);
mRight.setSpeed(spd);
}
void spinLeft(int spd)
{
mLeft.run(FORWARD);
mRight.run(BACKWARD);
mLeft.setSpeed(spd);
mRight.setSpeed(spd);
}
void spinRight(int spd)
{
mLeft.run(BACKWARD);
mRight.run(FORWARD);
mLeft.setSpeed(spd);
mRight.setSpeed(spd);
}
void halt()
{
mLeft.run(RELEASE);
mRight.run(RELEASE);
delay(10);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / cmconv;
}