Advertisement
Guest User

Xbox Motor Driver

a guest
Sep 30th, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.99 KB | None | 0 0
  1. /*
  2. Example sketch for the Xbox Wireless Reciver library - developed by Kristian Lauszus
  3. It supports up to four controllers wirelessly
  4. For more information see the blog post: http://blog.tkjelectronics.dk/2012/12/xbox-360-receiver-added-to-the-usb-host-library/ or
  5. send me an e-mail: kristianl@tkjelectronics.com
  6. */
  7.  
  8. #include <XBOXRECV.h>
  9. // Satisfy IDE, which only needs to see the include statment in the ino.
  10. #ifdef dobogusinclude
  11. #include <spi4teensy3.h>
  12. #endif
  13.  
  14. USB Usb;
  15. XBOXRECV Xbox(&Usb);
  16.  
  17. //Left Motor
  18. int PWMPin = 6;
  19. int PWMPin2 = 11;
  20.  
  21. //Right Motor
  22. int PWMPin3 = 3;
  23. int PWMPin4 = 5;
  24.  
  25. int wheelLock = 14;
  26.  
  27. int enable1 = 15;
  28. int enable2 = 16;
  29. int enable3 = 17;
  30. int enable4 = 18;
  31.  
  32. int wheelLockEnable = 0;
  33.  
  34. int ControllerConnected = A5;
  35.  
  36. void setup() {
  37. Serial.begin(115200);
  38. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  39. if (Usb.Init() == -1) {
  40. Serial.print(F("\r\nOSC did not start"));
  41. while (1); //halt
  42. }
  43. Serial.print(F("\r\nXbox Wireless Receiver Library Started"));
  44.  
  45. pinMode(PWMPin, OUTPUT);
  46. pinMode(PWMPin2, OUTPUT);
  47. pinMode(PWMPin3, OUTPUT);
  48. pinMode(PWMPin4, OUTPUT);
  49.  
  50. pinMode(enable1, OUTPUT);
  51. pinMode(enable2, OUTPUT);
  52. pinMode(enable3, OUTPUT);
  53. pinMode(enable4, OUTPUT);
  54.  
  55. pinMode(wheelLock, INPUT);
  56.  
  57. pinMode(ControllerConnected, OUTPUT);
  58. }
  59. void loop() {
  60. Usb.Task();
  61. if (Xbox.XboxReceiverConnected) {
  62. for (uint8_t i = 0; i < 4; i++) {
  63. if (Xbox.Xbox360Connected[i]) {
  64.  
  65. digitalWrite(ControllerConnected, HIGH); // light up an LED showing the controller is connected
  66. //the LED responds appropriately, but changes in bightess once controller data is received
  67.  
  68. if (Xbox.getButtonPress(L2, i) || Xbox.getButtonPress(R2, i)) {
  69. Serial.print("L2: ");
  70. Serial.print(Xbox.getButtonPress(L2, i));
  71. Serial.print("\tR2: ");
  72. Serial.println(Xbox.getButtonPress(R2, i));
  73. //Xbox.setRumbleOn(Xbox.getButtonPress(L2, i), Xbox.getButtonPress(R2, i), i);
  74. }
  75.  
  76. if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500 || Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500 || Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500 || Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) {
  77. if (Xbox.getAnalogHat(LeftHatX, i) > 7500 || Xbox.getAnalogHat(LeftHatX, i) < -7500) {
  78. Serial.print(F("LeftHatX: "));
  79. Serial.print(Xbox.getAnalogHat(LeftHatX, i));
  80. Serial.print("\t");
  81. }
  82. if (Xbox.getAnalogHat(LeftHatY, i) > 7500 || Xbox.getAnalogHat(LeftHatY, i) < -7500) {
  83. Serial.print(F("LeftHatY: "));
  84. Serial.print(Xbox.getAnalogHat(LeftHatY, i));
  85. Serial.print("\t");
  86. }
  87. if (Xbox.getAnalogHat(RightHatX, i) > 7500 || Xbox.getAnalogHat(RightHatX, i) < -7500) {
  88. Serial.print(F("RightHatX: "));
  89. Serial.print(Xbox.getAnalogHat(RightHatX, i));
  90. Serial.print("\t");
  91. }
  92. if (Xbox.getAnalogHat(RightHatY, i) > 7500 || Xbox.getAnalogHat(RightHatY, i) < -7500) {
  93. Serial.print(F("RightHatY: "));
  94. Serial.print(Xbox.getAnalogHat(RightHatY, i));
  95. }
  96. Serial.println();
  97. }
  98.  
  99. if (Xbox.getButtonClick(UP, i)) {
  100. Xbox.setLedOn(LED1, i);
  101. Serial.println(F("Up"));
  102. }
  103. if (Xbox.getButtonClick(DOWN, i)) {
  104. Xbox.setLedOn(LED4, i);
  105. Serial.println(F("Down"));
  106. }
  107. if (Xbox.getButtonClick(LEFT, i)) {
  108. Xbox.setLedOn(LED3, i);
  109. Serial.println(F("Left"));
  110. }
  111. if (Xbox.getButtonClick(RIGHT, i)) {
  112. Xbox.setLedOn(LED2, i);
  113. Serial.println(F("Right"));
  114. }
  115.  
  116. if (Xbox.getButtonClick(START, i)) {
  117. Xbox.setLedMode(ALTERNATING, i);
  118. Serial.println(F("Start"));
  119. }
  120. if (Xbox.getButtonClick(BACK, i)) {
  121. Xbox.setLedBlink(ALL, i);
  122. Serial.println(F("Back"));
  123. }
  124. if (Xbox.getButtonClick(L3, i))
  125. Serial.println(F("L3"));
  126. if (Xbox.getButtonClick(R3, i))
  127. Serial.println(F("R3"));
  128.  
  129. if (Xbox.getButtonClick(L1, i))
  130. Serial.println(F("L1"));
  131. if (Xbox.getButtonClick(R1, i))
  132. Serial.println(F("R1"));
  133. if (Xbox.getButtonClick(XBOX, i)) {
  134. Xbox.setLedMode(ROTATING, i);
  135. Serial.print(F("Xbox (Battery: "));
  136. Serial.print(Xbox.getBatteryLevel(i)); // The battery level in the range 0-3
  137. Serial.println(F(")"));
  138. }
  139. if (Xbox.getButtonClick(SYNC, i)) {
  140. Serial.println(F("Sync"));
  141. Xbox.disconnect(i);
  142. }
  143.  
  144. if (Xbox.getButtonClick(A, i))
  145. Serial.println(F("A"));
  146. if (Xbox.getButtonClick(B, i))
  147. Serial.println(F("B"));
  148. if (Xbox.getButtonClick(X, i))
  149. Serial.println(F("X"));
  150. if (Xbox.getButtonClick(Y, i))
  151. Serial.println(F("Y"));
  152.  
  153. //Code used for motor Drivers. 4 Enable pins and 2 drive functions
  154.  
  155. wheelLockEnable = digitalRead(wheelLock);
  156.  
  157. if (wheelLockEnable == HIGH) {
  158.  
  159. digitalWrite(enable1, HIGH);
  160. digitalWrite(enable2, HIGH);
  161. digitalWrite(enable3, HIGH);
  162. digitalWrite(enable4, HIGH);
  163. //turns ON individual H-Bridge chips
  164. //this chunk needs to run when the controller IS connected
  165. if (!Xbox.getButtonPress(Y)) {
  166. ForwardTankDrive ();
  167. }
  168.  
  169. if(Xbox.getButtonPress(Y)) {
  170. ReverseTankDrive ();
  171. }
  172.  
  173. }//Wheel lock enable
  174. }//end controller connected
  175.  
  176. else {
  177. digitalWrite(enable1, LOW);
  178. digitalWrite(enable2, LOW);
  179. digitalWrite(enable3, LOW); //these lines need to be written to LOW when the controller is not connected
  180. digitalWrite(enable4, LOW);
  181.  
  182. digitalWrite(ControllerConnected, LOW);
  183. //without these write statements, my HIGH values are 5 volts
  184. //while these are here, the HIGH values are only 0.6 volts
  185.  
  186. }//end else for when controller is not connected
  187. }//end for
  188. }//end receiver
  189. }//end loop
  190.  
  191. void ForwardTankDrive () {
  192.  
  193. analogWrite(PWMPin2, 0);
  194. analogWrite(PWMPin, Xbox.getButtonPress(R2));
  195. //left trigger
  196.  
  197. analogWrite(PWMPin4, 0);
  198. analogWrite(PWMPin3, Xbox.getButtonPress(L2));
  199. //right trigger
  200. }
  201.  
  202. void ReverseTankDrive () {
  203.  
  204. analogWrite(PWMPin, 0);
  205. analogWrite(PWMPin2, Xbox.getButtonPress(R2));
  206. //left trigger
  207.  
  208. analogWrite(PWMPin3, 0);
  209. analogWrite(PWMPin4, Xbox.getButtonPress(L2));
  210. //right trigger
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement