help with adding if statements to servo control code

hello all,

i am a noob, and im having difficultly understanding the if statements needed to keep my servos at a commanded point when using a spring centered joystick. i have all servos doing what they are supposed to do using simple servo libs. its just that when i get the camera to where i want it and release the stick, it automatically recenters bc of the potentiometer reading going back to the middle. 2 of the 4 servos that do that are actually what i need, lol happened by default. But the pan and tilt are the two that i would like stay in the last position that i directed them to until i move them left/right or up/down again. i have been reading about the conditional "IF" and havent found a clear tutorial on it yet, so i need some guidance on this issue. the code that i use is below and again it is very simple.

#include <Servo.h>

Servo panServo; // Create a servo object for the pan (horizontal) servo
Servo tiltServo; // Create a servo object for the tilt (vertical) servo
Servo zoomServo; // Create a servo object for zoom (left/right) servo
Servo downServo; // Create a servo obhect for torch (down/up) servo

int panPin = 0; // Analog input for the joystick pan axis
int tiltPin = 1; // Analog input for the joystick tiltl axis
int zoomPin = 2; // Analog input for the zoom joystick axis
int downPin = 3; // Analog input for the down axis

int tiltVal; // Value read from the vertical axis
int panVal; // Value read from the horizontal axis
int zoomVal; // Value read from the zoom axis
int downVal; // Value read from the down axis

void setup()
{
panServo.attach(9); // use pin 9 PWM output for pan servo
tiltServo.attach(10); // use pin 10 PWM output for tilt servo
zoomServo.attach(6); // use pin 6 PWM output for zoom servo
downServo.attach(5); // use pin 5 PWM output for down servo
}

void loop ()
{
panVal = analogRead(panPin); // read pan joystick position
panVal = map(panVal, 0, 1023, 0, 179); // scale reading to servo
panServo.write(panVal); //move servo to required position

tiltVal = analogRead(tiltPin); // read pan joystick position
tiltVal = map(tiltVal, 0, 1023, 0, 179); // scale reading to servo
tiltServo.write(tiltVal); //move servo to required postion

zoomVal = analogRead(zoomPin); // read zoom joystick position
zoomVal = map(zoomVal, 0, 1023, 0, 179); // scale reading to servo
zoomServo.write(zoomVal); //move servo to required position

downVal = analogRead(downPin); // read record joystick position
downVal = map(downVal, 0, 1023, 0, 179); // scale reading to servo
downServo.write(downVal); //move servo to required position
}

thanks in advance

If real question is - how does the code known that you no longer want to move the servos to match the joystick? How will it know that you want it to start following the joystick again?

Mark

Similar topic

capt_carroll:
hello all,

i am a noob, and im having difficultly understanding the if statements needed to keep my servos at a commanded point when using a spring centered joystick. i have all servos doing what they are supposed to do using simple servo libs. its just that when i get the camera to where i want it and release the stick, it automatically recenters bc of the potentiometer reading going back to the middle. 2 of the 4 servos that do that are actually what i need, lol happened by default. But the pan and tilt are the two that i would like stay in the last position that i directed them to until i move them left/right or up/down again. i have been reading about the conditional "IF" and havent found a clear tutorial on it yet, so i need some guidance on this issue. the code that i use is below and again it is very simple.

You've already been given a considerable amount of advice about this question in the other thread you started, so you need to be clear what part of this problem you need help with.

It seems to me that the simplest solution would be to get a joystick with a ratchet instead of a self-centering action.

If you intend to continue with the self-centering joystick then you need to figure out how you're going to tell the system when the servo is supposed to be following the joystick, and when it's supposed to stay put. That's not an Arduino question - it's a general problem that you need to choose a solution for. For example, you might incorporate a momentary switch into your joystick and hold the switched closed when you want the servo to follow the joystick. But only you can decide how you're going to do it.

Once you have figured how to address that issue, implementing that behaviour within an Arduino sketch would be easy.