Go Down

Topic: Play station controller programming... (Read 1 time) previous topic - next topic

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

Ashleysmith

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

Ashleysmith

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

Go Up