Advertisement
alif01

PS2 shield dc motor

Apr 27th, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. /*
  2. This example shows how to retrieve a button status on PS2 Controller.
  3.  
  4. Function:
  5. readButton(button); // Read button status, it will return corresponding data
  6. // Digital button: 0 = pressed, 1 = released
  7. // Analog button: return a value
  8.  
  9. Digital button:
  10. PS2_SELECT
  11. PS2_JOYSTICK_LEFT
  12. PS2_JOYSTICK_RIGHT
  13. PS2_START
  14. PS2_UP
  15. PS2_RIGHT
  16. PS2_DOWN
  17. PS2_LEFT
  18. PS2_LEFT_2
  19. PS2_RIGHT_2
  20. PS2_LEFT_1
  21. PS2_RIGHT_1
  22. PS2_TRIANGLE
  23. PS2_CIRCLE
  24. PS2_CROSS
  25. PS2_SQUARE
  26.  
  27. Analog button:
  28. PS2_JOYSTICK_LEFT_X_AXIS
  29. PS2_JOYSTICK_LEFT_Y_AXIS
  30. PS2_JOYSTICK_RIGHT_X_AXIS
  31. PS2_JOYSTICK_RIGHT_Y_AXIS
  32. PS2_JOYSTICK_LEFT_UP
  33. PS2_JOYSTICK_LEFT_DOWN
  34. PS2_JOYSTICK_LEFT_LEFT
  35. PS2_JOYSTICK_LEFT_RIGHT
  36. PS2_JOYSTICK_RIGHT_UP
  37. PS2_JOYSTICK_RIGHT_DOWN
  38. PS2_JOYSTICK_RIGHT_LEFT
  39. PS2_JOYSTICK_RIGHT_RIGHT
  40.  
  41. Product page:
  42. Cytron PS2 Shield: http://www.cytron.com.my/p-shield-ps2
  43. PS2 Controller: http://www.cytron.com.my/p-ps-gp-1
  44. CT-UNO: http://www.cytron.com.my/p-ct-uno
  45.  
  46. Original written by:
  47. Cytron Technologies
  48.  
  49. Modified:
  50. 29/06/15 Idris, Cytron Technologies
  51. */
  52.  
  53. #include <SoftwareSerial.h>
  54. #include <Cytron_PS2Shield.h>
  55. #include <AFMotor.h>
  56.  
  57. Cytron_PS2Shield ps2(2, 3); // SoftwareSerial: Rx and Tx pin
  58. //Cytron_PS2Shield ps2; // HardwareSerial
  59.  
  60. AF_DCMotor motor1(1);
  61. AF_DCMotor motor2(2);
  62. AF_DCMotor motor3(4);
  63.  
  64. int pwm_motor1 = 7;
  65. int pwm_motor2 = 8;
  66. int pwm_motor3 = 9;
  67.  
  68. void setup()
  69. {
  70. ps2.begin(9600); // This baudrate must same with the jumper setting at PS2 shield
  71. Serial.begin(9600);
  72. Serial.println("Start");
  73.  
  74. }
  75.  
  76. void loop()
  77. {
  78. // LED on main board will light up if 'Select' button is pressed
  79. if(ps2.readButton(PS2_UP) == 0) // 0 = pressed, 1 = released
  80. {
  81. Serial.println("Forward!!");
  82. motor1.run(FORWARD);
  83. motor2.run(FORWARD);
  84.  
  85. }
  86. if(ps2.readButton(PS2_DOWN) == 0){
  87. Serial.println("Backward!!");
  88. motor1.run(BACKWARD);
  89. motor2.run(BACKWARD);
  90. }
  91. if(ps2.readButton(PS2_LEFT) == 0){
  92. Serial.println("Left!!");
  93. motor1.run(FORWARD);
  94. }
  95. if(ps2.readButton(PS2_RIGHT) == 0)
  96. {
  97. Serial.println("Right!!");
  98. motor2.run(FORWARD);
  99. }
  100. if(ps2.readButton(PS2_CROSS) == 0 ){
  101. Serial.println("Float!!");
  102. motor3.run(FORWARD);
  103. }
  104. if(ps2.readButton(PS2_SQUARE) == 0 ){
  105. Serial.println("Submerge");
  106. motor3.run(BACKWARD);
  107. }
  108. if(ps2.readButton(PS2_CIRCLE) == 0){
  109. Serial.println("Stop");
  110. motor1.run(RELEASE);
  111. motor2.run(RELEASE);
  112. motor3.run(RELEASE);
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement