What is wrong in my sketch for Obstacle Avoidance Robot.

My bot is not moving forward. Please help.

/*Code for Obstacle avoiding Robot by Kavi */

#include <AFMotor.h> //import motor shield library
#include <Servo.h> //import servo library

#define trigPin A0 // define trigger pin
#define echoPin A1 // define echo pin

AF_DCMotor motor1(1,MOTOR12_64KHZ); // Set up motor pins
AF_DCMotor motor2(2,MOTOR12_8KHZ); // Set up motor pins

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

void setup()
Serial.begin(9600); // begin serial communitication
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.println("Motor test starts!");
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)


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) {sweep(1); /*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 !");
motor1.run(FORWARD); // Turn as long as there's an obstacle ahead.
motor2.run (BACKWARD);


if (distance >= 25)


Serial.println ("No obstacle detected. going forward");
motor1.run(FORWARD); //if there's no obstacle ahead, Go Forward!


void sweep(int NUM_OF_CYCLES)

for (int j=0; j<NUM_OF_CYCLES; j++)

for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'

What do your Serial.print()s tell you is happening? Do you have a hardware or software problem?