Can not stop servo from running

Hello Everyone.
I wrote a code for my Windows Blinds. The project involves Continuous Servo.
The below code makes servo turn in right directions but it never stopes to run. Any ideas od how to fix it?

//Create servo object to control a servo
Servo myServo;
int posCurrent;

void setup() 
{
    //Attaches the servo on pin D0 to the servo object 
    myServo.attach(D0);
    posCurrent = 0;
    
    //Register our Particle to control the servo
    Particle.function("openBlinds", openBlinds);
    Particle.function("closeBlinds", closeBlinds);
}

void loop()
{ 
}

int openBlinds(String command)
{
    //Particle.publish("Position", "currently at" + posCurrent);
    
    myServo.write(10); //clockwise slow
    delay(30000);
    for(int pos = 10; pos > 0; pos -= 1) // 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(15); // waits 15ms for the servo to reach the position 
	} 
   
   //done
   return 1;
}

int closeBlinds(String command)
{
    myServo.write(-90); //counter clockwise slow
	delay(30000);
	for(int pos = -90; pos < 0; pos += 1) // 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(15); // waits 15ms for the servo to reach the position 
	} 
	
   //done
   return 1;
}

If you are using a continuous servo, the idea of position/degrees doesn’t work.

I think the values control the speed at which it moves and the direction.

What i would suggest is to figure out the range of values that will stop the servo, turn clockwise and anticlockwise.

Also, for better coding practice:

  1. long delays are not recommended in a function called by Particle.function(). You should set a flag that will be polled in loop() and do the necessary stuff there.
  2. A long delay() is better off with a non-blocking implementation using millis()
2 Likes

All of the above and I’d think you need to continue the for() till pos == 0 to actually stop and finally I’d also detach the servo.

1 Like

For window blinds with a continuous rotation servo, I would look at adding limit switches to control the rotation since as @kennethlimcp pointed out, the parameter controls the speed, not the position in this case.

2 Likes