Code: Select all
/*
Code by: KR2_DIVING (Ryan)
) O
( o . O
) () .
/ O o
_.|._ o .()
/ _:_ \
|.(_ _).|
_\. : ./_
/ |..:..| \
/_/ `---' \_\
\_) \_)
\ T /
_)__|__(_
/....|....\
""""" """""
---KR2_DIVING---
I don't mind if you use this code for your own use. I have made it freely available online.
However, please be sure to credit my work, and the work of Bill Porter when using or passing
this code on.
Please direct any questions to: kr 2 diving (at) gmail (dot) com
Version History
Version Date Notes
------- -------- -----
1.0 23/09/12 First sucessful control of Motor (w/ Reverse) via Playstation2 Controller
1.1 28/09/12 Clean up of code. No changes to functionality. Added comments for posting to forum.
*/
#include <PS2X_lib.h> //Library for interfacing with PS2 Controller.
//Developed by: Bill Porter
//http://www.billporter.info/playstation-2-controller-arduino-library-v1-0/
#include <Servo.h> //include servo library
PS2X ps2x; // create PS2 Controller Class
Servo myservo; // create servo object to control servo
// maximum of eight servo objects can be created
/*#########################################
##### VARIABLE DECLALARATIONS #####
#########################################*/
int servoPos = 0; // variable to store the servo position
int LY = 0; // Variable for fixed values based on Left Stick Y-axis
int spd = 0; // variable to define speed sent to motor/servo
int R1state = 0; // used to determine state of R1
int R2state = 0; // used to determine state of R2
int neutral = 96; // neutral position for ESC. Will vary depending on ESC.
/*
right now, the PS2X library does NOT support hot pluggable controllers, meaning
you must always either restart your Arduino after you conect the controller,
or call config_gamepad(pins) again after connecting the controller.
*/
int error = 0; // used by PS2X.lib
byte type = 0; // used by PS2X.lib
byte vibrate = 0; // used by PS2X.lib
// Following are used in system just to indicate state.
//If spare I/O is available, will include as visual display in final control setup.
int GRNled = 3; // Green LED FULL speed
int YLWled = 4; // Yellow LED half speed
int REDled = 5; // Red LED Neutral
int PNKled = 6; // Pink LED reverse
/*#########################################
##### END OF SECTION #####
#########################################*/
void setup(){
Serial.begin(57600); // open serial communications.
// baudrate selected based on reccomendation for PS2X.lib
/*####################################################
##### DEFINE PINS OF PS2 CONTROLLER HERE #####
#####################################################*/
error = ps2x.config_gamepad(8,11,10,12, true, false); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
//Error checking from PS2X example code.
if(error == 0){
Serial.println("Found Controller, configured successful");
Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
Serial.println("holding L1 or R1 will print out the analog stick values.");
Serial.println("Go to www.billporter.info for updates and to report bugs.");
}
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); //Used only to debug PS2X code.
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;
}
/*############################################
##### DEFINE remaining pins here #####
##############################################*/
myservo.attach(2); //attach servo on pin 2 to the servo object
pinMode(GRNled, OUTPUT); // LED for visual confirmation of High speed mode
pinMode(YLWled, OUTPUT); // LED for visual confrimation of low speed mode
pinMode(REDled, OUTPUT); // LED for visual confirmation of Neutral state of motor
pinMode(PNKled, OUTPUT); // LED for visual confirmation of FWD direction of rotation
}
/*#####################################
##### END OF void setup() #####
######################################*/
void loop(){
/* You must Read Gamepad to get new values
Read GamePad and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
you should call this at least once a second
*/
if(error == 1) // skip loop if no controller found
return;
ps2x.read_gamepad(); // read controller and set large motor to spin at 'vibrate' speed
//set current position of Left Stick Y Axis to the variable LY
LY = ps2x.Analog(PSS_LY);
//limit value of LY to the range of 0 to 255
//value should automatically be constrained by controller, but this is just incase!
constrain(LY,0,255);
//determine state of "deadman" triggers
R1state = ps2x.Button(PSB_R1);
R2state = ps2x.Button(PSB_R2);
/*
For this program, I only use the Y-Axis (up down) of the LEFT joystick. The joystick has a range from 0 to 255.
0 = Full Up, 255 = Full down. This seemed counter intuitive to me...
After testing my ESC with a Potentiometer (code available upon request),
I discovered the following ranges for controlling the ESC
0 to 63 No rotation. Maxed out?
64 MAX "reverse"
92 MIN "reverse"
93 - 100 Neutral
101 MIN "forward"
135 MAX "forward"
136 - 255 No rotation. Maxed out?
Using the map() function, I was able to convert the 0-255 input of the controller
to a 63-135 output for the ESC.
The motor will only spin when 1 of 2 "deadman" switches are held. (R1 or R2)
R1 = Full Speed mode
R2 = 50% Speed mode
50% speed mode maintains 0-255 on the input, but limits output to 78-118.
*/
//##### Full speed mode #####
if((R1state ==1) && (R2state==0)) {
digitalWrite(GRNled, HIGH); // !! There must be a better way to code these LED lights. Suggestions?
digitalWrite(YLWled, LOW);
digitalWrite(REDled, LOW);
//set spd (speed) based on value of LY
if((LY >= 0) && (LY <=112)){
spd = map(LY, 0, 112, 64, 92); //this command maps or "converts" the Joystick input to ESC output range
digitalWrite(PNKled, HIGH);
//Clockwise rotation
}
if((LY >=113) && (LY <=133)){
spd = map(LY, 113, 133, 93, 100);
digitalWrite(PNKled, LOW);
//Neutral
}
if((LY >=134) && (LY <=255)) {
spd = map(LY, 134, 255, 101, 135);
digitalWrite(PNKled, LOW);
//Counter Clockwise
}
}
//##### Half speed mode #####
if((R1state ==0) && (R2state==1)) {
digitalWrite(GRNled, LOW);
digitalWrite(YLWled, HIGH);
digitalWrite(REDled, LOW);
//set spd (speed) based on value of LY
if((LY >= 0) && (LY <=112)){
spd = map(LY, 0, 112, 78, 92);
digitalWrite(PNKled, HIGH);
//Clockwise rotation
}
if((LY >=113) && (LY <=133)){
spd = map(LY, 113, 133, 93, 100);
digitalWrite(PNKled, LOW);
//Neutral
}
if((LY >=134) && (LY <=255)) {
spd = map(LY, 134, 255, 101, 118);
digitalWrite(PNKled, LOW);
//Counter Clockwise
}
}
//print values for debuging
Serial.print(LY); //ps2x.Analog(PSS_LY));
Serial.print(" ");
Serial.print(spd);
Serial.print(" ");
Serial.print(ps2x.Button(PSB_R1));
Serial.print(" ");
Serial.println(ps2x.Button(PSB_R2));
//send value to servo/motor
if((R1state == 1) || (R2state ==1)) {
myservo.write(spd); //if either "deadman" switch is held, send the correct 'mapped' speed value to the ESC.
}
if((R1state==0) && (R2state==0)) {
myservo.write(neutral); //if neither "deadman" switch is held, send 'neutral' to the Servo.
digitalWrite(GRNled, LOW);
digitalWrite(YLWled, LOW);
digitalWrite(REDled, HIGH);
digitalWrite(PNKled, LOW);
}
//allow servo/motor to achieve target speed
delay(50);
}
/* I am by no means a pro at Arduino programming. If you notice any bugs, please let me know! */
Best of luck!