Help with servo!

I need a little help with a servo.
What i want to do is that when i push a button, the servo goes from 0- 180 degrees.( in slow motion)
Than some delay, before it returns back to its starting position (0)

I hope someone understands me :wink:

My code till now looks like this :

int inputPin1 = 2;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
                // a maximum of eight servo objects can be created 
int pos = 0;    // variable to store the servo position 

void setup() {
 pinMode(inputPin1, INPUT); 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

void loop(){
  if (digitalRead(inputPin1) == LOW) 


HELP :-?

Here is the solution ot a similar problem:

(I'm just to lazy to retype it)


Thanks Korman, but i dont quite understand everything there ::) CAn somebody figure it out ? :-[


I'm not sure that the write method accepts negative angles.

You probably need to start with the below servo sweep example and incorporate a button function.

Step 1: Make you program work so that the servo moves at full speed but with a delay between the movements. Step 2: Replace Servo.h with VarSpeedServo.h and write by slowmove -- or -- Step 2 alternative: Add the code from Servo Sweep mentioned by Zoomcat. Step 3: Have a merry Christmas with your slow servo sweeper.

But anyway, you need to start with Step 1 and fix your program to do what you want (although just at full speed)


You are close… you have the logic for detecting the button press, you just need to fill in the logic to move the servo.

try replacing this line:



int deg;

for (deg = 0; deg <= 180; ++deg) { // go from 0 - 180
myserver.write(deg); // set the servo to that degree
delay(50); // bigger = slower sweep
for (deg = 180; deg >= 0; --deg) { // go from 180 - 0
myserver.write(deg); // same thing, but going down

That should do what you want, good luck!

Thanx for answers.

I will try this soon, and leave feedback. ::)