ping controlled servos

does anyone know if its possible to use a ping ultrasonic sensor to tell a servo to stop when it the sensor detects an object. any help is apreciated

Sure you can do that. Think of sensors as just inputs into the processor and servos are just one form of a output of the processor. Anything you want to happen between a input and an output is strickly software you create in your sketch.

Lefty

so ive looked over the ping and servo examples but im slightly confused on what the section of code that tells the servo to stop when a certain distance is detected would look like

heres the sensor sketch

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

im slightly confused on what the section of code that tells the servo to stop when a certain distance is detected would look like

In that code, cm holds the distance. Exactly how to stop the servo based on the value in cm depends on exactly how you are making the servo go. A review of the code you posted wasn't very helpful, and you've told us nothing about the servo (which probably isn't even a servo).

heres what im using to make the servo "go"

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
      for(pos = 0; pos < 180; pos += 180)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5000);                       // waits 15ms for the servo to reach the position 
  } 

  }
  else {
      for(pos = 180; pos>=1; pos-=180)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(0);                       // waits 15ms for the servo to reach the position 
  } 
    // turn LED off:
    digitalWrite(ledPin, LOW); 
  }
}

when the button is pushed the servo and led is active. i want the servo to stop when the sensor detects an object exactly 1 inch in front of it. and i assure you its a continuous rotation servo

i assure you its a continuous rotation servo

Wrong. The definition of a servo involves a bi-directional electric motor and position feedback. Once the servo has been modified to enable continuous rotation, the position feedback is no longer there, so, by definition, it is no longer a servo.

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

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

I love it when the (useless) comments match the code. This code is for moving a servo. What you have isn't a servo. So, what, exactly, is this code causing your electric motor to do?

Why does it delay 5 seconds between changes in one direction, and 0 in the other?

i dont meen to correct you on everything you think you know but i promise you its a servo i got it from parallax. it even says continuous rotation SERVO on the front. ive used the same code with these before and it worked. dont get me wrong id appreciate your help but please don't criticize me.

What you call it, and what parallax calls it, does not really matter. It isn't a servo. It's a bi-directional electric motor.

If it was a servo, Servo.write(90) would make it move to 90 degrees and hold still. Does your "servo" do that?

To re-iterate, what exactly does the code you posted make your "servo" do?

okay how about instead of your negative remarks how about you just help a kid out instead of beating him down. what i have works with the code I am using. so if you could help me out with the ping part of it instead of being obsessed with the DEFINITION of "servo" that be great

The definition of "servo" here is rather crucial to its behaviour, so I think a little discussion about what is and what most certainly is not a servo is perfectly acceptable.

To call what you have a "servo" raises unfulfillable expectations, and the sooner people recognise that, the less time we will waste.

Do you understand "for" loops? (We'll ignore the comments for now)

   for(pos = 0; pos < 180; pos += 180)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(5000);                       // waits 15ms for the servo to reach the position 
  }
myservo.attach(11);
 for(pos = 0; pos < 1; pos += 500)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 

    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
 } 
 
 digitalWrite(ledPin2,LOW);
delay(5000);
  for(pos = 500; pos>=1; pos-=500)     // goes from 180 degrees to 0 degrees 
  {                                
  
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  //  delay(1);                       // waits 15ms for the servo to reach the position 
 }

this is the code that i used for my doorlock the 5000 delay is what made the motor go it ran for 5 seconds and the stopped. all im aiming to do is combine this with button code and ping code

Do you understand "for" loops?

 for(pos = 0; pos < 1; pos += 500)  /

im 16 theres alot i dont understand. for instance i dont know why getting some code is so hard. all i can tell you is the door lock works according to the set amount of time in the delay. its as if the other part of the code is ignored. but the code worked. i usually dont mess with something if it works :slight_smile:

maybe im being to rash ive got another servo motor one that only rotates 180 degrees. lets assume i wanted to make this work with the ping sensor could you please just help.

This:

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

    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
 } 
 
 digitalWrite(ledPin2,LOW);
delay(5000);
  for(pos = 500; pos>=1; pos-=500)     // goes from 180 degrees to 0 degrees 
  {                                
  
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
  //  delay(1);                       // waits 15ms for the servo to reach the position 
 }

is equivalent to this:

myservo.attach(11);
myservo.write(0); 
digitalWrite(ledPin2,LOW);
delay(5000);
myservo.write(500);

(but don't use this with an unmodified servo - use 180 instead of 500)

dont know why getting some code is so hard

It isn't hard, but you do have to work at it and understand it.
If it looks messy, then it can be harder to understand.

Here's the simplified Ping code.

unsigned long pingRange (int pingPin)
{
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);
}  

long microsecondsToInches(unsigned long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

thank you awol i very much appreciate your help im sorry if i was argumentative. and thanks again i think i have enough to go on now

There are three parts to a for loop. The initialization part (pos = 0) defines what is performed before the body of the loop is executed. The continue portion (pos < 1) defines whether or not the body is executed. 0 is less than 1, so the body will be executed. The final part, the increment portion (pos += 500) defines what happens after the body of the loop is (or is not) executed. So, after the loop executes the first time, pos is incremented by 500, to 500. Since 500 is no longer less than 1, the body of the loop will not be executed again.

Thus, the question is why you have a for loop that is guaranteed to be executed exactly once.

ok lets start over forget about the code i posted this is all i want to do

OK, but 2.5cm is quite a tough proposition for a sonar.