Playstation Controller

Hi there -

So I'm basically working on the opposite of your project, that is, making a controller to interface with a PlayStation 2. I feel like the code you've got posted seems like it'd be able to do the job with a couple of modifications, but I don't really understand what it's doing exactly...

Really, it's these lines that I don't really understand -

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

...

#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))

I think if I knew what was going on in those lines, I could pretty easily figure out how to modify the rest of the program to emulate a controller instead of a playstation...So if anyone could help me out, it'd be a great help!

Can you elaborate a little on what you don't understand? I can try to give you a general description of what the defines are (note that I have no familiarity with the original program):

PORTB is defined in <avr/io.h>, which is automatically included in your sketches. It is the mega168 register that controls the output state of the pins on port B (Arduino digital pins 8 - 13). Setting bit n of PORTB causes the corresponding pin to drive high if the pin is set as an output, or it causes the internal pull-up to be enabled if the pin is set as an input. Clearing bit n of PORTB causes the corresponding pin to drive low if the pin is set as an output, or it causes the pin to be tri-stated (high impedance) if the pin is set as an input.

PINB is also defined in <avr/io.h>. It is the mega168 register that tells you the digital input values of the port B pins. You use this register to read digital inputs of Arduino digital pins 8 - 13.

CTRL_CLK and CTRL_BYTE_DELAY sound like they have to do with timing; I'd need to look at them in the context of the code to know what they are used for.

The remaining defines perform bit manipulations on bytes. SET(byte, bit) will set the specified bit of the specified byte to 1 while leaving the rest of the bits in the byte unchanged. CLR(byte, bit) clears the specified bit of the specified byte. CHK(byte, bit) returns 0 if the specified bit of the specified byte is 0, otherwise it returns a non-zero value. TOG(byte, bit) toggles the specified bit of the specified byte (1 -> 0 and 0 -> 1).

Useful bitwise operators to understand:

~: bitwise NOT. Example: ~1011 = 0100
|: bitwise OR. Example: 1010 | 0011 = 1011
&: bitwise AND. Example: 1010 & 0011 = 0010
<<: bitshift left. Example: 10001011 << 2 = 00101100

: bitshift right. Example: 10001011 >> 2 = 00100010
^: bitwise XOR. Example: 1010 ^ 0011 = 1001

Note that & and | are different from the boolean operators && and ||.

  • Ben

Wow, thanks Ben! That's going to help me alot I think! Mostly, I'm pretty new to messing with low-level stuff - I'm coming in from a Python/Java background (also an Economics major background), so the more EE stuff of these projects is all new stuff to me. Thanks though, and I'll try and get some controller emulation stuff done up.

I don't mean to step on your toes Shutter since you are working on a library for this already, but I had some old code used for reading a playstation controller and I threw together a library. It only works with the original digital controller, at least that is the only thing I have tested with it. More information and a download can be found on the playground.

http://www.arduino.cc/playground/Main/PSXLibrary

Hi. I'm using the psx library and my question is where I have to connect the ecknowledge pin? In the arduino webpage examples it don't explain anything about this.
I have a pololu LV-168 board and the arduino 11 because the libraries of psx don't work for 12 and obviously a Pllaystation 2 controller.
please help me and if these helps include photos or pictures better because i'm not a very experimented english speaker.
Thanks and sorry if my english is bad

I tried the PSX library posted by synlor, and could not get it to compile with arduino 017. I saw another similar result elsewhere on the forum.

I then used the code posted on Feb 29 by Shutter. It compiled free of errors without changes. I wired it up to a controller, and had the whole thing running after moving the wires up 8 to pins 10-13, and setting my baud rate to 57,600. Thanks for your post Shutter

Hi,
I´m new here, and I tried to read the data of a Playstations2 Controller with the Arduino but didint work. I tried your code and it dosen´t work. I dont know what I am doing wrong. Did you used some IC, our you just plugged the controller directly in to the arduino? Do you have some schematic to use whith this code? I will be glad for your answer.

PSX Controllers is a link to the controller pinout. According to Shutters code, the pins connect as follows. 5v goes to the 5v arduino supply. gnd goes to arduino ground. Att connects to pin 10. cmd connects to pin 11. Dat connects to pin 12. clk connects to pin 13. No Ic's are needed. Double check the baud rate. I just soldered 22 guage wire to the pins and put tape on them telling me where to wire them to arduino. It has been rock solid for me. I have hooked it to 2 windows pc's and a mac.

Good Luck,
Don

And the ACK pin? I dont need to use it?
I´m using Arduino 0017 software in a Windows PC, and I´m wiring the PS2 connector directly in to the Arduino.
Did you used the code posted of Shutter? If you dont please, can you sand me your code?
Thank you for your help.

Ack is not used.
Shutters code works just fine as posted.
I ran it using 017.
I modified it to send the data differently, but his code will get you up and running. It was just easier for me to alter the arduino code than the software I was interfacing with.

HI, I have 2 controllers,the alambric one works fine but with the wireless seems to be trying to associate the controller, I dont really have a play station, I dont know how to interprete the LEDs.

I have this device


I tried to power it with 5v and 3.3, same behavior.

any ideas?, im using the last code posted by Shutter

I would think it would be run off of 5v. That is what mine used, and it would connect into the same ps2 port, so I don't know how it could be anything else.

Look at the psx pin out I linked to and the pin number I posted as well.

Good luck.

HI i'm using the psx and psx libraries from arduino webpage and apear this error:

arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:110: error: expected unqualified-id before 'int'

arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:110: error: expected `)' before 'int'

arduino-0017/hardware/tools/avr/lib/gcc/../../avr/include/stdlib.h:110: error: expected `)' before 'int'

I don't now what happens and how to solve because if i modify this document will have repercusions in the arduino uses or what? I need help!
This or a code that rolls without libraries.

Thanks and sorry if my english is bad or very bad isn't my 1st lenguage

has anyone tried using the PS3 controller with the arduino mega, essientially I am trying to control a RC Car with a PS3 controller, and Im using Arduino Mega.

hey guys..
i, super new at this and i'm uploading shutter's program..
it compiles fine but it doesn't seem to work..
the readout just goes nuts and displays a bunch of commands really fast..
im using a ps2 controller if that matters....
help pleaaase

im using the arduino mega if that makes a difference

If you are getting fast comm, but it is legible, it is updates on a regular basis. Probably just too fast to read.

If it is gibberish, play with your comm settings they have to match between the arduino and the computer.

I hope that helps.

Don803

thnks!!
i'm guessing its going to fast.. how do i change this?

the controller pins are inserted in the analog correct??

some more info..

im not touching the controller and i get these fast readouts..

any ideas on how to fix it??