Pan/Tilt Camera

Hello all,

i am a noob to the arduino world. i built a pan/tilt camera system for my rc car. i have the generic code for the x and y axis and have that working fine, but i would like the camera to stay in the last position that i directed it to and then recenter with the system with the button press in the joystick. i dont have the button added to the code just yet nor the write to serial bc i am trying to get the main action resolved first.

hardware:

arduino nano
4 servos ( other 2 are for zoom and other function)
2 axis joystick with momentary button

code :

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 record (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

}

Any and all help/advice would be gratefully appreciated in this little DIY project of mine.
Thanks in advance

but i would like the camera to stay in the last position that i directed it to

That is what a servo is supposed to do. You seem to be implying that the camera does not stay still. Or, am I reading too much into the question?

and then recenter with the system with the button press in the joystick.

Servos can't "recenter". They go to commanded positions. If there is some position that you think of as "centered", then, be all means, command the servo to go there.

the camera is stationary atop the mount and servos. when i direct the camera to the left and up it goes there and then when i let go of the joystick it snaps back to the center due to the joystick being centered by a spring. what i would like to attain is that when i release the joystick, the joystick will re-center and the servos will hold the last commanded position.

http://www.instructables.com/id/Arduino-Servo-Dual-Mode-Control-PanTilt-with-Jo/ this link has a great video that is what im looking to do except the led blinking.

You put all the pot/servo code in a conditional IF statement such that when the button/trigger is pushed the if statement is entered and the servos are positioned. When the button is released the conditional if is not entered, so the servos stay in their last positions and the joystick can be released. To recenter the servos with the joystick in its neutral position, just push the button and the servos will go to the current stick neutral position. below is some simple button code.

//zoomkat servo button test 12-29-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
int button2 = 5; //button pin, connect to ground to move servo
int press2 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(170);
  }    
  
  press2 = digitalRead(button2);
  if (press2 == LOW)
  {
    servo1.write(10);
  }
}

A simple code mod for a button like below might work.

panVal = analogRead(panPin); // read pan joystick position
panVal = map(panVal, 0, 1023, 0, 179); // scale reading to servo
press1 = digitalRead(button1);
if (press1 == LOW) panServo.write(panVal);

thank you for your quick replies, pardon my...ineptness on the matter but could you expound on the conditional IF and non conditional IF. once again im new to this and have the slightest clue to what you are talking about. i will google the conditional IF and the non conditional IF to further my knowledge but your advice would be greatly appreciated.

Thanks again

You can try the below using a wire between pin 4 and ground to simulate your button (wire connected, servos move, wire not connected, servos don't move). I haven't tried this so YMMV.

#include <Servo.h>
int button = 4; //button pin, connect to ground to move servo
int press1 = 0;

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 record (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()
{

  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(4, HIGH); //enable pullups to make pin high  

  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
  if (press1 == LOW) 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
  if (press1 == LOW) 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
  if (press1 == LOW) 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
  if (press1 == LOW) downServo.write(downVal); //move servo to required position
}

Again thanks for your quick responses, i greatly appreciate your help. i will hook the unit up as you stated and upload the sketch and see what happens and keep you in the loop and also do a little reading up on the un/conditional "IF" in the morning. Im debating on just not even worrying about the button depress to center bc its really easy to depress that button on the ps2 controller joystick and i would not want it to recenter while looking around, just a thought. there is so much more than i originally thought that goes into this, but at the same time you learn so much more. lol

thanks again good sir
captain

also do a little reading up on the un/conditional "IF

I don't think there is such a thing as an unconditional IF. Lots of info at the arduino main site.

well i broke a servo because it couldnt take the weight, i ordered 2 28BYJ-48 5V Stepper Motorx and ULN2003 driver boards. From what ive read these are actually quite nice for the price and pretty straight forward to hook up and will accomplish what im am looking to do and possibly be a little more stable than the servo setup. I have to learn how to code now, have you ever hooked used a stepper motor and driver controller?

I have to learn how to code now, have you ever hooked used a stepper motor and driver controller?

Yes. They are as easy to use as servos. The driver board should have DIR and STEP pins that connect to two pins on the Arduino, as well as Gnd and 5V. The DIR pin controls direction. Setting the step pin HIGH, pausing, and then setting it LOW causes the motor to step once.

The Stepper library makes it even easier.

Of course, by now you should know that links are worth far more than words when describing what you bought.

http://lightake.com/detail.do/sku.28YBT_48_DC_5V_Stepper_Motor_with_ULN2003_Driver-58588 is the motor and driverboard that i bought for the camera setup.

I have a couple of those. I haven't done anything with them yet, but, if you've destroyed a servo already, because you are overloading it, I don't think those itty, bitty steppers are going to hold up.

I am sorry if this is the wrong forum for this question. I am a newbie. I have 2 servos and want to control them ultimately through a web interface to pan tilt a web camera. I am trying to follow the instruction of this website http://blog.aus10.org/?p=102 but to no avail. She talks about uploading code and then being able to control the servos via the serial monitor. I don't know what commands to use. Could you please at least point me in the right direction. I am very confused. I tried contacting the author of the webpage but she won't get back to me. Also how do you create a new post. I was told there is a "new topic" button but I'm unable to find it.
Thanks so much for any help you can provide me!!

Shawn.

I was told there is a "new topic" button but I'm unable to find it.

Go to the main topic page like below and there it is along the green stripe near the top of the page.

http://arduino.cc/forum/index.php/board,14.0.html

Thank you very much. I have seen the new topic button before it's just hard to find :slight_smile: Do you have any documentation on the other information in my post?

welchsc:
Thank you very much. I have seen the new topic button before it's just hard to find :slight_smile: Do you have any documentation on the other information in my post?

You need to start a new topic to discuss your particular issues.