servo, ping sensor issues with arduino

Hi,

I have a 180 degree rotation servo. I mounted the ultrasonic sensor to this servo. Both are powered from 5v pin of arduino duemilanove.

I want to rotate the servo every 90 degrees and take a ping reading. I am giving a delay of 2000 milliseconds after invoking the servo rotation call.

apparently, the delay is observed. But, the ping reading is not working all the times. ping is doing a sensor reading only on one specific angle [I believe it is at 180 degrees].

If I do not rotate the servo and just take the ping readings for every 2 seconds. that works like a charm. I am really confused and wondering what is the wrong I am doing ?? :-/

int front, left, right;
  while(intialReading == 0) {
    front = ping(0); 
    Serial.println(front);
    left = ping(1);  
    Serial.println(left);
    right = ping(2); 
    Serial.println(right);    
  }
long ping(int i) {

  Serial.println("switch value - ");
  Serial.println(i);   
  
  i=i*90;
  pingServo.write(i);     
  Serial.println("servo rotation completed. ping started.");
  
  delay(2000);
    
  long duration, inches, cm;  
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  Serial.print("duration, ");
  Serial.println(duration); 
  
  cm = microsecondsToCM(duration); 
  Serial.println("ping completed.");  
  delay(100);
  return cm;
}

output:

switch value -
0
servo rotation completed. ping started.
duration, 0
ping completed.
0
switch value -
1
servo rotation completed. ping started.
duration, 0
ping completed.
0
switch value -
2
servo rotation completed. ping started.
duration, 1787
ping completed.
12

Any help would be greatly appreciated !! Thanks a lot !!

The output may be correct. You don't say anything about what is in front of the ping sensor at each position. If there is nothing near the sensor at the 180 degree position, and there is at the 0 and 90 degree position, the output could be correct.

What version of the IDE are you using? What pins are the servo and ping sensor connected to?

hmm .. actually there are objects at a distance of 1 to 3 feet on all 3 angles [0, 90, 180].

If I do not rotate the servo, the ping sensor is taking the readings every 2 seconds. [If I rotate it using the hand .. it was finely taking the readings as expected] :frowning:

Ping sensor is connected to port 7 and Servo is connected to port 9.

Please let me know if you need more information. Greatly appreciate your help.

Probably this might be a very common thing to do in robotics. to mount a sensor onto a servo and rotate it using the servo. Wondering if anybody have a sample code to achieve this and would like to share it? Thanks a lot !!

Since there is no load on the servo (nothing to make it drift), you might try detaching the servo before doing the ping sensor stuff, and then reattaching the servo.

Otherwise, I have no idea what the problem is.

Wow !! Thanks for the idea ... it worked !!! ;D

I am rotating the servo .. giving it 2 secs to complete the rotation. Then detach it. wait another 2 secs. take ping reading and attach the servo back ..

Working excellent !! I am wondering if for some reason .. the board was unable to supply required power to run both servo and sensor. I will probably run the sensor using an external source and see if that will work.

will update here !! hoping it may help others with similar problem !! Thanks !!

There is no need to wait 2 seconds after detaching the servo. As soon as the detach function returns, it's done.

I am wondering if for some reason .. the board was unable to supply required power to run both servo and sensor.

It's not that. The servo library uses a timer that interferes with the pulseIn function. Detaching the servo frees the timer, so pulseIn can use it.

I was trying to run a servo motor and an infrared sensor but everytime I hooked them both up the timing values I read from the infrared were screwed up. It was only when I hooked up a different power source to run the servo from the arduino when I got it work. I connected the grounds together.