Offline
Newbie
Karma: 0
Posts: 21
|
 |
« on: April 02, 2012, 03:19:41 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #1 on: April 02, 2012, 03:45:56 pm » |
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: 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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #2 on: April 02, 2012, 03:49:19 pm » |
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
|
|
|
|
« Last Edit: April 02, 2012, 03:52:22 pm by Ashleysmith »
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #3 on: April 02, 2012, 04:07:20 pm » |
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
|
|
|
|
« Last Edit: April 02, 2012, 05:41:33 pm by sirbow2 »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #4 on: April 02, 2012, 04:16:59 pm » |
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??
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #5 on: April 02, 2012, 06:06:47 pm » |
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++; } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #6 on: April 03, 2012, 05:48:49 am » |
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'sServo 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 numsteering. 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 UPvoid 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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #7 on: April 03, 2012, 07:33:39 am » |
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  ) what is this: i think you have some parenthesis issues  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)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #8 on: April 03, 2012, 07:53:56 am » |
yea, the bit before was fine with just the use of the analog sticks..  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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #9 on: April 03, 2012, 07:57:28 am » |
i just couldn't get my head around the code i once said the same.( and still do sometimes  )
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #10 on: April 03, 2012, 08:00:04 am » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #11 on: April 03, 2012, 08:03:02 am » |
no, that would just be better for understanding what is going on. a integer named hi2 doesnt say much about what its for 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #12 on: April 03, 2012, 08:11:30 am » |
now i'm lost again :-(
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 478
|
 |
« Reply #13 on: April 03, 2012, 10:09:32 am » |
i guessed what "just added in the attachment for the servos" meant... show me code
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 21
|
 |
« Reply #14 on: April 03, 2012, 10:19:39 am » |
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'sServo 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 UPvoid 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
|
|
|
|
|
Logged
|
|
|
|
|
|