Trouble with parallax servo

Hi, recently I am doing a project with a parallax servo and a distance sensor. I m trying to use the servo as a winch, so that someone is in front of the sensor, the winch moves down and stay. If sensor did not detect anyone in front, the winch goes back up and stay.

Since I will need more rotation than a standard servo, a continuous rotation servo is used.

I have been trying different codes. Since it is different than a normal servo, I cannot get the servo to spin for a few times and stop.
Can anyone help me with coding on arduino?

THANKS

This is what I have so far.

int sensorPin = 0;
int servoPin = 9;

int fullCounter = 1200; // 1.2ms
int stopped = 1500; // 1.5ms
int fullClock = 1800; // 1.8ms

int dirInt = 1500; //stopped to start

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(servoPin, OUTPUT);  
 }
 
void loop() {
   int analogValue = analogRead(sensorPin);
   Serial.println(analogValue);
   delay(50);
   
   digitalWrite(servoPin,HIGH);
   delayMicroseconds(dirInt);
   digitalWrite(servoPin,LOW);
   delay(20); // 20ms
   
   if (analogValue >= 200) {
     dirInt = fullCounter;
     
   } else {
     dirInt = stopped;
     
   }
   
   
     Serial.println("Position");
     Serial.println(dirInt);
}

If you want to stop a continuous "servo" (don't get me started), simply "detach" it.

(PS, that means using the servo library, or in your case, just stop pulsing it)

I am new to arduino.
As far as I know, the servo can be stopped at delayMicroseconds 1.5.
But the thing is how can I make it spin for a number of time then stop it, while triggered by the distance sensor.

first of all I would suggest using the servo library

then you can simply

myservo.write(0) or myservo.write(180) , depending on direction

and use the

delay(X)

where X is the delay in ms

the "servo" will rotate for X milliseconds.

to stop the servo just

myservo.write(90)

on the parralax continous rotation servo I have there is a POT that needs to be adjusted so that the myservo.write(90) actually stops the servo.

Read through the library examples :slight_smile:

But the thing is how can I make it spin for a number of time

Without the addition of some external device to count revolutions, you can't.
([rant] I wish suppliers would stop referring to knobbled R/C servos as "servos" [/rant])

However, the necessary device could be as simple as a microswitch or a photo-interrupter.

Thanks for all the help.
I think I got it figured out. Here is the code if anyone is interested.
However, there is still one minor problem.
When the servo goes left and then back right. It wont stop at the initial position. It goes a little more than the initial position.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

int go = 0;// Servo go value
int cw = 180;// Servo reverse
int c = 0;
int i = 0;
int spin = 2000; // delay time for servo to stop spinning

int sensorPin = 0;

void setup() {
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
//  myservo.attach(9);
 }
 
void loop() {
   int analogValue = analogRead(sensorPin);
   Serial.println(analogValue);
   delay(50);
   
   
   if (analogValue >= 200) {
     if (c == 0){
     myservo.attach(9);
     myservo.write(go);  
     delay(spin);
     myservo.detach();
     c++;
     i=0;
     }
     else if (c == 1) {
     myservo.detach();
     }
   }   
   else {
     if (i == 0){
     myservo.attach(9);
     myservo.write(cw);  
     delay(spin);
     myservo.detach();
     i++;
     c=0;
     }
     else if (i == 1) {
     myservo.detach();
     }
     
   }
   Serial.println(c);

}

You might consider to use OptoEndStops, there might be some slack in you system (mechanic or electronic translation), so having absolute breakers will help, might just consider to stop the motor a bit sooner on the way back, this has to be calibrated.

David