Connecting Playstation 2 controller to arduino

Control Boards, Controllers, Tethers, Ect.
Swa31s
Posts: 3
Joined: Mar 4th, 2013, 1:30 pm

Re: Connecting Playstation 2 controller to arduino

Post by Swa31s »

I bought a origenal controller, makes things easier.

But now I'm struggling with my code.

I want to control 2 esc's with my LY analog stick.
So (127,0) = LY up, control 1st esc
(128,255)=LY down, contol 2de esc

This my code

#include <PS2X_lib.h>

#include <Servo.h>

PS2X ps2x;
Servo myservo;
Servo myservo2;
int LY = 0; // Variable for fixed values based on Left Stick Y-axis
int error = 0; // used by PS2X.lib
byte type = 0; // used by PS2X.lib
byte vibrate = 0; // used by PS2X.lib

void setup()
{
Serial.begin(57600); // open serial communications.
// baudrate selected based on reccomendation for PS2X.lib


error = ps2x.config_gamepad(8,11,10,12, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
//Error checking from PS2X example code
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 http://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 http://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 http://www.billporter.info for troubleshooting tips");

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

//Serial.print(ps2x.Analog(1), HEX); //Used only to debug PS2X code.

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;
}

myservo.attach(2); //attach servo on pin 2 to the servo object
myservo2.attach(3); //attach servo on pin 3 to the servo object
}

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

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

//set current position of Left Stick Y Axis to the variable LY
LY = ps2x.Analog(PSS_LY);

//limit value of LY to the range of 0 to 255
//value should automatically be constrained by controller, but this is just incase!
constrain(LY,0,255);

if((LY >= 0) && (LY <=127)){
LY = map(LY, 127, 0, 0, 179); //this command maps or "converts" the Joystick input to ESC output range
myservo.write(LY);
}

if((LY >= 128) && (LY <=255)){
LY = map(LY, 128, 255, 0, 179); //this command maps or "converts" the Joystick input to ESC output range
myservo2.write(LY);
}


1 motor is running ok , the other doesn't arm

any suggestions?
User avatar
KR2_Diving
Posts: 391
Joined: Aug 30th, 2012, 11:43 am
Location: Currently: NW Suburbs of Chicago. Originally: NE Wisconsin

Re: Connecting Playstation 2 controller to arduino

Post by KR2_Diving »

After a quick glance at your code, nothing is jumping out at me... however, it may be that your ESC requires a specific neutral value before it will activate? For example I have to send a 96 (on scale from 0 to 180).
Vertcnc
Posts: 11
Joined: Sep 8th, 2011, 9:21 pm

Re: Connecting Playstation 2 controller to arduino

Post by Vertcnc »

It's been a little while since I worked on my setup with Xbox controller but I do remember having to adjust the programming within the ESC. Also check brake settings. A servo tester can help with testing your ESC settings.
eska-dh
Posts: 6
Joined: Jun 12th, 2011, 11:50 am

Re: Connecting Playstation 2 controller to arduino

Post by eska-dh »

I seem to recall that a servo needs a PWM pin to run. It's been a while..

Pin 2 is NOT PWM, so if that is the servo that doesn't run, use another pin that has "~" next to it. For an uno that would be 3, 5, 6, 9, 10 or 11.
Post Reply