problem with ping while interfacing with servo motor pannig left to right. . .

My PING sensor works only in one direction when the ping sensor is rotated to complete right direction . . . . having trouble in code :confused: plz guide me for the code thanQ smiley

here is the code

// here is the code 
// problem facing with the HEAD servo and the PING sensor 
// this is paning from left to right but the problem is that PING works only in 1 direction.

#define trigPin 3
#define echoPin 12
#define led 4
#define relay 8

Servo sl;
Servo head;
Servo sr;
Servo el;  
Servo er;
int posh=0;
int posl=0;
int posr=0;
void setup()
{
  sl.attach(9);
  sr.attach(10);
  el.attach(11);
  er.attach(5);
  head.attach(6);
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
   pinMode(relay, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop()
{
 
  for(posh = 0;posh <= 180; posh +=1)
  {
 delay(15);
    head.write(posh);
  }
  
  for(posh = 180;posh >=0; posh -=1)
  {
 
    head.write(posh);
delay(15);  
}
  
  
 
  //shoulder up
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

if (distance < 5)
{  
// This is where the LED On/Off happens
     digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
     Serial.println("WELCOME");
   head.write(90);
 
 
  for(posl = 0, posr = 180; posl <= 90 ; posl +=1)
  {
 
    sl.write(posl);
     sr.write(posr);
     posr -=1;
    delay(15);
  }
  
  
    // elbow close
  
  for(posr = 0,posl=180; posr <= 45; posr +=1)
  {
    er.write(posr);
    el.write(posl);
    posl -=1;
    delay(15);
  }
 
  delay(3000);
  //elbow open
 
 
  for(posr = 45,posl=135; posr >= 0; posr -=1)
  {
    er.write(posr);
    el.write(posl);
    posl +=1;
    delay(15);
  }
 
  //shoulder down
 
 
  for(posr = 90,posl= 90; posr <= 180; posr +=1)
  {
    sr.write(posr);
    sl.write(posl);
    posl -=1;
    delay(15);
  }
 
   digitalWrite(relay,HIGH);
   delay(1000);
    digitalWrite(relay,LOW);

  }
 
  else
  {
    digitalWrite(led,LOW);
  }
 
  delay(100);
 
  }

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

the problem is that PING works only in 1 direction.

Can you explain what you mean by this ?

Your code first moves the head servo through 180 degrees then moves it back to the start position before the ping sensor is used. Where you expecting that the ping sensor would work whilst the head was moving or perhaps that the head would move fully one way, check the distance then move fully the other way and check the distance again ?

If the latter then you need to insert distance checking after each of the for loops that move the head. Even better, write a function to do the checking and call that to avoid duplicating code. If the former then you need to do the distance checking periodically during the head move for loops.

@speak2sainow, please do not cross-post.