Go Down

Topic: Play station controller programming... (Read 2009 times) previous topic - next topic

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

sirbow2

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

so 50deg?

this library: http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/

look:
Code: [Select]
if(ps2x.Button(PSB_L2))
         Serial.println("L2 pressed");
        if(ps2x.Button(PSB_R2))
         Serial.println("R2 pressed");

so:

Code: [Select]
if(ps2x.Button(PSB_L2))
{
    myServo.write(50);
}
if(ps2x.Button(PSB_R2))
{
    myServo.write(0);
}
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

#2
Apr 02, 2012, 10:49 pm Last Edit: Apr 02, 2012, 10:52 pm by Ashleysmith Reason: 1
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

sirbow2

#3
Apr 02, 2012, 11:07 pm Last Edit: Apr 03, 2012, 12:41 am by sirbow2 Reason: 1
Code: [Select]
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
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

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??

sirbow2

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
Code: [Select]
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
Code: [Select]
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++;
    }
}
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

using your code i have this so far...

Quote

#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


sirbow2

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 :P
Code: [Select]
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)               
        }


Code: [Select]
#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)
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

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

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

sirbow2

Quote
i just couldn't get my head around the code

i once said the same.( and still do sometimes :P)
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

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?

sirbow2

no, that would just be better for understanding what is going on. a integer named hi2 doesnt say much about what its for :)
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar


sirbow2

i guessed what "just added in the attachment for the servos" meant... show me code
http://dduino.blogspot.com all my Arduino/electronic projects!!!

{NEW} Getting Started, Learning, Reference + FAQ PDF!!:
[url]http://ar

heres the code...

Quote

#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


Go Up