Servo help

Can some help me write a code for moving a servo that will go from 0 to 180 depending on the distance?
i am using a parallax ping sensor and one servo. any help would be awesome!

have you got details of your "parallax ping sensor" (maybe a datasheet or perhaps a link)

Do you have an understanding of how to do each of those things, ie read the sensor and move the servo, separately? If not, that's where you should start:

  • There are loads of examples of how to read that ping sensor: I'm pretty sure there's some in the Playground for a start
  • Either or both of the servo knob or sweep examples will help you with how to position the servo.

Then you need to consider how to move the servo based on the ping's reading, and for that you could consider using an if-else or switch case approach; maybe even map the ping reading straight to the servo angle.

Thanks for your reply! i have tried to use if-else and tried map. here is what code almost works but it does not sweep like i need it to. i would like it start off at 0 and the closer you get to it the closer it gets to 170?

#include <Servo.h>
Servo myservo;
const int pingPin = 7;
int x = 1000;
int pos = 0;
const int distancelimit = 10;
const int distancelimit2 = 15;
const int distancelimit3 = 20;
const int distancelimit4 = 25;
const int distancelimit5 = 30;

void setup()
{
Serial.begin(9600);
myservo.attach(9);
delay(1000);

}

void loop(){

int distance = ping(); // us the ping() function to see if anything is ahead.

if (distance < distancelimit){

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

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

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

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

}
}
int ping(){

long duration, inches, cm;

//Send Pulse

pinMode(pingPin, OUTPUT);

digitalWrite(pingPin, LOW);

delayMicroseconds(2);

digitalWrite(pingPin, HIGH);

delayMicroseconds(5);

digitalWrite(pingPin, LOW);

//Read Echo

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(50);

return round(inches);

}

long microsecondsToInches(long microseconds){

return microseconds / 74 / 2;

}

long microsecondsToCentimeters(long microseconds){

return microseconds / 29 / 2;

}

Morning....

I haven't had a full look at that, 5am here so I'm leaving for work in 20 minutes or so, but first comment is that I don't think you need to use that for-based sweep approach.

Looks like pos goes back to 0 at the start of each sequence? So why not just have a myservo.write() for each of the distances, like:

if (distance < distancelimit3)
   
  {                                  
    myservo.write(90);           
    delay(15);                       
  }

Have another go at map though: it should work with just a simple linear relationship between distance and angle: (not real code....)

// not real code
pos = (distance reading, closest dist, furthest dist, angle when closest, angle when furthest)

hey it worked thanks a lot for your help!

Glad to help: great that it worked

PS.... why not post your final code- it might help other members