# PS2 motor control

I am trying to get a ps2 joystick to control a motor through an arduino.
Can you please help ?
Below is the code on the Arduino.

#file : home/wkinne/ps3joymotor.c edit raw
```LY=ps2x.Analog(PSS_LY);
Serial.println(LY);
if (LY < 127) {
spd = map(LY, 126, 0, 0, 257);
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(spd);
}
if (LY > 127) {
spd = map(LY, 127, 257, 0, 257);
analogWrite(enB, spd);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(spd);
}
if (LY == 127) {
spd = 0;
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println(spd);
}
```

## Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

### opps

sorry for the double print. code is:

LY=ps2x.Analog(PSS_LY);
analogWrite(enB, LY);
if (LY > 126)  {
LY = 200;
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(LY);
}
else (LY < 127);  {
LY = 80;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

it only responds to else part it seems. We are trying to control a motor on a L298N but we only get one direction and we also want it to vary the speed. So we need to make the center of the joy stick make LY = 0 and all the way up and down make LY = 256

### step 1      print out the

step 1
print out the value before the if statement, you got to see what you have before you try more stuff...

Then run it - then you get cause --->effect .. you play with the joystick and just see the values of the movement.

For example is 0 == deadstick ?

-1 = down & 1 = up ?

Typically this is how it is .. (except deadstick is very often not 0 .. it "close" to example -> 0 .. 0.0003939

### LY=ps2x.Analog(PSS_LY);

LY=ps2x.Analog(PSS_LY);
analogWrite(enB, LY);
Serial.println(LY);
if (LY < 127)  {
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(LY);
}
else (LY == 127);  {
LY = 0;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}
else (LY > 126);  {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

### Try this

LY=ps2x.Analog(PSS_LY);
analogWrite(enB, LY);
if (LY > 126)  {
LY = 200;
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(LY);
}
else  {
LY = 80;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

The problem with you code was that the else statement would execute  (LY < 127);
Then always this part
{
LY = 80;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

### latest

LY=ps2x.Analog(PSS_LY);
analogWrite(enB, LY);
Serial.println(LY);
if (LY < 127)  {
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(LY);
}
else (LY == 127);  {
LY = 0;
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}
else (LY > 126);  {
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

### ok .. you said the ps3

ok .. you said the ps3 joystick library gives you values

0 (full down)  <-->  127 (deadstick) <--> 256 (full up)

So you want your if statement to be

if  LY < 127
go forward
else if LY > 127
go reverse
else
stop

The default I like to have "stop" it makes it easy then to "widen" the deadstick area.
For example :

if  LY < 126
go forward
else if LY > 128
go reverse
else
stop

Now there is a bit more deadstick so the robot doesnt go when you don't want it to go...

### not working yet

We seem to be closer but still not there. It controls the motor in one direction only, but does seem to control speed. I am working with my student team programmer over facebook so it is hard to do. We have class Tuesday, Wendsday, and Thursdays. I need to bring home one of the extra motors so I can try this at home too.

When we tried "else" statments we get an error: https://www.facebook.com/photo.php?fbid=1292902834189512&set=p.1292902834189512&type=3&theater&ifg=1

Not sure if you can use the link or not.

Any thoughts?

LY=ps2x.Analog(PSS_LY);
Serial.println(LY);
if (LY < 127) {
Spd = map(LY, 126, 0, 0, 257);
analogWrite(enB, LY);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(LY);

if (LY > 127) {
Spd = map(LY, 127, 257, 0, 257);
analogWrite(enB, LY);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

if (LY == 127) {
LY = 0;
analogWrite(enB, LY);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(LY);
}

High on "in3" and Low in "in4" causes the motor to spin in one derection and High on "in4" and Low in "in3" causes the motor to spin in the oposite direction, "enB" is motor speed.

### You are setting a speed value

You are setting a speed value by mapping it from your joystick value, but I dont see you using it?

In addition, if you are using an H-bridge isnt it better to do a brake with both pins low instead of one high and the other low?

### Opps and thanks!

I was not sure about the double LOW's and ya I forgot some code LOL thanks! Hope this looks better!

LY=ps2x.Analog(PSS_LY);
Serial.println(LY);
if (LY < 127) {
spd = map(LY, 126, 0, 0, 257);
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(spd);

if (LY > 127) {
spd = map(LY, 127, 257, 0, 257);
analogWrite(enB, spd);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(spd);
}

if (LY == 127) {
spd = 0;
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println(spd);
}

### full code

•
• #include <PS2X_lib.h>  //for v1.6
#include <Servo.h>

PS2X ps2x; // create PS2 Controller Class

//if(ps2x.Button(PSB_R1)) means if the button is being held down
//if(ps2x.ButtonPressed(PSB_R1)) is if it was jut pressed
//if(ps2x.ButtonReleased(PSB_R1) is if it was just released

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;
byte attempts = 0;

#define TIBIA 45
#define DELAY 300

#define COXA_CCW 70
#define COXA_CW 105

#define AC_UP 92
#define AC_DOWN 125

int UP = AC_UP;
int DOWN = AC_DOWN;
int joyStick;
int R2;
int L2;

int enB = 5;
int in3 = 7;
int in4 = 6;
int spd = 0;
int LY;
/*
~back~
6 5
4 3
2 1
~front~
*/

Servo jaw;   // servo to open and close jaw

int jawPos = 130; // variable to store jaw servo position - default closed

void setup(){
Serial.begin(57600);

pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(52, OUTPUT);

jaw.attach(52,70,180);    // attaches jaw servo on pin 3 to the object jaw

jaw.write(jawPos);

//CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************

while(attempts<25) { //loop around until a PS2 controller is found or we've attempted 25 times
error = ps2x.config_gamepad(13,11,10,12, false, false);   //setup pins and settings:  GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
if((error==0)||(error==3)) attempts=26; //PS2 controller was found, or found with pressure disabled so we'll get out of this WHILE loop
Serial.println(attempts);
delay(50);
attempts++;
}

if(error == 0){
Serial.println("Found Controller, configured successful");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Go to www.billporter.info for updates and to report bugs.");
}

else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");

else if(error == 2)
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

else if(error == 3)
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");

jaw.write(jawPos);

//Serial.print(ps2x.Analog(1), HEX);

type = ps2x.readType();
switch(type) {
case 0:
Serial.println("Unknown Controller type");
break;
case 1:
Serial.println("DualShock Controller Found");
break;
case 2:
Serial.println("GuitarHero Controller Found");
break;
}

}

void loop(){

/* You must Read Gamepad to get new values
Read GamePad and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
you should call this at least once a second
*/

if(error == 1) //skip loop if no controller found
return;

else { //DualShock Controller

ps2x.read_gamepad(false, vibrate);  //read controller and set large motor to spin at 'vibrate' speed

if(ps2x.Button(PSB_R2))    //&& jawPos<130) {  //125 is max servo position for jaw (full open)
{
jaw.write(jawPos);
jawPos=jawPos +15;              // a higher number here will make the servo/jaw move faster
// Serial.println(jawPos);
delay(5);
}

if(ps2x.Button(PSB_L2)) //&& jawPos>70){   // 70 is min servo position for jaw (full closed)
{
jaw.write(jawPos);
jawPos=jawPos -15;              // a higher number here will make the servo/jaw move faster
// Serial.println(jawPos);
delay(5);
}

if(ps2x.Button(PSB_START))                   //will be TRUE as long as button is pressed
Serial.println("Start is being held");
if(ps2x.Button(PSB_SELECT))
Serial.println("Select is being held");

LY=ps2x.Analog(PSS_LY);                              //  **************************

Serial.println(LY);
if (LY < 127) {
spd = map(LY, 126, 0, 0, 255);
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
Serial.println(spd);

if (LY > 127) {
spd = map(LY, 127, 255, 0, 255);
analogWrite(enB, spd);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
Serial.println(spd);
}

if (LY == 127) {
spd = 0;
analogWrite(enB, spd);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
Serial.println(spd);
}                                                                               //  ********************************

//digitalWrite(in3, HIGH);                      THESE 3 LINES WORK
//digitalWrite(in4, LOW);
// analogWrite(enB, 200);

if(ps2x.Button(PSB_PAD_UP)) {         //will be TRUE as long as button is pressed
Serial.print("Up held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
}
if(ps2x.Button(PSB_PAD_RIGHT)){
Serial.print("Right held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
}
if(ps2x.Button(PSB_PAD_LEFT)){
Serial.print("LEFT held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
}
if(ps2x.Button(PSB_PAD_DOWN)){
Serial.print("DOWN held this hard: ");
Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
}

vibrate = ps2x.Analog(PSAB_BLUE);        //this will set the large motor vibrate speed based on
//how hard you press the blue (X) button

if (ps2x.NewButtonState())               //will be TRUE if any button changes state (on to off, or off to on)
{

if(ps2x.Button(PSB_L3))
Serial.println("L3 pressed");
if(ps2x.Button(PSB_R3))
Serial.println("R3 pressed");
if(ps2x.Button(PSB_L2))
Serial.println("L2 pressed");
if(ps2x.Button(PSB_R2))
Serial.println("R2 pressed");
if(ps2x.Button(PSB_GREEN))
Serial.println("Triangle pressed");

}

if(ps2x.ButtonPressed(PSB_RED))             //will be TRUE if button was JUST pressed
Serial.println("Circle just pressed");

if(ps2x.ButtonReleased(PSB_PINK))             //will be TRUE if button was JUST released
Serial.println("Square just released");

if(ps2x.NewButtonState(PSB_BLUE))            //will be TRUE if button was JUST pressed OR released
Serial.println("X just changed");

// if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) // print stick values if either is TRUE
if(ps2x.Button(PSB_R1))
{
Serial.print("Stick Values:");
Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
Serial.print(",");
Serial.print(ps2x.Analog(PSS_LX), DEC);
Serial.print(",");
Serial.print(ps2x.Analog(PSS_RY), DEC);
Serial.print(",");
Serial.println(ps2x.Analog(PSS_RX), DEC);

}

}

delay(50);

};

•