Playstation Controller

IMPORTANT, LAST CODE HAD BUGS IN IT!

In PSButton:
return (~buttons & button > 0); -> return ((~buttons & button) > 0);

Tested on:
Arduino Decimila (should work for any atmega168 or atmega 8 board if pinouts are the same!)
Arduino MEGA (VERY BUGGY! Needs to be looked at under oscilloscope)

On the mega when you click the analog buttons all buttons come trough clearly, except the fact select is always pressed. Select happens to be a signed bit if the button intiger ws signed, but it is unsigned :confused:

#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>

#define LED_PIN 13
#define DELAY(wait) digitalWrite(LED_PIN,LOW); delay(wait); digitalWrite(LED_PIN,HIGH);

/* These are AVR PORTB pins, +8 to convert to Arduino pins */

#if defined(__AVR_ATmega1280__)
    //PortB pin mapping on arduino Mega
    #pragma message("Arduino Mega")
    #define PS2clk 7  //13
    #define PS2cmd 5  //11
    #define PS2att 4  //10
    #define PS2dat 6  //12
#else
    #pragma message("Arduino")
    //PortB mapping on arduino168/8 bassed boards
    #define PS2clk 5  //13
    #define PS2cmd 3  //11
    #define PS2att 2  //10
    #define PS2dat 4  //12
#endif

#define PS2PORT         PORTB
#define PS2IN           PINB
#define PS2DDR          DDRB
#define CTRL_CLK        20
#define CTRL_BYTE_DELAY 20

//These are our button constants
#define PSB_SELECT      0x0001
#define PSB_L3          0x0002
#define PSB_R3          0x0004
#define PSB_START       0x0008
#define PSB_PAD_UP      0x0010
#define PSB_PAD_RIGHT   0x0020
#define PSB_PAD_DOWN    0x0040
#define PSB_PAD_LEFT    0x0080
#define PSB_L2          0x0100
#define PSB_R2          0x0200
#define PSB_L1          0x0400
#define PSB_R1          0x0800
#define PSB_GREEN       0x1000
#define PSB_RED         0x2000
#define PSB_BLUE        0x4000
#define PSB_PINK        0x8000

#define SET(x,y) (x|=(1<<y))
#define CLR(x,y) (x&=(~(1<<y)))
#define CHK(x,y) (x & (1<<y))
#define TOG(x,y) (x^=(1<<y))

boolean PSButton(uint16_t button);
unsigned char PS2data[9];
void read_gamepad();
void config_gampad();
unsigned char get_gamepad_mode();
unsigned char i;

void setup() {
    Serial.begin(57600);
    pinMode(LED_PIN,OUTPUT);
    digitalWrite(LED_PIN,HIGH);

    //similar to pinmod(pin,OUTPUT)
    //Setting up the pin to be output
    SET(PS2DDR,PS2clk);
    SET(PS2DDR,PS2att);
    SET(PS2DDR,PS2cmd);
    //Sets pin to Input mode
    CLR(PS2DDR,PS2dat);

    SET(PS2PORT,PS2dat);

    config_gampad();
}

void loop () {
    read_gamepad();
    if(PSButton(PSB_SELECT))
     Serial.print("Select\n");
    if(PSButton(PSB_L3))
     Serial.print("L3\n");
    if(PSButton(PSB_R3))
     Serial.print("R3\n");
    if(PSButton(PSB_START))
     Serial.print("Start\n");
    if(PSButton(PSB_PAD_UP))
     Serial.print("Up\n");
    if(PSButton(PSB_PAD_RIGHT))
     Serial.print("Right\n");
    if(PSButton(PSB_PAD_LEFT))
     Serial.print("LEFT\n");
    if(PSButton(PSB_PAD_DOWN))
     Serial.print("DOWN\n");
    if(PSButton(PSB_L1))
     Serial.print("L1\n");
    if(PSButton(PSB_R1))
     Serial.print("R1\n");
    if(PSButton(PSB_L2))
     Serial.print("L2\n");
    if(PSButton(PSB_R2))
     Serial.print("R2\n");
    if(PSButton(PSB_GREEN))
     Serial.print("Triangle\n");
    if(PSButton(PSB_RED))
     Serial.print("Circle\n");
    if(PSButton(PSB_PINK))
     Serial.print("Square\n");
    if(PSButton(PSB_BLUE))
     Serial.print("X\n");
}

boolean PSButton(uint16_t button) {
    uint16_t buttons = *(uint16_t*)(PS2data+3);
    return ((~buttons & button) > 0);
}

unsigned char _gamepad_shiftinout (char byte) {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
      if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
        else  CLR(PS2PORT,PS2cmd);
      CLR(PS2PORT,PS2clk);
      delayMicroseconds(CTRL_CLK);
        if(CHK(PS2IN,PS2dat)) SET(tmp,i);
      SET(PS2PORT,PS2clk);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void _gamepad_shiftout (char byte) {
   for(i=0;i<8;i++) {
      if(CHK(byte,i)) SET(PS2PORT,PS2cmd);
        else  CLR(PS2PORT,PS2cmd);
      CLR(PS2PORT,PS2clk);
      delayMicroseconds(CTRL_CLK);
      SET(PS2PORT,PS2clk);
      //delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
}
unsigned char _gamepad_shiftin() {
   unsigned char tmp = 0;
   for(i=0;i<8;i++) {
      CLR(PS2PORT,PS2cmd);
      CLR(PS2PORT,PS2clk);
      delayMicroseconds(CTRL_CLK);
        if(CHK(PS2IN,PS2dat)) SET(tmp,i);
      SET(PS2PORT,PS2clk);
      delayMicroseconds(CTRL_CLK);
   }
   SET(PS2PORT,PS2cmd);
   delayMicroseconds(CTRL_BYTE_DELAY);
   return tmp;
}
void read_gamepad() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    delayMicroseconds(CTRL_BYTE_DELAY);
    //Send the command to send button and joystick data;
    char dword[9] = {0x01,0x42,0,0,0,0,0,0,0};
    for (int i = 0; i<9; i++) {
        PS2data[i] = _gamepad_shiftinout(dword[i]);
    }
    SET(PS2PORT,PS2att); // HI disable joystick
}
unsigned char get_gamepad_mode() {
    SET(PS2PORT,PS2cmd);
    SET(PS2PORT,PS2clk);
    CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    unsigned char x = _gamepad_shiftin();
    SET(PS2PORT,PS2att); // HI disable joystick
    return x;
}
void config_gampad() {
   SET(PS2PORT,PS2cmd);
   SET(PS2PORT,PS2clk);
   CLR(PS2PORT,PS2att); // low enable joystick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x00);
    // Lock to Analog Mode on Stick
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x44);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    // Vibration
    /*
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4D);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    */
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x4F);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0xFF);
    _gamepad_shiftout(0x03);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x01);
    _gamepad_shiftout(0x43);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x00);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
    _gamepad_shiftout(0x5A);
   SET(PS2PORT,PS2att);
}

great.. yo are like and arduino angel!! ;D going to test it later tonight!
so to sum up
i should just copy and paste the code exactly as is

then:
the clk is in pin 13 in PWN
the data is in pin 12 in PWN
the cmd is in pin 11 in PWN
the clk is in pin 10 in PWN
the pwr is in pin 5+ straight from the arduino
the grd is in the grd next to 5+

does the arduinomega need any more external power besides the usb?
exactly what brand controller are you using?
ahh.. also does this code read the thumbsticks?

thank you soo soo much

anyone tested this on the Mega yet???

Plus one waiting for a mega update, because if this works im going to give up on the Wii Nunchuck on the mega

Ok, so shutter's code compiles fine on Arduino 018. Now how can I integrate this to my sketch to control my robot? I mean how would I structure my 'if...else' statements to call my movement functions based on the analog sticks' input?

UPDATE: I've upload to my Freeduino w/328P board, no problem. Hooked up power, ground, and the following.
#define PS2clk 13 //BLUE
#define PS2cmd 11 //ORG
#define PS2att 10 //YELLOW
#define PS2dat 12 //BROWN
When I open the terminal, I get a very fast constant stream of various random button presses.
Oh yeah, I'm using a Lynxmotion ps2 wireless controller which does sync immediately.

Another update.

I connected a wired Sony Dualshock 2. Same continuous stream of button presses printed to serial terminal. Even when power and ground disconnected! :o

Oh and the button presses are not random, they are in order of 'if.. then' tests, 'SELECT' through 'X'.

