Advertisement
lcordero

Untitled

Aug 2nd, 2013
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. #include <PS2X_lib.h> //for v1.6
  2.  
  3. PS2X ps2x; // create PS2 Controller Class
  4.  
  5. //right now, the library does NOT support hot pluggable controllers, meaning
  6. //you must always either restart your Arduino after you conect the controller,
  7. //or call config_gamepad(pins) again after connecting the controller.
  8. int error = 0;
  9. byte type = 0;
  10. byte vibrate = 0;
  11. int a = 0;
  12. //Arduino PWM Speed Control for DFRobot Motor Shield (default pins)
  13. int E1 = 3;
  14. int M1 = 12;
  15. int E2 = 11;
  16. int M2 = 13;
  17. int lmotor = 0;
  18. int rmotor = 0;
  19. int led = 10;
  20. int light = 2;
  21. void setup()
  22. {
  23. Serial.begin(57600);
  24.  
  25. // set pin modes for DFRobot Motor Shield. These are the modes for both motors. For example: M1 is for motor 1 and M2 is for motor 2.
  26. pinMode(M1, OUTPUT);
  27. pinMode(M2, OUTPUT);
  28.  
  29. pinMode(led, OUTPUT);
  30. pinMode(light, OUTPUT);
  31. error = ps2x.config_gamepad(7,6,5,4, true, true); //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  32.  
  33. if(error == 0)
  34. {
  35. Serial.println("Found Controller, configured successful");
  36. Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
  37. Serial.println("holding L1 or R1 will print out the analog stick values.");
  38. Serial.println("Go to www.billporter.info for updates and to report bugs.");
  39. }
  40. else if(error == 1)
  41. {
  42. Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
  43. }
  44. else if(error == 2)
  45. {
  46. Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
  47. }
  48. else if(error == 3)
  49. {
  50. Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  51. }
  52.  
  53. type = ps2x.readType();
  54. if (type != 1)
  55. {
  56. Serial.println("warning: DualShock Controller Not Found!");
  57. }
  58. }
  59.  
  60. void loop()
  61. {
  62. if(error == 1) //skip loop if no controller found
  63. return;
  64. if (type == 1)
  65. {
  66. ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
  67. if (ps2x.Button(PSB_PINK)){ // On this line I just added another button that I can use on my controller to turn on muy led lights. The color of the button is pink so that is what it's called.
  68. if( a == 0)
  69. a = 1;
  70. else
  71. a = 0;}
  72.  
  73. if (a == 0){
  74. digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
  75. digitalWrite(light, LOW); // turn the LED off by making the voltage LOW
  76. }
  77. else
  78. {
  79. digitalWrite(led, HIGH); // turn the LED off by making the voltage LOW
  80. digitalWrite(light, HIGH); // turn the LED off by making the voltage LOW
  81. }
  82.  
  83.  
  84. lmotor = 0;
  85. if (ps2x.Button(PSB_L1)) // this is the left motor speed. 255 is the top speed that the tank goes forward for the left motor. The -255 is the top speed that the tank goes backwards.
  86. // it is the same way with the right motor. It works the same way that the right motor works.
  87. lmotor = 255;
  88. if (ps2x.Button(PSB_L2))
  89. lmotor = -255;
  90.  
  91. rmotor = 0;
  92. if (ps2x.Button(PSB_R1))
  93. rmotor = 255;
  94. if (ps2x.Button(PSB_R2))
  95. rmotor = -255;
  96.  
  97. }
  98. else
  99. {
  100. lmotor = 0;
  101. rmotor = 0;
  102. }
  103.  
  104. // update motors
  105. if (lmotor < 0)
  106. {
  107. digitalWrite(M1, LOW);
  108. analogWrite(E1, -lmotor); //PWM Speed Control
  109. }
  110. else
  111. {
  112. digitalWrite(M1, HIGH);
  113. analogWrite(E1, lmotor); //PWM Speed Control
  114. }
  115.  
  116. if (rmotor < 0)
  117. {
  118. digitalWrite(M2, LOW);
  119. analogWrite(E2, -rmotor); //PWM Speed Control
  120. }
  121. else
  122. {
  123. digitalWrite(M2, HIGH);
  124. analogWrite(E2, rmotor); //PWM Speed Control
  125. }
  126. // the HIGH means that the motors are at full speed and it is what makes it move. The low means that the motors are stopped.
  127. delay(30); // I added the led code in this so that I can get everything on my tank working together.
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement