Limiting Servo Range

I'm controlling two servos with a joystick. Currently, the code I am using will move a servo based on whether I push the joystick in the horizontal or vertical direction and the servo will remain in that final position, which I do want.

I'd like to restrict the range of both servos. I only need +/- 20 degrees from the center position (90 degrees). I'm just not sure what I change in my current code to achieve that.

#include <Servo.h>

Servo servo1;
Servo servo2;
int x_key = A1;
int y_key = A0;
int x_pos;
int y_pos;
int servo1_pin = 8;
int servo2_pin = 9;
int initial_position = 90;
int initial_position1 = 90;

void setup ( ) {
Serial.begin (9600) ;
servo1.attach (servo1_pin ) ;
servo2.attach (servo2_pin ) ;
servo1.write (initial_position);
servo2.write (initial_position1);
pinMode (x_key, INPUT) ;
pinMode (y_key, INPUT) ;
}

void loop ( ) {
x_pos = analogRead (x_key) ;
y_pos = analogRead (y_key) ;

if (x_pos < 300){
if (initial_position < 10) { }
else{ initial_position = initial_position - 20; servo1.write ( initial_position ) ; delay (100) ; } }
if (x_pos > 700){
if (initial_position > 180)
{
}
else{
initial_position = initial_position + 20;
servo1.write ( initial_position ) ;
delay (100) ;
}
}

if (y_pos < 300){
if (initial_position1 < 10) { }
else{ initial_position1 = initial_position1 - 20; servo2.write ( initial_position1 ) ; delay (100) ; } }
if (y_pos > 700){
if (initial_position1 > 180)
{
}
else{
initial_position1 = initial_position1 + 20;
servo2.write ( initial_position1 ) ;
delay (100) ;
}
}
}

Hi amencurley, welcome to the forum.
One thing I would change is to format your code. CTRL-T from the editor. This makes it SO much easier to read and hunt down problems with mismatch curly braces.

To your question, there are a few ways to address it.
The servo.Attach method has optional arguments for the min and max pulse width values. With those your servo commands will be mapped into whatever range you define with min and max. The defaults for those are something like 544 and 2400.
Try somehitng like this (untested) code and see how it behaves.

servo1.attach (servo1_pin, 1000, 2000) ;
1 Like

Thank you for your response. When I use the parameters in servo.attach, 1000 & 2000 work for min and max values, but if I try to narrow the range any more than that, the initial position changes and the servo returns to its full range. Do you know why this may be happening with my current code?

Also, is there a way to slow the servos' speed when the joystick is activated?

Thank you!

amencurley:
When I use the parameters in servo.attach, 1000 & 2000 work for min and max values, but if I try to narrow the range any more than that, the initial position changes and the servo returns to its full range. Do you know why this may be happening with my current code?

Also, is there a way to slow the servos' speed when the joystick is activated?

You should be able to narrow the range further with e.g. attach(servo2_pin, 1200, 1800). Your valus hould always be centred round 1500. But I can't see your current code so I have no idea why it wouldn't.

A few Serial.prints would help you know exactly what you are writing to the servo. I can't actually follow what you're trying to do in that part of the old code I can see so you're on your own there.

As for the speed you generally just do that by moving the joystick more slowly. Surely the idea is that servo follows the joystick rather than lagging behind it?

Steve

Based on your code, since your joystick is just on/off for each direction you can simulate speed control by incrementing a variable each time a x/y button is held down and resetting that variable back to zero when the button reads between 300-700.

Use that variable instead of “20” for your position increments.

The longer you hold down the direction, the faster it will move.