2 joystick 4 servo last position problem

I am trying to control a robotic arm using 4 servos and 2 analog joysticks. I had found and modified a 1JS 2Servo code with last position memory which I had verified and uploaded and it ran perfect. I have now assembled and reloaded the code and only one axis works one each joystick, the other is non responsive. I am sure all of my connections are correct and I have troubleshooted by re-arranging the pins in the code as well as physically rearranging the contacts to confirm all directions on the joystick and all servos are operating. I don’t understand how it can work one time and then not anymore.

Here is my code…please help…you are my only hope

FINAL_Arm_code.ino (1.55 KB)

#include <Servo.h>

Servo panServo;      
Servo tiltServo;
Servo topServo;
Servo turnServo;

int servoPanPosition = 180;
int servoTiltPosition = 180;
int servoTopPosition = 180;
int servoTurnPosition = 180;
int joystickPanPin = A1;
int joystickTiltPin = A2;
int joystickTopPin = A3;
int joystickTurnPin = A4;
int joystickPanSpeed = 0;
int joystickTiltSpeed = 0;
int joystickTopSpeed = 0;
int joystickTurnSpeed = 0;
int servoPanPin = 10;
int servoTiltPin = 9;
int servoTopPin = 5;
int servoTurnPin = 6;

void setup() 
  pinMode(servoPanPin, OUTPUT);
  pinMode(servoTiltPin, OUTPUT);
  pinMode(servoTopPin, OUTPUT);
  pinMode(servoTurnPin, OUTPUT);

void loop() 
  joystickPanSpeed = (analogRead(joystickPanPin) - 512) / 50;
  joystickTiltSpeed = (analogRead(joystickTiltPin) - 512) / -50;
  joystickTopSpeed = (analogRead(joystickTopPin) - 512) / -50;
  joystickTurnSpeed = (analogRead(joystickTurnPin) - 512) / -50;
  servoPanPosition = constrain((servoPanPosition + joystickPanSpeed), 1, 180);
  servoTiltPosition = constrain((servoTiltPosition + joystickTiltSpeed), 1, 180);
  servoTopPosition = constrain((servoTopPosition + joystickTopSpeed), 1, 180);
  servoTurnPosition = constrain((servoTurnPosition + joystickTurnSpeed), 1, 180);
    // -512 to provide equal +/- numbers

I have finally figured it out. It seems (I am very new to this) that each joystick can only be attached to one pwm(one with the ~). I re-designated the pins and now and work fine.

It seems (I am very new to this) that each joystick can only be attached to one pwm(one with the ~).

PWM pins are digital outputs, whereas joysticks would normally be connected to analogue inputs.

thank you.

meant servo not joystick...sorry

Servos don't need to be connected to PWM (~) pins.

Don’t compress your code into one line like this because you have no easy way to debug it

 joystickPanSpeed = (analogRead(joystickPanPin) - 512) / 50;

Instead do it step by step

joyPanVal = analogRead(joystickPanPin);
joystickPanSpeed = (joyPanVal - 512) / 50;

I think you will have problems dividing an integer by 50 because you will lose a lot of data. Try to avoid the need for the division. Alternatively use float variables.

Print the values of joyPanVal and joystickPanSpeed so you can check that the maths is doing what you think it is.

I have not thought hard about this

servoPanPosition = constrain((servoPanPosition + joystickPanSpeed), 1, 180);

but my instinct is that this is wrong. What are you trying to achieve.

Have a look at the code in this excavator Thread - it may be conceptually similar to what you want.