PS2X_Example.ino 6.9 KB

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