calling all roboteers

good morning fellow roboteers,

i picked up this code from the internet for an obstacle avoiding robot with a small servo for turning the sensor but the cod3 uses 2 dc motors instead of 2 continuous rotation servos also it uses a ping sensor rather than a HCSRF-04

so i was hoping someone would help me change the code to add servos instead of motors and ultrasonic sensor instead of ping sensor

thanks

the code

//#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>

#define TRIG_PIN A4
#define ECHO_PIN A5
#define MAX_DISTANCE_POSSIBLE 1000
#define MAX_SPEED 150 //
#define MOTORS_CALIBRATION_OFFSET 3
#define COLL_DIST 20
#define TURN_DIST COLL_DIST+10
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE_POSSIBLE);

AF_DCMotor leftMotor(4, MOTOR12_8KHZ);
AF_DCMotor rightMotor(3, MOTOR12_8KHZ);
Servo neckControllerServoMotor;

int pos = 0;
int maxDist = 0;
int maxAngle = 0;
int maxRight = 0;
int maxLeft = 0;
int maxFront = 0;
int course = 0;
int curDist = 0;
String motorSet = "";
int speedSet = 0;

void setup() {
neckControllerServoMotor.attach(10);
neckControllerServoMotor.write(90);
delay(2000);
checkPath();
motorSet = "FORWARD";
neckControllerServoMotor.write(90);
moveForward();
}

void loop() {
checkForward();
checkPath();
}

void checkPath() {
int curLeft = 0;
int curFront = 0;
int curRight = 0;
int curDist = 0;
neckControllerServoMotor.write(144);
delay(120);
for(pos = 144; pos >= 36; pos-=18)
{
neckControllerServoMotor.write(pos);
delay(90);
checkForward();
curDist = readPing();
if (curDist < COLL_DIST) {
checkCourse();
break;
}
if (curDist < TURN_DIST) {
changePath();
}
if (curDist > curDist) {maxAngle = pos;}
if (pos > 90 && curDist > curLeft) { curLeft = curDist;}
if (pos == 90 && curDist > curFront) {curFront = curDist;}
if (pos < 90 && curDist > curRight) {curRight = curDist;}
}
maxLeft = curLeft;
maxRight = curRight;
maxFront = curFront;
}

void setCourse() {
if (maxAngle < 90) {turnRight();}
if (maxAngle > 90) {turnLeft();}
maxLeft = 0;
maxRight = 0;
maxFront = 0;
}

void checkCourse() {
moveBackward();
delay(500);
moveStop();
setCourse();
}

void changePath() {
if (pos < 90) {lookLeft();}
if (pos > 90) {lookRight();}
}

int readPing() {
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
return cm;
}

void checkForward() { if (motorSet=="FORWARD") {leftMotor.run(FORWARD); rightMotor.run(FORWARD); } }

void checkBackward() { if (motorSet=="BACKWARD") {leftMotor.run(BACKWARD); rightMotor.run(BACKWARD); } }

void moveStop() {leftMotor.run(RELEASE); rightMotor.run(RELEASE);}

void moveForward() {
motorSet = "FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET);
rightMotor.setSpeed(speedSet);
delay(5);
}
}

void moveBackward() {
motorSet = "BACKWARD";
leftMotor.run(BACKWARD);
rightMotor.run(BACKWARD);
for (speedSet = 0; speedSet < MAX_SPEED; speedSet +=2)
{
leftMotor.setSpeed(speedSet+MOTORS_CALIBRATION_OFFSET);
rightMotor.setSpeed(speedSet);
delay(5);
}
}

void turnRight() {
motorSet = "RIGHT";
leftMotor.run(FORWARD);
rightMotor.run(BACKWARD);
delay(400);
motorSet = "FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
}

void turnLeft() {
motorSet = "LEFT";
leftMotor.run(BACKWARD);
rightMotor.run(FORWARD);
delay(400);
motorSet = "FORWARD";
leftMotor.run(FORWARD);
rightMotor.run(FORWARD);
}

void lookRight() {rightMotor.run(BACKWARD); delay(400); rightMotor.run(FORWARD);}
void lookLeft() {leftMotor.run(BACKWARD); delay(400); leftMotor.run(FORWARD);}//

"i picked up this code from the internet "

And let's assume it works? No thanks, it's a delay-ridden mess.

Your time would be better spent learning the "do many things at once" lesson -after- reading the BEFORE YOU POST thread.

and ultrasonic sensor instead of ping sensor

The Ping is an ultrasonic sensor.

Please remember to use code tags when posting code.

 neckControllerServoMotor.write(144);

This is an example of an instruction to change the speed of the motor using the write function of the servo library. 0 is full back, 90 stop and 180 full ahead, nominally. Replace the Servo write commands with whatever code is appropriate for your motor drivers.

Here's an example of collision-avoiding robot code. It's been used hundreds of times. You'll have to change the motor part but at least it may be understandable...

total_newbie:
the code doesn't use an arduino uno.. so its tough to change the motor part ....... any other suggestions

That board uses the same chip as an Uno. It is an Uno with improved hardware.
http://www.yourduino.com/sunshop/index.php?l=product_detail&p=429

Same code runs on the Uno but wiring the Uno may take more circuitry.

the code doesn't use an arduino uno..

Yes, it is a totally UNO compatible derivative design, the differences are the easy-to-connect pins and better power supply.