Ping))) on servo

Hello,

I am having an odd problem when mounting my ping sensor on a servo doing a sweep.

First, (in previous code where I had the sweep going alot quicker) The servo will slow down some times there is an object in front of it and the servo sweeps alot more slowly than it should. Then after the "hiccups" it starts going at its normal speed.

I have tried changing code and for the most part seem to have gotten around that issue. Now it seems my ping sensor cannot get a good reading! I have tried and tried to change the speed give the sensor some pauses to get a good reading but the distances printed in serial monitor are all over the place and do not seem to respond to any object in front of it.
I do not know if this is a programming issue or if it is a hardware issue.

I am using Arduino 2560 mega, but I will also want it to work on my UNO. My guess is the board is getting overwhelmed? Is the servo causing interference?

I want to mount this on my obstacle avoidance bot so there will be alot of moving around. Is ping))) appropriate to use for this purpose?

ANY suggestions, tips, or hints would be great, whether its programming or better hardware to use.

Excuse typos... its late:)

#include <Servo.h>
#define trigPin 5
#define echoPin 6


Servo myservo; 

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

void setup() {
  Serial.begin(9600);

  pinMode (trigPin, OUTPUT); // this is for the ping sensor
  pinMode (echoPin, INPUT); // this is for the ping sensor

  myservo.attach(7);  // attaches the servo on pin 7 to the servo object
  myservo.write(0);
  delay(2000);
}

void loop() {

  int duration, distance;


  for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree

  myservo.write(pos);  
    if (pos % 30 == 0)
    {
      digitalWrite(trigPin, LOW); // give me  2 pings and a 1 sec pause
      delay(10);
      digitalWrite(trigPin, HIGH);
      delay(10);
      digitalWrite(trigPin, LOW);
      Serial.println (distance);
      delay(10);
      digitalWrite(trigPin, HIGH);
      delay (10); // Originally 10
      digitalWrite(trigPin, LOW);
       Serial.println (distance);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      delay(1000);
      Serial.println (distance);

      if ( pos <= 180 && pos >= 150)
      {
        Serial.println ( "Quadrant 6");

      }
      else if( pos <= 150 && pos >= 120)
      {
        Serial.println ( "Quadrant 5");

      }
      else if( pos <= 120 && pos >= 90)
      {
        Serial.println ( "Quadrant 4");

      }
      else if( pos <= 90 && pos >= 60)
      {
        Serial.println ( "Quadrant 3");

      }
      else if( pos <= 60 && pos >= 30)
      {
        Serial.println ( "Quadrant 2");

      }
      else if( pos <= 30 && pos >= 0)
      {
        Serial.println ( "Quadrant 1");

      }




    }
    delay(5);                      
  }

  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);
    if (pos % 30 == 0)   // give me  2 pings and a 1 sec pause
    {
      digitalWrite(trigPin, LOW);
      delay(10);
      digitalWrite(trigPin, HIGH);
      delay(10);
      digitalWrite(trigPin, LOW);
      Serial.println (distance);
      delay(10);
      digitalWrite(trigPin, HIGH);
      delay (10); // Originally 10
      digitalWrite(trigPin, LOW);
       Serial.println (distance);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      delay(1000);
      Serial.println (distance);

      // For later use in appropriate drive algorithms

      if ( pos <= 180 && pos >= 150)
      {
        Serial.println ( "Quadrant 6");

      }
      else if( pos <= 150 && pos >= 120)
      {
        Serial.println ( "Quadrant 5");

      }
      else if( pos <= 120 && pos >= 90)
      {
        Serial.println ( "Quadrant 4");

      }
      else if( pos <= 90 && pos >= 60)
      {
        Serial.println ( "Quadrant 3");

      }
      else if( pos <= 60 && pos >= 30)
      {
        Serial.println ( "Quadrant 2");

      }
      else if( pos <= 30 && pos >= 0)
      {
        Serial.println ( "Quadrant 1");

      }




    }
    delay(5);                      
  }
  
}

You could try commenting out the "myservo.write(pos);" lines so the servo doesnt move and see if the readings settle down.

I dont think that sending extra pulses or extending the length will actually work better. The Ultrasonic sensor docs suggest a 5 microsecond pulse wheras your code uses multiple 10 millisecond pulses.

You may also want to check out the NewPing library which is reputed to work much better than pulseIn().