# PS2X library how to use a joystick?

I don't know much about arduino's c language so I am pretty confused on how to use the value printed in the serial monitor by the joystick of the ps2 controller ranging from 127 (for the Y and 128 for the X) in a neutral position to 255 and 0 in forward and reverse positions of the joystick.

Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX

I am building a soccer robot with hobby servos with 180 degrees of rotation and I want to vary the distance of each step by the degree of the joystick's position for example a 50 value would be divided by 127 and multiplied by a neutral position of 90 degrees to tell each motor to rotate in a pattern of ~35.5 degrees for a casual walking motion and if the value 255 were returned the robot would do a split or go into a full sprint or something along those lines.

I cannot figure out what Serial.println(ps2x.Analog(PSS_LY), DEC); means because when I made:
Serial.println(ps2x.Analog(90 * ((PSS_LY), DEC) /127)); the serial monitor would print a lot of 0's and 1's. I then tried several different versions of divisible and multiples with the most interesting results coming from dividing the subject only by .5 and being returned with values ranging from 0 to 2000 and dividing by the value 2 returning values of 0-67 with a high range of letters like CF and some other stuff. I thought this was just a serial print issue but I cannot get my servos to cooperate with this code at best getting the servos to only turn to low and neutral positions.

I also tried;
float x = (ps2x.Analog(PSS_LY), DEC);
Serial.println(x);
and got the strict result of 10 without any changes.

Do any of you have any thoughts on how to use this part of the ps2x library?

If it helps, this is what I use to control my servos at the moment:
#include <Wire.h>

#define MIN_PULSE_WIDTH 500
#define MAX_PULSE_WIDTH 2500
#define DEFAULT_PULSE_WIDTH 1500
#define FREQUENCY 50

pwm.setPWM(0, 0, pulseWidth(0)); //Servo 0 set to angle 0 degrees
pwm.setPWM(1, 0, pulseWidth(180)); //Servo 1 set to angle 180 degrees

int pulseWidth(int angle) {
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH,MAX_PULSE_WIDTH);
analog_value = int (float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;

/* I wasn't sure if there was a problem reading decimals so I changed all the ints to floats but that hasn't helped nor did any harm.

float pulseWidth(float angle) {
float pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH,MAX_PULSE_WIDTH);
analog_value = float (float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;
*/

I cannot figure out what Serial.println(ps2x.Analog(PSS_LY), DEC); means

It means call the ps2x.Analog() function, with the value PSS_LY, and print the returned value in base 10.

``````int yVal = ps2x.Analog(PSS_LY);
Serial.println(yVal, DEC);
``````

would do the same thing AND allow you to use the value in the variable in if statements, map() calls, etc.

Thank you so much and for the quick response! So happy it works