Hello,
-
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.
-
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;
}