How to Slow a Servo down using Arduino.

I am currently working on a project for a charity display. The project I am working on envolves servos, I have pieced some code together but need to slow the servo down. The circuit involves a push button switch that that is supposed to go from 0 to 90 degrees when pressed and when let go, back to 0. Im not sure how to slow the servo down whilst keeping the button working any help would be greatly appreciated. I will attack code below.
Cheers
Tom

#include <Servo.h> //include the servo library for this program

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

void setup() //do these things once

{

Serial.begin(9600); //set data transmission rate to communicate with computer

pinMode(8,INPUT_PULLUP); //pin 8 forced to HIGH when there is no external input

myservo.attach(9); //attaches the servo on pin 9 to the servo object

myservo.write(60); //tells servo to go to 60 degree position

}

void loop() //do these things forever

{

while(digitalRead(8)== HIGH)

{

myservo.write(60) //…servo position is 60 degrees

Serial.print(“open”); //print the word “open”

Serial.println(""); //print nothing, go to next line

}

myservo.write(150); //otherwise, servo position is 150 degrees

Serial.println(""); //print nothing, go to next line

Serial.println(“closed”); //print the word “closed”

Serial.print(“waiting 5 seconds”); //print “waiting 5 seconds”

Serial.println(""); //print nothing, go to next line

delay(5000); //waits for 5 seconds before re-doing the loop

}

Swap Servo.h for VarSpeedServo.h and use the speed setting features provided.

Steve

Hi tomp1111,
Welcome to the forum.
I see that you used the quote block to post your code. That is close. The code tags are the way to do that. That way the forum software won't get confused and put in smiley faces.

Slowing a servo down is fairly straight forward. Tell it to move a very small distance from where it is now and then wait a short time. Repeat until you get all the way to the actual destination.
Take a look at the sweep tutorial for a simple example.

And while you are looking at that, consider reading through Robin2's Demonstration code for several things at the same time

Have a look at the servo-sweep example that comes with the Arduino IDE and at the servo code in Several Things at a Time

To make it easy for people to help you please use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

…R

Cheers, managed to get it working great. Thanks for the help.

Hi,
Can you post your working code so it will help others with a similar problem?

Thanks.. Tom... :slight_smile:

TomGeorge:
Hi,
Can you post your working code so it will help others with a similar problem?

Thanks… Tom… :slight_smile:

Hi attaching the working code now, you have to dowload the relevant library at GitHub - netlabtoolkit/VarSpeedServo: Arduino library for servos that extends the standard servo.h library with the ability to set speed, and wait for position to complete and then apply it at sketch to your project.

#include <VarSpeedServo.h>


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


void setup()                //do these things once

{

  Serial.begin(9600);           //set data transmission rate to communicate with computer

  pinMode(8,INPUT_PULLUP);      //pin 8 forced to HIGH when there is no external input

  myservo.attach(9);        //attaches the servo on pin 9 to the servo object

  myservo.write(0);        //tells servo to go to 60 degree position

}


void loop()                     //do these things forever

{

  while(digitalRead(8) == HIGH) //as long as pin 8 is HIGH,...

    {

    myservo.write(0,40,true);      //...servo position is 0 degrees at 40 speed
    


    Serial.print("open");       //print the word "open"

    Serial.println("");         //print nothing, go to next line

    }
   myservo.write(90, 40, true);
  

 
  
  Serial.println("");           //print nothing, go to next line

  Serial.println("closed");     //print the word "closed"

  Serial.print("waiting 5 seconds"); //print "waiting 5 seconds"

  Serial.println("");           //print nothing, go to next line

  delay(0);              //waits for 0  seconds before re-doing the loop

}