help regarding obstacle avoiding robot

hi could someone please help
i am making a obstacle avoiding robot using an adafruit motor shield 2 continuous sevos an uno and a hc srf04

i'm not sure how to connect the sevos to the motor shield could any one help

also a code would really help

godivaPrima:
Adafruit has a tutorial on the board, have you seen it?

There are a number of ways of programming the ultrasonic sensor, such as NewPing.

yes but im new to programming and the newping only has code for the sensor not the servos and all

godivaPrima:
Well you need to give it a bit of thought.

Forget the coding- can you describe what you want the robot to do? For example, read the sensor as the robot moves forward, and if the distance to an obstacle is more than some value you choose, carry on; else if obstacle is too close, do <what?> . Stop and sit for ever hoping the obstacle goes away? Veer to one side and carry on reading?

I'd say use the adafruit servo tutorial to get the thing moving, perhaps sequentially stopping, reversing, veering to one side etc. Have another sketch to read the sensor, and make sure the value changes as you move the obstacle.

Then think about how to merge the two. Probably have an "if" that checks if the obstacle is too close, then stop, spin round and run away, or whatever you decided. You will by then already have figured how to read the sensor, and how to stop and spin (or whatever) separately, now just get it to do the actions as a result of an if.

Take it step by step, gradually building up the robot's capability.

That's my suggestion anyway, fwiw.

well thanks a lot

so i got this code from roy but he uses dc motors so i tries to change it to servos but it says that the servos is not defined please have a look at the code

//#include <AFMotor.h> //import your motor shield library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13

void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() {

long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");q
Serial.println (" Turning !");
servoLeft.write(0);
servoRight.write(180);

}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}

} //

i tried it but now it says "servo does not type a name"
i'm sorry to be such a pain but i'm new to programming

well that worked but now it has another problem

at this line 'void turnRight() {' it says a function definition is not allowed here before { token ..... what does it mean ??

also could you kindly check it the code would work

godivaPrima:
You'll need to post it before anyone can check it :wink:

sure

//#include <AFMotor.h> //import your motor shield library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
#include <Servo.h>
Servo servoLeft; // and similar for other ones
Servo servoRight; //
int pos = 0; // variable to store the servo position

void setup() {
Serial.begin(9600); // begin serial communitication
Serial.println("Motor test!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
servoLeft.attach(10); // Set left servo to digital pin 10
servoRight.attach(9); // Set right servo to digital pin 9
}

void loop() {

long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2); // delays are required for a succesful sensor operation.
digitalWrite(trigPin, HIGH);

delayMicroseconds(10); //this delay is required as well!
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;// convert the distance to centimeters.
if (distance < 25)/*if there's an obstacle 25 centimers, ahead, do the following: */ {
Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ")
;Serial.println (" Turning !");
servoLeft.write(0);
servoRight.write(180);

}
else {
Serial.println ("No obstacle detected. going forward");
delay (15);
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
//

Serial.println (" The obstacle is declared a threat due to close distance. ")
;Serial.println (" Turning !");

Some thought before banging the carriage return would be good.

else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
  void turnRight() {
  servoLeft.write(180);
  servoRight.write(180);
  }

Care to explain this mess? Where does the else block end? Where does the turnRight() function start? Where does it end? Where does loop() end?

how should i do this

else {
Serial.println ("No obstacle detected. going forward");
delay (15); <<<<<<<<<<<<<<<<< you're not closing the "else" with a } here
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
}
//
is this okay ??

is this okay ??

The compiler already (or could have already) told you no.

Delete the stupid turnRight() function until you understand that you can NOT define a function inside another function (loop()).

Use Tools + Auto Format, so your code doesn't look like it was typed by a drunken monkey. You will be told, when you try to use it, if the number of { and the number of } (do not) match. If not, YOU will have to do the formatting properly until you get the number to match. Then, you can more easily see the structure of the program, and any mis-placed blocks of code.

total_newbie:
how should i do this

else {
Serial.println ("No obstacle detected. going forward");
delay (15); <<<<<<<<<<<<<<<<< you're not closing the "else" with a } here
void turnRight() {
servoLeft.write(180);
servoRight.write(180);
}
}
//
is this okay ??

will this work

// else {
Serial.println ("No obstacle detected. going forward");
delay (15);
void turnRight()
;servoLeft.write(180);
servoRight.write(180);
} //

will this work

Ask the f**king compiler. It will tell you no. I can not understand why you can't understand that you MUST put the turnRight() function declaration/implementation AFTER the end of loop().