Hello,
I am trying to use a joystick to control a robotic turret with my UNO. It basically has a pan/tilt function, and I need the servos to adjust position based on the position of the joystick. Then when the joystick returns to center, the servos should hold position (similar to the view control in any 3rd person shooter). I have been rather unsuccessful with any code I have done. I found a thread from around year ago that dealt with this same problem ( Making a joystick control for two servos to hold position - Project Guidance - Arduino Forum ), but the code posted there didn't work for me;
#include <Servo.h>
#define mainArm 0
#define jib 1
#define bucket 2
#define slew 3
Servo servo[4]; //code used for attaching up to 4 servos
byte angle[4] = {90, 90, 90, 90}; // middle point for servo angle
byte potPin[4] = {A0, A1, A2, A3}; // input pins to attach your potentiometers
byte servoPin[4] = {7, 6, 10, 9}; // input pins to attach servos
void setup() {
Serial.begin(9600);
Serial.println("Starting DiggerCode.ino");
for (byte n = 0; n < 4; n++) {
servo[n].attach(servoPin[n]);
}
}
void loop() {
readPotentiometers();
moveServos();
delay(10);
}
void readPotentiometers() {
int potVal;
for (byte n = 0; n < 4; n++) {
potVal = analogRead(potPin[n]);
if (potVal < 200) { // dead zone for the joystick I used is 200 to 550.
angle[n] += 1;
if (angle[n] > 170) {
angle[n] = 170;
}
}
if (potVal > 550) { // deadzone upper value
angle[n] -= 1;
if (angle[n] < 10) {
angle[n] = 10;
}
}
}
}
void moveServos() {
for (byte n = 0; n < 4; n++) {
servo[n].write(angle[n]);
}
}
This code did manage to move my servos, but it was very choppy and unpredictable. The two axis moved simultaneously by moving the joystick in one direction. I have a Hitech digital servo programmer I used to check the pulse outputs from the Arduino. They are pretty are consistent with the servo movements (beginning around 1800 and going as high as 1900 and as low as 1200 with sparadic high numbers thrown in like 16000). I am fairly new to Arduino and have never programmed anything before this. I was hoping that I could get some help understanding and fixing this code. I believe it should do what I need but I don't know what to adjust.
Any help at all would be greatly appreciated.
Clay