# How to control the angle of a servo motor?

Hi,

How do you control the angle of a servo motor with a thumbstick (like the ones on a playstation controller) by programming an arduino?

So what I'm doing in my project is making a toy crane. The servo motor controls the pivot/rotation of the crane. But i need to restrict the angle so it only rotates between 45degree and 165degree. So when i move the thumbstick to the left, the crane will rotate to the left but will stop at 45degree and when i move thumbstick to the right, the crane will rotate to the right stopping at 165degree.

Heres an image to help.
http://img694.imageshack.us/img694/8042/img2221r.jpg

## I am totally new to programming the arduino and i have went to look at the examples and i think these are the closest code to doing the job.

SWEEP code

``````[size=10]Code

// Sweep
// by BARRAGAN <http://barraganstudio.com>

#include <Servo.h>

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

int pos = 0;    // variable to store the servo position

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; 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
}
for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
{
myservo.write(pos);              // tell servo to go to position in variable 'pos'
delay(15);                       // waits 15ms for the servo to reach the position
}
}
``````

KNOB code

``````Code

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

#include <Servo.h>

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

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
myservo.write(val);                  // sets the servo position according to the scaled value
delay(15);                           // waits for the servo to get there
}
[/size]
``````

Which one would be the optimal choice for my case? I feel that the SWEEP one is closer to what i need because the KNOB code requires turning a knob and im only using a thumbstick. Very importantly, how would you edit the codes so it restricts the angles between 45 and 165degree?

Or are both codes incorrect for my use?

Any help would be greatly appreciated. Thank you.

I think that Knob is almost exactly what you want.

To limit the output to ranges you can use, modify the map function parameters.

``````val = map(val, 0, 1023, 0, 179);
``````

The last 2, 0 and 179, set the range of values sent to the servo to be between 0 degrees and 179 degrees. If you replace those with your values, the limit will be created.

like so:

``````val = map(val, 0, 1023, 45, 165);
``````

Now, the sketch uses analogRead to get the value from the potentiometer. You will need to replace that with whatever method works for your input device.
Also, arguments 2 and 3 of the map command (0 and 1023) are the expected range values for the pot input. If your input device has different range values, you would need to plug them in to the map command. Since I am not familiar with your input device, I am kind of guessing here.