amurison718
Published © GPL3+

Atari PS3 controller

Dump that retro joystick and join the 21st Century.

BeginnerFull instructions provided4,335
Atari PS3 controller

Things used in this project

Story

Read more

Code

Atari_PS3_joystick.ino

C/C++
// USB_Host_Shield_2.0 - Version: Latest 
#include <PS3USB.h>

// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>

USB Usb;
/* You can create the instance of the class in two ways */
PS3USB PS3(&Usb); // This will just create the instance
//PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch

bool printAngle;
uint8_t state = 0;

int firepin = 3;
int uppin = 4;
int downpin = 5;
int leftpin = 6;
int rightpin = 7;

void setup() {
   Serial.begin(9600);
#if !defined(__MIPSEL__)
  while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
  if (Usb.Init() == -1) {
    Serial.print(F("\r\nOSC did not start"));
    while (1); //halt
  }
  Serial.print(F("\r\nPS3 USB Library Started"));

  pinMode(firepin, OUTPUT);
  pinMode(uppin, OUTPUT);
  pinMode(downpin, OUTPUT);
  pinMode(leftpin, OUTPUT);
  pinMode(rightpin, OUTPUT);
}   

void reset ()
{
// reset pins
  digitalWrite(firepin, HIGH);
  digitalWrite(uppin, HIGH);
  digitalWrite(downpin, HIGH);
  digitalWrite(leftpin, HIGH);
  digitalWrite(rightpin, HIGH);
  
}

void loop() 
{
  Usb.Task();

  if (PS3.PS3Connected || PS3.PS3NavigationConnected) 
  {
  // Left (left stick)
  if (PS3.getAnalogHat(LeftHatX) < 80 )
    {
      Serial.print(F("\r\nLeftHatX: Left "));
      digitalWrite(leftpin, LOW);
      }
      
  // Right (left stick)
  if (PS3.getAnalogHat(LeftHatX) > 175) 
{
      Serial.print(F("\r\nLeftHatX: Right"));
      digitalWrite(rightpin, LOW);
}

// Down (left stick)
  if   (PS3.getAnalogHat(LeftHatY) > 175)
  {
      Serial.print(F("\r\nLeftHatY: Down"));
      digitalWrite(downpin, LOW);    
  }
// Up (left stick)
  if( PS3.getAnalogHat(LeftHatY) < 80)
    {
      Serial.print(F("\r\nLeftHatY: Up"));
      digitalWrite(uppin, LOW);       
  }

// right (right stick)  
 if (PS3.getAnalogHat(RightHatX) > 175)
 {
  // enter code here
 }

// left (right stick)
  if (PS3.getAnalogHat(RightHatX) < 80)
  {
    //enter code here
  }

// down (right stick)
  if (PS3.getAnalogHat(RightHatY) > 175)
  {
    // enter code here
  }
// up (right stick)
  if ( PS3.getAnalogHat(RightHatY) < 80) 
{
  // enter code here  
}
  

  // Analog button values can be read from almost all buttons
    if (PS3.getAnalogButton(L2) )
    {
      Serial.print(F("\r\nL2: "));
      digitalWrite(firepin, LOW);
    }
              
    if (PS3.getAnalogButton(R2)) 
    {
      Serial.print(F("\r\R2: "));
      digitalWrite(firepin, LOW);

      }

    delay(15);
    reset();
    
  }
}

void notused()
{
  /*
    if (PS3.getButtonClick(PS))
      Serial.print(F("\r\nPS"));

    if (PS3.getButtonClick(TRIANGLE))
      Serial.print(F("\r\nTriangle"));
    if (PS3.getButtonClick(CIRCLE))
      Serial.print(F("\r\nCircle"));
    if (PS3.getButtonClick(CROSS))
      Serial.print(F("\r\nCross"));
    if (PS3.getButtonClick(SQUARE))
      Serial.print(F("\r\nSquare"));

    if (PS3.getButtonClick(UP)) {
      Serial.print(F("\r\nUp"));
      PS3.setLedOff();
      PS3.setLedOn(LED4);
    }
    if (PS3.getButtonClick(RIGHT)) {
      Serial.print(F("\r\nRight"));
      PS3.setLedOff();
      PS3.setLedOn(LED1);
    }
    if (PS3.getButtonClick(DOWN)) {
      Serial.print(F("\r\nDown"));
      PS3.setLedOff();
      PS3.setLedOn(LED2);
    }
    if (PS3.getButtonClick(LEFT)) {
      Serial.print(F("\r\nLeft"));
      PS3.setLedOff();
      PS3.setLedOn(LED3);
    }

    if (PS3.getButtonClick(L1))
      Serial.print(F("\r\nL1"));
    if (PS3.getButtonClick(L3))
      Serial.print(F("\r\nL3"));
    if (PS3.getButtonClick(R1))
      Serial.print(F("\r\nR1"));
    if (PS3.getButtonClick(R3))
      Serial.print(F("\r\nR3"));

    if (PS3.getButtonClick(SELECT)) {
      Serial.print(F("\r\nSelect - "));
      PS3.printStatusString();
    }
    if (PS3.getButtonClick(START)) {
      Serial.print(F("\r\nStart"));
      printAngle = !printAngle;
    }
    if (printAngle) {
      Serial.print(F("\r\nPitch: "));
      Serial.print(PS3.getAngle(Pitch));
      Serial.print(F("\tRoll: "));
      Serial.print(PS3.getAngle(Roll));
    }
  }
  else if (PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB
    if (state == 0) {
      PS3.moveSetRumble(0);
      PS3.moveSetBulb(Off);
    } else if (state == 1) {
      PS3.moveSetRumble(75);
      PS3.moveSetBulb(Red);
    } else if (state == 2) {
      PS3.moveSetRumble(125);
      PS3.moveSetBulb(Green);
    } else if (state == 3) {
      PS3.moveSetRumble(150);
      PS3.moveSetBulb(Blue);
    } else if (state == 4) {
      PS3.moveSetRumble(175);
      PS3.moveSetBulb(Yellow);
    } else if (state == 5) {
      PS3.moveSetRumble(200);
      PS3.moveSetBulb(Lightblue);
    } else if (state == 6) {
      PS3.moveSetRumble(225);
      PS3.moveSetBulb(Purple);
    } else if (state == 7) {
      PS3.moveSetRumble(250);
      PS3.moveSetBulb(White);
    }
   
   */
}

Github

https://github.com/felis/USB_Host_Shield_2.0

Credits

amurison718
4 projects • 2 followers

Comments