Hi everyone,
I just completed my first obstacle avoidance robot build and I'm having a bit of an issue with one of my drive servos turning slower than the other causing my robot to track in a circle. I am wondering if there is something that I can do (aside from buying another servo and hoping that it matches the speed of one of the other two) to change the speed that the slower one rotates at so that my robot tracks straight.
I'm currently using two Parallax continuous rotation servos; any advice you can provide is both welcome and appreciated. Thanks in advance!
Here is the full code for my build, just in case.
#include <Servo.h>
#define trigPin 2 // Trigger pin on ultrasonic sensor
#define echoPin 4 // Echo pin on ultrasonic sensor
#define leftPin 8 // Right drive-servo
#define pingPin 10 // Ultrasonic sensor servo
#define rightPin 12 // Left drive-servo
long duration, distance, leftDistance, rightDistance;
Servo rightServo;
Servo pingServo;
Servo leftServo;
void setup()
{
Serial.begin(9600);
rightServo.attach(rightPin);
pingServo.attach(pingPin);
leftServo.attach(leftPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(rightPin, OUTPUT);
pinMode(pingPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pingServo.write(90);
}
void moveForward()
{
leftServo.write(180);
rightServo.write(0);
}
void moveBackward()
{
leftServo.write(0);
rightServo.write(180);
}
void turnLeft()
{
leftServo.write(0);
rightServo.write(0);
}
void turnRight()
{
leftServo.write(180);
leftServo.write(180);
}
void halt()
{
leftServo.write(90);
rightServo.write(90);
}
void ping()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2.0) / 29.1;
Serial.print(distance);
Serial.println(" cm.");
delay(500);
}
void scanArea()
{
pingServo.write(45);
delay(500);
ping();
leftDistance = distance;
delay(50);
pingServo.write(135);
delay(500);
ping();
rightDistance = distance;
delay(50);
pingServo.write(90);
delay(50);
}
void loop()
{
ping();
if (distance > 20.0)
{
moveForward();
}
else if(distance <= 20.0)
{
halt();
delay(50);
scanArea();
delay(50);
if (leftDistance < rightDistance)
{
moveBackward();
delay(1000);
turnRight();
delay(1500);
}
else if (leftDistance >= rightDistance)
{
moveBackward();
delay(1000);
turnLeft();
delay(1500);
}
}
}