problem with the coding of obstacle avoiding robot using ping

The robot is working good but not up to my mark.It uses ping mounted on servo motor to scan the environment.The code is given below.The glowing part is where i struck.It should scan to its left and then to its right and take turn according to the greatest distance but what it doing is it is just scanning left and moving to its right.where i’m gone wrong.please help thanks in advance.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 400
Servo scanservo;
int pos = 90;
const int ml1=7;
const int ml2=6;
const int mr1=5;
const int mr2=4;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup()
{
scanservo.attach(9);
pinMode(ml1,OUTPUT);
pinMode(ml2,OUTPUT);
pinMode(mr1,OUTPUT);
pinMode(mr2,OUTPUT);
}
void loop()
{
delay(50);
unsigned int uS = sonar.ping();
int dis= uS / US_ROUNDTRIP_CM;
if((dis>25)&&(dis<=400)||(dis<3))
{
forward();
delay(350);
}
else
{
Stop();
scanleft();
int leftdis= uS / US_ROUNDTRIP_CM;
delay(100);
scanright();
int rightdis= uS / US_ROUNDTRIP_CM;
delay(100);
if(leftdis>rightdis)
{
left();
delay(100);
scanrightback();
}
else
{
right();
delay(100);
scanrightback();
}
}
}
void forward()
{
digitalWrite(ml1,HIGH);
digitalWrite(ml2,LOW);
digitalWrite(mr1,LOW);
digitalWrite(mr2,HIGH);
}
void right()
{
digitalWrite(ml1,LOW);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,LOW);
digitalWrite(mr2,HIGH);
}
void left()
{
digitalWrite(ml1,HIGH);
digitalWrite(ml2,LOW);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,LOW);
}
void reverse()
{
digitalWrite(ml1,LOW);
digitalWrite(ml2,HIGH);
digitalWrite(mr1,HIGH);
digitalWrite(mr2,LOW);
}
void Stop()
{
digitalWrite(ml1,LOW);
digitalWrite(ml2,LOW);
digitalWrite(mr1,LOW);
digitalWrite(mr2,LOW);
}
void scanleft()
{
for(pos = 90; pos <= 180; pos +=1)
{
scanservo.write(180);
delay(10);
}
}
void scanright()
{
for(pos =180; pos <= 0; pos -=1)
{
scanservo.write(0);
delay(10);
}
}

void scanrightback()
{
for(pos = 0; pos <= 90; pos +=1)
{
scanservo.write(90);
delay(10);
}
}

Looks like you send the servo to the same position all the time, in the loops. Which might not be your problem, but can’t be right…

See <<<<<<<<<< below

void scanleft()
{
  for(pos = 90; pos <= 180; pos +=1)
  {
  scanservo.write(180);   //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   should be scanservo.write(pos); <<<<<<<<<
  delay(10);
  }
}

Or just take the loop stuff out

Thanks for your reply it is now scanning to the right but not moving left anyway.

Sounds like you made some changes: post the new code.....

edit.... And I would suggest using serial.print in each section of the code so you can get a message to tell you where the logic flow is taking you. Also perhaps print out the distances so you can see what it's "thinking".

deekshithreddy22: Thanks for your reply it is now scanning to the right but not moving left anyway.

Seriously? You were just told why scanleft() didn't work. Did it not occur to you to have a look at your scanright() function? You know, programming is not really very difficult, but it does take some thought.

I just changed Servo.write(180) to Servo.write(pos) similarly in all other places of the program and the reamining code is same

deekshithreddy22: I just changed Servo.write(180) to Servo.write(pos) similarly in all other places of the program and the reamining code is same

Put some Serial.print()'s in so you can see where the flow is going and what the distance is at various times.

By the way, what's the point of the for loop to go from 90 to 180 a degree at a time?- why not just send it straight to 180. That's why I suggested perhaps just removing the for loop stuff altogether.