# ps2x in functions?

I have an arm from Lynxmotion i am controlling with my Mega1280 using an SSC-32 from lynxmotion.

The general control scheme in my working code looks like this

``````if (ps2x.Analog(PSS_RX) >= 0 && ps2x.Analog(PSS_RX) <= 127){      // If (Right joystick is left of center) do the below items
azimuthVal = map (ps2x.Analog(PSS_RX),0,127,jointSpeed,0);   // Adjusts the speed at which the joint moves relative to the
amount of joystick used | To change the jointSpeed modify
the value of the variable
azimuthPos = azimuthPos - azimuthVal;                                 //  Take the current position of the Azmiuth joint and subtract
the value needed to adjust it to the new position
if (azimuthPos <= 600){                                    // If the value makes it to the lower limit of the possible swing of the servo
azimuthPos = 600;                                          //  stop the counter at this value
}

Serial1.print("#1 P");                                      // Making string for SSC-32: Azimuth is Servo #1, P indicates the positioning wanted
Serial1.println(azimuthPos);                                //  Positioning value needed to move the servo to
}

if (ps2x.Analog(PSS_RX) >= 129 && ps2x.Analog(PSS_RX) <= 255){               // Same as above but move joystick to the right
azimuthVal = map (ps2x.Analog(PSS_RX),255,129,jointSpeed,0);
azimuthPos = azimuthPos + azimuthVal;
if (azimuthPos >= 2400){
azimuthPos = 2400;
}

Serial1.print("#1 P");                                          // Azimuth Servo is #1
Serial1.println(azimuthPos);
}
``````

I have this set up for all of the servos on the arm. I call them azimuth, shoulder, elbow, wrist, and jaw. All is well with that program. It’s just quite a few repetitions of the above code. I have done a proof of concept now and was looking to get better at coding so i decided to see if i could condense the long form version of the program and make a function for analog functions and a function for the digital functions.

i was hoping for something like:

`````` analogMotion(ps2x.Analog(PSS_RX), azimuth, 600, 2400, 1);

void analogMotion(String analogStick,String joint,int lowStop,int highStop,int servoChannel){

if (analogStick >= 0 && analogStick <= 127){
jointVal = map(analogStick, 0, 127, jointSpeed, 0);
joint = joint - jointVal;

if (joint <= lowStop){
joint = lowStop;
}

Serial1.print(35, BYTE);  //prints # to Serial
Serial1.print(servoChannel, BYTE);   //prints the proper servo channel number for ssc-32
Serial1.print(32, BYTE);  // prints a space
Serial1.print(80, BYTE);  // prints the letter P
Serial1.println(joint);  //Print the numeric value for the servo position and a <CR>

}

if (analogStick >= 129 && analogStick <=255){
jointVal = map(analogStick, 255, 129, jointSpeed, 0);
joint = joint + jointVal;

if(joint >= highStop){
joint = highStop;
}

Serial1.print(35, BYTE);  //prints # to Serial
Serial1.print(servoChannel, BYTE);   //prints the proper servo channel number for ssc-32
Serial1.print(32, BYTE);  // prints a space
Serial1.print(80, BYTE);  // prints the letter P
Serial1.println(joint);  //Print the numeric value for the servo position and a <CR>

}
}
``````

I have tried different types where you see String but can’t seem to make it work. Any suggestions on how i could use a function for my movements and call out a certain button in the ps2x library properly? I can’t seem to find a way to drop something like ps2x.Analog(PSS_RX) into the call of the function.

Any help is appreciated…

The map function takes an int as the first argument. You can’t pass it a String object. So, that pretty much defines the type analogStick.

Since the map function returns an int, and you are combining this value with joint, or setting joint to an int, it’s pretty obvious what type joint ought to be.

I think i see what you are saying. But if

ps2x.Analog(PSS_RX) is an int value between 0 and 255

and analogStick = ps2x.Analog(PSS_RX)

Wouldn’t map(analogStick, 0, 127, jointSpeed, 0) == map(ps2x.Analog(PSS_RX), 0, 127, jointSpeed, 0) as it does in the first set that is tested and working?

i think my error is coming in where i don’t know how to make the call properly reference what value i should read form the controller for that iteration of the function. In this case the arduino would see ps2x.Analog(PSS_RX) as a string rather than a ‘variable’ from the library… am i understanding your statement properly?

the same issue presents with the second portion. I am trying to replace “joint” with “azimuth” but i am getting a string instead of a variable reference…

If i am correct in understanding you… how would you recommend i make my call to be more functional? I am just trying to refine my coding skills so i am looking for more efficient ways to do things.

Thanks for your help.

In all the code you posted in the first post, you are calling the ps2x.Analog() function with the same value (PSS_RX) over and over again. Just call it once, and store the result in an appropriately typed variable (int), named analogStick. Pass that value to the function, in an int variable.

understood. So i would end up with something like this in my full code..... (abridged version)

``````int RX;
int RY;
int LX;
int LY;
int azimuth = 128;

void loop () {

RX = ps2x.Analog(PSS_RX);
RY = ps2x.Analog(PSS_RY);
LX = ps2x.Analog(PSS_LX);
LY = ps2x.Analog(PSS_LX);

analogMotion (RX, aximuth, 600, 2400, 1);

}
``````

So my joint values are stored as int and refreshed each loop. but passed as integers.

So my joint values are stored as int and refreshed each loop. but passed as integers.

Yes, although RX, RY, LX, and LY are lousy names. Don't the values have more meaning than that?

in this case. no.

its a ps2 controller. left x is side to side left y is up and down. same for right joystick. in my sketch they will do a few different things at different times so if I name them for the function it would be more confusing than if I refer to them as the x and y axis of the left and right joystick.

in this case. no.

its a ps2 controller. left x is side to side left y is up and down. same for right joystick. in my sketch they will do a few different things at different times so if I name them for the function it would be more confusing than if I refer to them as the x and y axis of the left and right joystick.

The variables are local. Each time you read a value, it is stored in a new variable. At any point, a reading is to be used for some specific purpose. The name of the variable that the reading is stored in should reflect that purpose, not where the reading came from. In my opinion. Your mileage may vary.

I see your point. In most cases i would or if I would be storing these variables inside of the function where it is needed.... which I suppose would be possible.

in this case i am using these Numbers in a call. so I would be specify in that call all needed information:

source of control, which would be my controller assignments as I know them
desitantion of command, the channel on the ssc32
high and low limits, to stop motion before physical impact
speed limit of joint.

so if I am always passing this into a function as a value directly from the controller I am not seeing how it would benefit the flow of the program to name is as the distination rather than the source....

maybe its just the way we see the flow in our minds.

in either case, the help is much appreciated. "