Advertisement
alif01

PS2 modifie

Apr 26th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. #include <PS2X_lib.h> //for v1.6
  2.  
  3. /******************************************************************
  4. * set pins connected to PS2 controller:
  5. * - 1e column: original
  6. * - 2e colmun: Stef?
  7. * replace pin numbers by the ones you use
  8. ******************************************************************/
  9. #define PS2_DAT 13 //14
  10. #define PS2_CMD 11 //15
  11. #define PS2_SEL 10 //16
  12. #define PS2_CLK 12 //17
  13. #include <AFMotor.h>
  14.  
  15. AF_DCMotor motor1(4);
  16. AF_DCMotor motor2(7);
  17. AF_DCMotor motor3(8);
  18. /******************************************************************
  19. * select modes of PS2 controller:
  20. * - pressures = analog reading of push-butttons
  21. * - rumble = motor rumbling
  22. * uncomment 1 of the lines for each mode selection
  23. ******************************************************************/
  24. //#define pressures true
  25. #define pressures false
  26. //#define rumble true
  27. #define rumble false
  28.  
  29. PS2X ps2x; // create PS2 Controller Class
  30.  
  31. //right now, the library does NOT support hot pluggable controllers, meaning
  32. //you must always either restart your Arduino after you connect the controller,
  33. //or call config_gamepad(pins) again after connecting the controller.
  34.  
  35. int error = 0;
  36. byte type = 0;
  37. byte vibrate = 0;
  38.  
  39. void setup(){
  40.  
  41. Serial.begin(57600);
  42.  
  43. delay(300); //added delay to give wireless ps2 module some time to startup, before configuring it
  44.  
  45. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  46.  
  47. //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  48. error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  49.  
  50. if(error == 0){
  51. Serial.print("Found Controller, configured successful ");
  52. Serial.print("pressures = ");
  53. if (pressures)
  54. Serial.println("true ");
  55. else
  56. Serial.println("false");
  57. Serial.print("rumble = ");
  58. if (rumble)
  59. Serial.println("true)");
  60. else
  61. Serial.println("false");
  62. Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  63. Serial.println("holding L1 or R1 will print out the analog stick values.");
  64. Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  65. }
  66. else if(error == 1)
  67. Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  68.  
  69. else if(error == 2)
  70. Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
  71.  
  72. else if(error == 3)
  73. Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  74.  
  75. // Serial.print(ps2x.Analog(1), HEX);
  76.  
  77. type = ps2x.readType();
  78. switch(type) {
  79. case 0:
  80. Serial.print("Unknown Controller type found ");
  81. break;
  82. case 1:
  83. Serial.print("DualShock Controller found ");
  84. break;
  85. case 2:
  86. Serial.print("GuitarHero Controller found ");
  87. break;
  88. case 3:
  89. Serial.print("Wireless Sony DualShock Controller found ");
  90. break;
  91. }
  92. }
  93.  
  94. void loop() {
  95. /* You must Read Gamepad to get new values and set vibration values
  96. ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
  97. if you don't enable the rumble, use ps2x.read_gamepad(); with no values
  98. You should call this at least once a second
  99. */
  100. if(error == 1) //skip loop if no controller found
  101. return;
  102.  
  103. if(type == 2){ //Guitar Hero Controller
  104. ps2x.read_gamepad(); //read controller
  105.  
  106. if(ps2x.ButtonPressed(GREEN_FRET))
  107. Serial.println("Green Fret Pressed");
  108. if(ps2x.ButtonPressed(RED_FRET))
  109. Serial.println("Red Fret Pressed");
  110. if(ps2x.ButtonPressed(YELLOW_FRET))
  111. Serial.println("Yellow Fret Pressed");
  112. if(ps2x.ButtonPressed(BLUE_FRET))
  113. Serial.println("Blue Fret Pressed");
  114. if(ps2x.ButtonPressed(ORANGE_FRET))
  115. Serial.println("Orange Fret Pressed");
  116.  
  117. if(ps2x.ButtonPressed(STAR_POWER))
  118. Serial.println("Star Power Command");
  119.  
  120. if(ps2x.Button(UP_STRUM)) //will be TRUE as long as button is pressed
  121. Serial.println("Up Strum");
  122. if(ps2x.Button(DOWN_STRUM))
  123. Serial.println("DOWN Strum");
  124.  
  125. if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
  126. Serial.println("Start is being held");
  127. if(ps2x.Button(PSB_SELECT))
  128. Serial.println("Select is being held");
  129.  
  130. if(ps2x.Button(ORANGE_FRET)) { // print stick value IF TRUE
  131. Serial.print("Wammy Bar Position:");
  132. Serial.println(ps2x.Analog(WHAMMY_BAR), DEC);
  133. }
  134. }
  135. else { //DualShock Controller
  136. ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
  137.  
  138. if(ps2x.Button(PSB_START)) //will be TRUE as long as button is pressed
  139. Serial.println("Start is being held");
  140. if(ps2x.Button(PSB_SELECT))
  141. Serial.println("Select is being held");
  142.  
  143. if(ps2x.Button(PSB_PAD_UP)) { //will be TRUE as long as button is pressed
  144. motor1.setSpeed(255); //I'm not sure if the motor is turning left or not
  145. motor1.run(FORWARD); //If it is not, change this function to run(BACKWARD)
  146. motor2.setSpeed(255);
  147. motor2.run(FORWARD);
  148. Serial.print("Up held this hard: ");
  149. Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  150. }
  151. if(ps2x.Button(PSB_PAD_RIGHT)){
  152. motor1.setSpeed(255); //I'm not sure if the motor is turning left or not
  153. motor1.run(FORWARD); //If it is not, change this function to run(BACKWARD)
  154. Serial.print("Right held this hard: ");
  155. Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
  156. }
  157. if(ps2x.Button(PSB_PAD_LEFT)){
  158. motor2.setSpeed(255);
  159. motor2.run(BACKWARD); // and change this function to .run(FORWARD)
  160. Serial.print("LEFT held this hard: ");
  161. Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
  162. }
  163. if(ps2x.Button(PSB_PAD_DOWN)){
  164. motor1.setSpeed(255);
  165. motor1.run(BACKWARD); // and change this function to .run(FORWARD)
  166. motor2.setSpeed(255);
  167. motor2.run(FORWARD);
  168. Serial.print("DOWN held this hard: ");
  169. Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  170. }
  171.  
  172. vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
  173. motor3.setSpeed(255);
  174. motor3.run(BACKWARD); // and change this function to .run(FORWARD)
  175. if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
  176. if(ps2x.Button(PSB_L3))
  177. Serial.println("L3 pressed");
  178. if(ps2x.Button(PSB_R3))
  179. Serial.println("R3 pressed");
  180. if(ps2x.Button(PSB_L2))
  181. Serial.println("L2 pressed");
  182. if(ps2x.Button(PSB_R2))
  183. Serial.println("R2 pressed");
  184.  
  185. // if(ps2x.Button(PSB_TRIANGLE))
  186. // motor3.setSpeed(255);
  187. // motor3.run(FORWARD); // and change this function to .run(FORWARD)
  188. //Serial.println("Triangle pressed");
  189. }
  190.  
  191. if(ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
  192. motor3.setSpeed(255);
  193. motor3.run(FORWARD);
  194. Serial.println("Circle just pressed");
  195. //if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
  196. //Serial.println("X just changed");
  197. if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
  198. motor3.setSpeed(255);
  199. motor3.run(BACKWARD);
  200. Serial.println("Square just released");
  201.  
  202. if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
  203. Serial.print("Stick Values:");
  204. Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX
  205. Serial.print(",");
  206. Serial.print(ps2x.Analog(PSS_LX), DEC);
  207. Serial.print(",");
  208. Serial.print(ps2x.Analog(PSS_RY), DEC);
  209. Serial.print(",");
  210. Serial.println(ps2x.Analog(PSS_RX), DEC);
  211. }
  212. }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement