Show Posts
Pages: [1] 2 3 ... 5
1  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 23, 2010, 02:30:06 pm
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 :/


Code:
#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);
}
2  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 18, 2010, 03:16:53 am
I optomized  PSButton() -

It is now:
Code:
   uint16_t buttons = *(uint16_t*)(PS2data+3);
    return (~buttons & button > 0);
instead of
Code:
int byte = 3;
if (button >= 0x100) {
  byte = 4;
  button = button >> 8;
}
  if (~PS2data[byte] & button)
  return true;
  else
  return false;

Code:
#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);
}

A well made guide
3  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 16, 2010, 11:25:14 pm
Nah, It is not a big deal, I THink it should work. Ill post the new code.
4  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 15, 2010, 10:13:56 am
Ok, So should I make this code into an Update PSx lib or what?
5  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 14, 2010, 05:59:59 pm
Sounds like a problem with the enviorment
What are the compiler errors?
6  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 12, 2010, 10:56:53 am
Oh wow, I had no idea this code has been usefull to so many people smiley
I never enabled notifing to replies on this topic!

I think I should actually finish the librarry then seing it still work, which it should because I wrote it without using much of of the arduino librarry.

Oh boy, this code could use some improvments smiley-lol


OK, lets look at the At the Arduino Mega Schematic


AH there is your problem :3

Code:
/* These are AVR PORTB pins, +8 to convert to Arduino pins */
#define PS2clk 5
#define PS2cmd 3    
#define PS2att 2    
#define PS2dat 4
#define PS2PORT PORTB
#define PS2IN PINB
#define CTRL_CLK 20
#define CTRL_BYTE_DELAY 20

PORTB2 and PORTB3 are not hooked up to pins that you can use...Easily

the pins we are going to to be using are these here:


Ill be right back to help after I get the snow off my trees.  ;D

Also, I think I will improve this code to make it easier for people to use, and i will add lots of coments.
I am snowed in the house so it will give me something to work on smiley

Sorry I didn't see all of these replies!

Update:
I finished re-writting the code a bit, I need to test it out and see how well it works smiley
I have an arduino Decimilla and Mega and I will see how it preforms on both.
7  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: March 11, 2008, 10:41:56 am
Ok, so I have been working on this and improving it. So, it looks like there is a small problem. it is suppose to be that you can hook up the PSX controllers in a bus. when i have the rx line hooked up to another rx line it gets grounded D:
It is suppose to be that I select which controller to talk to using the select line and i can use the same pins for everything else.
Any ideas why this is not working?

I will make an official Lib for arduino soon ^-^
8  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 28, 2008, 07:37:42 pm
YES! IT WORKS!
Code:
#include <math.h>
#include <stdio.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 */
#define PS2clk 5
#define PS2cmd 3
#define PS2att 2
#define PS2dat 4
#define PS2PORT PORTB
#define PS2IN PINB
#define CTRL_CLK 20
#define CTRL_BYTE_DELAY 20

//These are our button constants
#define PSB_SELECT 0x01
#define PSB_L3 0x02
#define PSB_R3 0x04
#define PSB_START 0x08
#define PSB_PAD_UP 0x10
#define PSB_PAD_RIGHT 0x20
#define PSB_PAD_DOWN 0x40
#define PSB_PAD_LEFT 0x80

#define PSB_L2 0x100
#define PSB_R2 0x200
#define PSB_L1 0x400
#define PSB_R1 0x800
#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();
unsigned char PS2data[9];
void read_gamepad();
void config_gampad();
unsigned char get_gamepad_mode();
unsigned char i;

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

    pinMode(PS2clk+8,OUTPUT);
    pinMode(PS2att+8,OUTPUT);
    pinMode(PS2cmd+8,OUTPUT);
    pinMode(PS2dat+8,INPUT);
    digitalWrite(PS2dat+8,HIGH);

    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(unsigned int button) {
int byte = 3;
if (button >= 0x100) {
  byte = 4;
  button = button >> 8;
}
  if (~PS2data[byte] & button)
  return true;
  else
  return false;
}
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);
    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);
}
9  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 28, 2008, 07:50:58 am
Well. I am going to convert it to an analog joystick for use in the First robotics Competition. Also on that note i need to sooth out pwm signals >.<
i am using ps1 controller and have been going to a friends house using there oscilloscope
.

code sent from me from someone who has gotten it to work with the arduino, have yet to test it out.
...i will post it on my web site if it works, and reply to this smiley-kitty
"If you can;t get an answer, try harder and do it yourself D:"
10  Forum 2005-2010 (read only) / Interfacing / Re: Playstation Controller on: February 23, 2008, 07:06:15 pm
I know i am new and it helps if someone had the hardware, but i really need sombody to look at this. I want to make a converter so that i can use this controller to control a robot. I only have a week or so to do it. So any help i can get will be greatly appreciated. Does anybody have any suggestions or have any ideas why my code isn't working?
11  Forum 2005-2010 (read only) / Interfacing / Playstation Controller on: February 23, 2008, 12:32:59 am
Ok, wow, I have been trying to talk to a playstation controller using arduino. I am a novice so i think it is a tad hard for me.

first of the clock speed of 16Mhz is way to fast, so I added a 1ms delay between high and low of clock to get a 500hz clock.

the fallowing code should work, But It doesn't!

Code:
int clock = 2;
int ATT = 3;
int RX = 4;
int TX = 5;
int ACK = 6;
char TMP;
void setup () {
 Serial.begin(19200);
 pinMode(clock,OUTPUT);
 pinMode(ATT,OUTPUT);
 pinMode(RX,INPUT);
 pinMode(TX,OUTPUT);
 pinMode(ACK,INPUT);
}

unsigned char psx_data(unsigned char out_byte); /*  This is an example of a forward declaration or "prototype" */
void lcdDisplay(unsigned char); /* Also note your parameters don't have to match names... you only need the types and commas, FYI the moar yuo know */

void loop() {
        unsigned char data_1;
        unsigned char data_2;
      digitalWrite(ATT, LOW); // lower ATT
      
      psx_data(0x01); // send a start command to the psx controller
      unsigned char type = psx_data(0x42); // send a data request to psx controller + get controller type

      psx_data(0x00); // get 'here comes data' code

      data_1 = psx_data(0x00); // get controller data byte 1
      data_2 = psx_data(0x00); // get controller data byte 2

      digitalWrite(ATT, HIGH); // raise ATT
                                                            
        lcdDisplay(type);
}

unsigned char psx_data(unsigned char out_byte)
{
      unsigned char in_byte; // data received from PSX controller
      unsigned char mask_byte;
      unsigned char bit_counter;
                            
      mask_byte = 1;
      in_byte = 0;
      
      // for each bit in the transmitted byte
      for (bit_counter = 0; bit_counter < 8; bit_counter++)
      {            
            // CLK low
            digitalWrite(clock,LOW);
            
            // set up CMD line (data out to controller)
            if (out_byte & mask_byte) digitalWrite(TX,HIGH);
            else digitalWrite(TX,LOW);
                        
            // shift the mask bit for the next comparison
            mask_byte <<= 1;
            
                delay(1);
            // CLK high
            digitalWrite(TX,HIGH);
            
            // load the current controller data bit on the bus into our return byte
            if (digitalRead(RX) == 1) in_byte |= 0x80;
            // and right-shift to line up the next bit (last bit has final value, no need to shift)
            if (bit_counter < 7) in_byte >>= 1;
                delay(1);
      }              
      return in_byte;
}

//talks to my matrix orbital LCD display
void lcdDisplay (unsigned char x) {
       Serial.print(254,BYTE);
       Serial.print("X");
       Serial.print("Sample: ");
       Serial.print(x, DEC);
}

Some sites i have been using fro help
source code i based it off of
http://www.lynxmotion.com/images/files/atmel01.c

more stuff
http://www.geocities.com/digitan000/Hardware/22/e22_page.html?200822

playstation data protocol and pinouts
http://pinouts.ru/Game/playstation_9_pinout.shtml

Thanks smiley-grin
12  Forum 2005-2010 (read only) / Interfacing / ATmega16U4 ATmega32U4 USB HID on: May 17, 2009, 07:19:22 pm
Hey Guys!
 I wan't to use the ATmega16U4 ATmega32U4 with the arduino boot loader, anyone done this?
I made a large 32 input multiplexed HID device board for it. These chips have a handy USB interface port.
I noticed that in the avr dude includes there is not an io include for these chips...
13  Forum 2005-2010 (read only) / Interfacing / Re: Unipolar stepper motor turning problem on: February 12, 2010, 01:57:44 pm
it sounds like a step pin is not being held long enough and does not have enough time to complete the step. So the motor begin to move but then returns to its inital state.
14  Forum 2005-2010 (read only) / Interfacing / Re: Simulating VGA Signal on: May 17, 2009, 09:24:01 pm
Here you are!
http://www.linusakesson.net/scene/craft/

I am the one who posted the eagle schematics in the reply  :3
15  Forum 2005-2010 (read only) / Interfacing / Re: Interfacing joystick for wheelchair controller on: February 12, 2010, 01:00:27 pm
THe center tap is the referance for half the voltage in, IE: Nutral.

To simulate this all you need to do is use the PWM out, But you need to modify the PWM out to be higher frequincy, to do that is straight foward.

Use PWM pins 5 and 6;
TCCR0B = TCCR0B & 0b11111000 | 0x01;

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

Using this is good enough to fool my sound card into thinking it is an analog voltage output.
If it is not, Add an RC Low pass filter circut to smooth out the pwm.
Pages: [1] 2 3 ... 5