you ur terminal thing is at a different baud then the board, in the setup function, there is a Serial.begin(some number); the monitor has to be the same number thing.......

No thats not it. Serial begin is 57600 and so is terminal. Anything else would just read gibberish. And I am not pushing any buttons, so why the output?

Hey. I just tryed this out and was amazed. :stuck_out_tongue: i just wonder, is there any outputs. So i can use the PS control for something. In the best case like a 16 bit serial so i can convert that to parallell (with a multiplexer) and use it for whatever i want?

Sorry for bad English. Im from Sweden you know :wink:

Anyway. Thanks for everything. Hope you can help me out :smiley:

ow. Nice i got this to work :stuck_out_tongue:

void loop () {
read_gamepad();
if(PSButton(PSB_SELECT)){
Serial.print("Select\n");
int hejhej = 7;
pinMode(hejhej,OUTPUT);
digitalWrite(hejhej,HIGH);
delay(5);
digitalWrite(hejhej,LOW);
}

i will continue to try this out more.. hopefully i will learn my way and get my 16 bit serial multiplex cutcutpewpew :smiley:

I too seem to get a similar result to mannyr7 using a PS1 controller.

I get a stream of button presses without actually pressing anything.

could someone tell me if this to be expected or am i doing something wrong. I've been trying to get this to work for ages

thanks

In my situation, I'm thinking its the decode byte. But I have no idea what to replace it with. And PS1 controllers are half the clock of the PS2, if I remember correctly.

Can someone post some pictures as how they hooked theirs up to their arduino?

Yes, can someone post pictures of it being wired up; hopefully to the mega.

I have tried so much code (from many sources) over at least a month and nothing works. I have to edit the code to even get it to compile, even then it still does not work.

Please, those of you that have had success with hooking up a Playstation 2 gamepad to their Arduino (I have a non-mega), please post your electronics hookups and full code as it would really help the rest of us out that are clearly missing something.

Thanks!

i've european ps2 and my controller's hw board is different!
in many different sites this controller has different colors..
this is mine with color cables

http://img229.imageshack.us/img229/364/teorico.jpg

and this is other websites with different colors

http://letsmakerobots.com/node/18599

http://www.lynxmotion.com/images/html/build036.htm

i have arduino2009

i want to be able to use analog sticks to pilot servos and other things..
and i'm confused 'cause this color's difference

M

YESSSS!!!!

Shutter I could kiss you. Weeks trying to get other PS2 controller libraries working and your last code post works like a charm on my Arduino Pro mini.

But it looks like there it doesn't support the analog sticks yet? I need those as I am build a controller for my ground vehicle.

I really would like those analog sticks to work, and I'm willing to make a donation to you for the work finishing you library. If you beat me(I'm gonna try in the mean time) at adding analog stick values to your code, i'll give you a $20 donation. If you don't believe me, PM me a PayPal email and I'll send you $10 for getting me this far already, and another $10 when you add the stick values.

Well, I seemed to have devised my own addition that reads stick values.

In header:

//These are stick values
#define PSS_RX 5
#define PSS_RY 6
#define PSS_LX 7
#define PSS_LY 8


Then as a function.

byte PSStick(unsigned int stick) {
  return PS2data[stick];
}

So PSStick(PSS_LY) returns the value of the left sticks Y axis.

Offer for donation still stands if you put a little work into making this a library, commenting the code on how it works, and/or come up with a better method of reading the sticks then I.

Well, through need I went ahead and did my best to make Shutter's code into a library with analog stick support. (Shutter, hope you don't mind, I still want to donate to you if you PM me a paypal address)

The library, example sketch, and details are all available on my website at:

Please post comments / bugs there if you are using my lib version of Shutter's code.

I've started going full speed on making my version of the library more powerful. Here's a taste:

psx.Button(this_button) is TRUE if this_button is currently pressed
psx.NewButtonState() will return TRUE if any button changed state
psx.NewButtonState(this_button) will return TRUE if this_button changed state
psx.ButtonPressed(this_button) will return TRUE if this_button was just pressed
psx.ButtonReleased(this_button) will return TRUE if this_button was just released

I think that this should cover all possible uses for a button.

I'm not going to keep updates on this forum, so be sure to check my site for updates with bug fixes and more features. Up next on my list is to enable rumble and read the button pressures.