Continuous rotation control with Ping

Hello,

  1. Im trying to control a Parallax continuous rotation servo with a Ping sensor. Im detecting objects within a range of 50-500mm. I would like the servo to move in one direction if an object is moving towards the Ping and in the opposite direction when the object moves away. I can do this with a regular servo since you can map to angle but with the continuous Im a bit lost.

  2. I would like the servo to seem to react immediately to movement, at the moment it seems to need a sizeable amount of delay to act somewhat normally, I noticed a delay(200) worked but thats not fast enough. Anything less and it starts acting strange. Maybe its just my code.
    If anyone could take a look at my code I would be muchos appreciative.

#include <Servo.h>

Servo servo;

int pingPin = 7;
int pingValue = 0;

int servoPin = 9;

void setup() {
Serial.begin(9600);
servo.attach(servoPin);
}

void loop() {
long duration, mm;

pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);

mm = microsecondsToMillimeters(duration);

delay(100);

pinMode(servoPin, OUTPUT);

if(mm>500){
servo.write(1490);
delay(100);
Serial.print("over");
Serial.println();
//delay(10);
}
else if(mm<50){
servo.write(1490);
delay(100);
Serial.print("under");
Serial.println();
//delay(10);
}
else{
pingValue = map(mm, 50, 500, 1400, 1600);
servo.write(pingValue);
delay(200);
Serial.print(mm);
Serial.println();
//delay(20);
}
}

long microsecondsToMillimeters(long microseconds)
{
return microseconds / 29 / 2 * 10;
}

 pinMode(servoPin, OUTPUT);

Why do you have this in loop?

I can do this with a regular servo since you can map to angle but with the continuous Im a bit lost.

There is some value that makes the servo hold still. Values below that cause it to rotate one way. Values above that cause it to rotate the other way. How can you get lost on this simple fact?

I would like the servo to seem to react immediately to movement

That's kind of an unrealistic expectation, given the nature of the device that detects motion.

at the moment it seems to need a sizeable amount of delay to act somewhat normally, I noticed a delay(200) worked but thats not fast enough.

This delay, between setting the servo in motion and Serial.print of the value written to the servo, should not be necessary. Why do you think it is?

Thanks for the reply.
As you can tell Im a baby at this so making some silly mistakes.

I understand how it is working with regard to there being a value in the middle but I still cant figure out how I can make the servo slow down when moving toward and speed up when moving away.

Regarding Ping lag, I guess I should just accept it.

Regarding my crazy delays, this is a left over from my understanding of how the servo works in that it needs some sort of delay in order for a successful pulse. It had nothing to do with the Serial.print. Leaving them in seemed to help, otherwise the servo goes a little crazy.

Im gonna try with a stepper motor and the adafruit motor shield.

Done!! It works perfectly I am amazed
use google translate: