Play station controller programming...

Has anyone used the bill porter program?

I've done the basics and it works but (and here is the but)

What I want to be able to do is,

Use the R1 and L1 to move a servo by 25-30degrees

But I want to be able to click the R1 once and it moves 25degrees and then it moves the same amount again and then use the L1 to reverse this...

As a noob I have no idea on how to do this...

Cheers in advance

it moves 25degrees and then it moves the same amount again

so 50deg?

this library: PlayStation 2 Controller Arduino Library v1.0 « The Mind of Bill Porter

look:

if(ps2x.Button(PSB_L2))
         Serial.println("L2 pressed");
        if(ps2x.Button(PSB_R2))
         Serial.println("R2 pressed");

so:

if(ps2x.Button(PSB_L2))
{
    myServo.write(50);
}
if(ps2x.Button(PSB_R2))
{
    myServo.write(0);
}

50deg but across 2 presses...

So basically if I press R1 it moves clockwise by 25deg per press and if I press L1 it goes anti-clockwise by 25deg... Hope that make sense and yes that's the library I'm using

int hi = 0;
boolean test = false;

if(ps2x.Button(PSB_L2))
{
test = true;
    if(hi == 1)
    {
        myServo.write(50);
        hi = 0;
        test = false;
    }

    if(hi == 0 && test)
    {
        myServo.write(25);
        hi++;
    }


}

sorry i was in a rush

Forgive my inexperience..

Is that last code like a button counter? Counting how many times it's been pressed??

My last question is...

Is there a way of putting limits so it can't go more than +50deg and -50deg??

yes, sort of like a button counter.

are you using a real servo(270 deg of motion) or a "fake" servo motor that has continuous rotation? if you are using a real servo, this will go to exactly 25 and 50 deg and then a similar bit of code for the other button to go back to 25 and 0 deg.

//left button (L2) code, sets 25 deg on first press, 50 on second press

int hi = 0;
boolean test = false;

if(ps2x.Button(PSB_L2))
{
test = true;
    if(hi == 1)
    {
        myServo.write(50);
        hi = 0;
        test = false;
    }

    if(hi == 0 && test)
    {
        myServo.write(25);
        hi++;
    }
}

//right button (R2) code, sets 25 deg on first press, 0 on second press

int hi2 = 0;
boolean test2 = false;

if(ps2x.Button(PSB_R2))
{
test2 = true;
    if(hi2 == 1)
    {
        myServo.write(0);
        hi2 = 0;
        test2 = false;
    }

    if(hi2 == 0 && test2)
    {
        myServo.write(25);
        hi2++;
    }
}

using your code i have this so far...

#include <PS2X_lib.h> //for v1.6
#include <Servo.h>
PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always restart your Arduino after you conect the controller

int error = 0;
byte type = 0;
byte vibrate = 0;

int val;

// Servo's
Servo steering;// create servo object to control a servo
Servo drive; // a maximum of eight servo objects can be created
Servo gear;

void setup(){
Serial.begin(57600);

//Set pin num
steering.attach(30); // attaches the servo on pin 30 to the servo object
drive.attach(34); //attaches the servo on pin 32 to the servo object

// #### SET THESE PINS ####
error = ps2x.config_gamepad(38,42,40,44, true, true); //setup pins and settings: GamePad(clock(38), command(42), attention(40), data(44), Pressures?, Rumble?) check for error

if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit 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 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);

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

// error = 0; // Skips error checking for controller
// type = 1;

} // END OF SET UP

void loop(){

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

else { //DualShock Controller

ps2x.read_gamepad(); // Needs to be called at least once a second

{ // Steering
val = (ps2x.Analog(PSS_LX)); // val = Left stick X (0-255)
val = map(val, 0, 225, 45, 135); // Convert Left X(0-255) to Servo angle(135-45)
steering.write(val); // write angle to servo
delay(15); // wait for servo to get there ( .0015 of a second)
}

{ //drive
val = (ps2x.Analog(PSS_RY)); // val = Left stick X (0-255)
val = map(val, 0, 225, 135, 45); // Convert Left X(0-255) to Servo angle(135-45)
drive.write(val); // write angle to servo
delay(15); // wait for servo to get there ( .0015 of a second)
}

int hi = 0;
boolean test = false;

if(ps2x.Button(PSB_L2))
{
test = true;
if(hi == 1)
{
gear.write(50);
hi = 0;
test = false;
}

if(hi == 0 && test)
{
gear.write(25);
hi++;
}
}

int hi2 = 0;
boolean test2 = false;

if(ps2x.Button(PSB_R2))
{
test2 = true;
if(hi2 == 1)
{
myServo.write(0);
hi2 = 0;
test2 = false;
}

if(hi2 == 0 && test2)
{
myServo.write(25);
hi2++;
}
}

delay(50);
}
} // END OF LOOP

hi, hi2, test and test2 were just quick place holders. they should be renamed and placed at the top of the code. where they are now, they initialize in each loop, and thus reset back to their default values each loop(so you cant count :P)

what is this: i think you have some parenthesis issues :stuck_out_tongue:

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

 else { //DualShock Controller
     
       ps2x.read_gamepad();   // Needs to be called at least once a second
 
  
        {    // Steering  
       val = (ps2x.Analog(PSS_LX));  // val = Left stick X (0-255)
       val = map(val, 0, 225, 45, 135); //  Convert Left X(0-255) to Servo angle(135-45)
       steering.write(val); // write angle to servo
       delay(15);  // wait for servo to get there ( .0015 of a second)
        }
        
       {    //drive 
       val = (ps2x.Analog(PSS_RY));  // val = Left stick X (0-255)
       val = map(val, 0, 225, 135, 45); //  Convert Left X(0-255) to Servo angle(135-45)
       drive.write(val); // write angle to servo
       delay(15);  // wait for servo to get there ( .0015 of a second)               
        }
#include <PS2X_lib.h>  //for v1.6
#include <Servo.h>
PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning 
//you must always restart your Arduino after you conect the controller 

int error = 0; 
byte type = 0;
byte vibrate = 0;

int val;

// Servo's
Servo steering;// create servo object to control a servo 
Servo drive;                // a maximum of eight servo objects can be created 
Servo gear;

int hi = 0; //moved here.....
boolean test = false;
int hi2 = 0;
boolean test2 = false;

void setup(){
  Serial.begin(57600);
  
  //Set pin num
  steering.attach(30);  // attaches the servo on pin 30 to the servo object 
  drive.attach(34);    //attaches the servo on pin 32 to the servo object


  // ####  SET THESE PINS  ####
  error = ps2x.config_gamepad(38,42,40,44, true, true);   //setup pins and settings:  GamePad(clock(38), command(42), attention(40), data(44), Pressures?, Rumble?) check for error

  if(error == 0){
    Serial.println("Found Controller, configured successful");
  }
  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit 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 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);

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

  //  error = 0;  //  Skips error checking for controller     
  //  type = 1;


}  //  END OF SET UP

void loop(){  

  if(error == 1) //skip loop if no controller found
  {
    return; 
  }
  else //DualShock Controller
  { 
//fixed parenthesis here
    ps2x.read_gamepad();   // Needs to be called at least once a second
    // Steering  
    val = (ps2x.Analog(PSS_LX));  // val = Left stick X (0-255)
    val = map(val, 0, 225, 45, 135); //  Convert Left X(0-255) to Servo angle(135-45)
    steering.write(val); // write angle to servo
//no wait

    //drive 
    val = (ps2x.Analog(PSS_RY));  // val = Left stick X (0-255)
    val = map(val, 0, 225, 135, 45); //  Convert Left X(0-255) to Servo angle(135-45)
    drive.write(val); // write angle to servo
//no wait

    if(ps2x.Button(PSB_L2))
    {
      test = true;
      if(hi == 1)
      {
        gear.write(50);
        hi = 0;
        test = false;
      }

      if(hi == 0 && test)
      {
        gear.write(25);
        hi++;
      }
    }

    if(ps2x.Button(PSB_R2))
    {
      test2 = true;
      if(hi2 == 1)
      {
        myServo.write(0);
        hi2 = 0;
        test2 = false;
      }

      if(hi2 == 0 && test2)
      {
        myServo.write(25);
        hi2++;
      }
    }

    delay(50); //just wait here, not after setting each servo.
  }   
}  // END OF LOOP

also, you dont need those delay 15ms(well you do, but you are already waiting 50ms at the end)

yea, the bit before was fine with just the use of the analog sticks.. :stuck_out_tongue:

i just couldn't get my head around the code, building the circuit board is fine, but i had to get a friend to help me with a bit of the code :S

i just couldn't get my head around the code

i once said the same.( and still do sometimes :P)

just added in the attachment for the servos... so would i need to change the test1/2 and hi1/2?? or can i run it as that for now?

no, that would just be better for understanding what is going on. a integer named hi2 doesnt say much about what its for :slight_smile:

now i'm lost again :frowning:

i guessed what "just added in the attachment for the servos" meant... show me code

heres the code...

#include <PS2X_lib.h> //for v1.6
#include <Servo.h>
PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always restart your Arduino after you conect the controller

int error = 0;
byte type = 0;
byte vibrate = 0;

int val;

// Servo's
Servo steering;// create servo object to control a servo
Servo drive; // a maximum of eight servo objects can be created
Servo gear;

int hi = 0; //moved here.....
boolean test = false;
int hi2 = 0;
boolean test2 = false;

void setup(){
Serial.begin(57600);

//Set pin num
steering.attach(30); // attaches the servo on pin 30 to the servo object
drive.attach(34); //attaches the servo on pin 32 to the servo object
gear.attach (32); // attaches the servo on pin 32

// #### SET THESE PINS ####
error = ps2x.config_gamepad(38,42,40,44, true, true); //setup pins and settings: GamePad(clock(38), command(42), attention(40), data(44), Pressures?, Rumble?) check for error

if(error == 0){
Serial.println("Found Controller, configured successful");
}
else if(error == 1)
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit 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 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);

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

// error = 0; // Skips error checking for controller
// type = 1;

} // END OF SET UP

void loop(){

if(error == 1) //skip loop if no controller found
{
return;
}
else //DualShock Controller
{
//fixed parenthesis here
ps2x.read_gamepad(); // Needs to be called at least once a second
// Steering
val = (ps2x.Analog(PSS_LX)); // val = Left stick X (0-255)
val = map(val, 0, 225, 45, 135); // Convert Left X(0-255) to Servo angle(135-45)
steering.write(val); // write angle to servo
//no wait

//drive
val = (ps2x.Analog(PSS_RY)); // val = Left stick X (0-255)
val = map(val, 0, 225, 135, 45); // Convert Left X(0-255) to Servo angle(135-45)
drive.write(val); // write angle to servo
//no wait

if(ps2x.Button(PSB_L2))
{
test = true;
if(hi == 1)
{
gear.write(25);
hi = 0;
test = false;
}

if(hi == 0 && test)
{
gear.write(25);
hi++;
}
}

if(ps2x.Button(PSB_R2))
{
test2 = true;
if(hi2 == 1)
{
gear.write(25);
hi2 = 0;
test2 = false;
}

if(hi2 == 0 && test2)
{
gear.write(25);
hi2++;
}
}

delay(50); //just wait here, not after setting each servo.
}
} // END OF LOOP

nah its good. i was saying before; you dont have to change the name from 'hi' or 'hi2' to something else, but if you changed 'hi' to 'L2Count', it would make more sense of what that value, 'hi', is used for.

I tried that code but it doesn't register the l1 or r1 buttons :frowning:

try some debugging to see if the arduino is even detecting the controller presses. put a Serial.println("some message"); inside the if(ps2x.Button(PSB_R2)) and if(ps2x.Button(PSB_L2)) statements.

 if(ps2x.Button(PSB_L2))
    {
      Serial.println("some message"); //like this

      test = true;
      if(hi == 1)
      {
        gear.write(25);
        hi = 0;
        test = false;
      }

      if(hi == 0 && test)
      {
        gear.write(25);
        hi++;
      }
    }

this has to work. the hi integer is declared as 0, and the test boolean is set to true at the beginning of the if statement, so:

if(hi == 0 && test)
      {
        gear.write(25);
        hi++;
      }

MUST run on the first button press.