You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

PS3BT.ino 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
  3. For more information visit my blog: http://blog.tkjelectronics.dk/ or
  4. send me an e-mail: [email protected]
  5. */
  6. #include <PS3BT.h>
  7. #include <usbhub.h>
  8. // Satisfy the IDE, which needs to see the include statment in the ino too.
  9. #ifdef dobogusinclude
  10. #include <spi4teensy3.h>
  11. #include <SPI.h>
  12. #endif
  13. USB Usb;
  14. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  15. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  16. /* You can create the instance of the class in two ways */
  17. PS3BT PS3(&Btd); // This will just create the instance
  18. //PS3BT PS3(&Btd, 0x00, 0x15, 0x83, 0x3D, 0x0A, 0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
  19. bool printTemperature;
  20. bool printAngle;
  21. void setup() {
  22. Serial.begin(115200);
  23. #if !defined(__MIPSEL__)
  24. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  25. #endif
  26. if (Usb.Init() == -1) {
  27. Serial.print(F("\r\nOSC did not start"));
  28. while (1); //halt
  29. }
  30. Serial.print(F("\r\nPS3 Bluetooth Library Started"));
  31. }
  32. void loop() {
  33. Usb.Task();
  34. if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
  35. if (PS3.getAnalogHat(LeftHatX) > 137 || PS3.getAnalogHat(LeftHatX) < 117 || PS3.getAnalogHat(LeftHatY) > 137 || PS3.getAnalogHat(LeftHatY) < 117 || PS3.getAnalogHat(RightHatX) > 137 || PS3.getAnalogHat(RightHatX) < 117 || PS3.getAnalogHat(RightHatY) > 137 || PS3.getAnalogHat(RightHatY) < 117) {
  36. Serial.print(F("\r\nLeftHatX: "));
  37. Serial.print(PS3.getAnalogHat(LeftHatX));
  38. Serial.print(F("\tLeftHatY: "));
  39. Serial.print(PS3.getAnalogHat(LeftHatY));
  40. if (PS3.PS3Connected) { // The Navigation controller only have one joystick
  41. Serial.print(F("\tRightHatX: "));
  42. Serial.print(PS3.getAnalogHat(RightHatX));
  43. Serial.print(F("\tRightHatY: "));
  44. Serial.print(PS3.getAnalogHat(RightHatY));
  45. }
  46. }
  47. // Analog button values can be read from almost all buttons
  48. if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  49. Serial.print(F("\r\nL2: "));
  50. Serial.print(PS3.getAnalogButton(L2));
  51. if (PS3.PS3Connected) {
  52. Serial.print(F("\tR2: "));
  53. Serial.print(PS3.getAnalogButton(R2));
  54. }
  55. }
  56. if (PS3.getButtonClick(PS)) {
  57. Serial.print(F("\r\nPS"));
  58. PS3.disconnect();
  59. }
  60. else {
  61. if (PS3.getButtonClick(TRIANGLE))
  62. Serial.print(F("\r\nTraingle"));
  63. if (PS3.getButtonClick(CIRCLE))
  64. Serial.print(F("\r\nCircle"));
  65. if (PS3.getButtonClick(CROSS))
  66. Serial.print(F("\r\nCross"));
  67. if (PS3.getButtonClick(SQUARE))
  68. Serial.print(F("\r\nSquare"));
  69. if (PS3.getButtonClick(UP)) {
  70. Serial.print(F("\r\nUp"));
  71. if (PS3.PS3Connected) {
  72. PS3.setLedOff();
  73. PS3.setLedOn(LED4);
  74. }
  75. }
  76. if (PS3.getButtonClick(RIGHT)) {
  77. Serial.print(F("\r\nRight"));
  78. if (PS3.PS3Connected) {
  79. PS3.setLedOff();
  80. PS3.setLedOn(LED1);
  81. }
  82. }
  83. if (PS3.getButtonClick(DOWN)) {
  84. Serial.print(F("\r\nDown"));
  85. if (PS3.PS3Connected) {
  86. PS3.setLedOff();
  87. PS3.setLedOn(LED2);
  88. }
  89. }
  90. if (PS3.getButtonClick(LEFT)) {
  91. Serial.print(F("\r\nLeft"));
  92. if (PS3.PS3Connected) {
  93. PS3.setLedOff();
  94. PS3.setLedOn(LED3);
  95. }
  96. }
  97. if (PS3.getButtonClick(L1))
  98. Serial.print(F("\r\nL1"));
  99. if (PS3.getButtonClick(L3))
  100. Serial.print(F("\r\nL3"));
  101. if (PS3.getButtonClick(R1))
  102. Serial.print(F("\r\nR1"));
  103. if (PS3.getButtonClick(R3))
  104. Serial.print(F("\r\nR3"));
  105. if (PS3.getButtonClick(SELECT)) {
  106. Serial.print(F("\r\nSelect - "));
  107. PS3.printStatusString();
  108. }
  109. if (PS3.getButtonClick(START)) {
  110. Serial.print(F("\r\nStart"));
  111. printAngle = !printAngle;
  112. }
  113. }
  114. #if 0 // Set this to 1 in order to see the angle of the controller
  115. if (printAngle) {
  116. Serial.print(F("\r\nPitch: "));
  117. Serial.print(PS3.getAngle(Pitch));
  118. Serial.print(F("\tRoll: "));
  119. Serial.print(PS3.getAngle(Roll));
  120. }
  121. #endif
  122. }
  123. #if 0 // Set this to 1 in order to enable support for the Playstation Move controller
  124. else if (PS3.PS3MoveConnected) {
  125. if (PS3.getAnalogButton(T)) {
  126. Serial.print(F("\r\nT: "));
  127. Serial.print(PS3.getAnalogButton(T));
  128. }
  129. if (PS3.getButtonClick(PS)) {
  130. Serial.print(F("\r\nPS"));
  131. PS3.disconnect();
  132. }
  133. else {
  134. if (PS3.getButtonClick(SELECT)) {
  135. Serial.print(F("\r\nSelect"));
  136. printTemperature = !printTemperature;
  137. }
  138. if (PS3.getButtonClick(START)) {
  139. Serial.print(F("\r\nStart"));
  140. printAngle = !printAngle;
  141. }
  142. if (PS3.getButtonClick(TRIANGLE)) {
  143. Serial.print(F("\r\nTriangle"));
  144. PS3.moveSetBulb(Red);
  145. }
  146. if (PS3.getButtonClick(CIRCLE)) {
  147. Serial.print(F("\r\nCircle"));
  148. PS3.moveSetBulb(Green);
  149. }
  150. if (PS3.getButtonClick(SQUARE)) {
  151. Serial.print(F("\r\nSquare"));
  152. PS3.moveSetBulb(Blue);
  153. }
  154. if (PS3.getButtonClick(CROSS)) {
  155. Serial.print(F("\r\nCross"));
  156. PS3.moveSetBulb(Yellow);
  157. }
  158. if (PS3.getButtonClick(MOVE)) {
  159. PS3.moveSetBulb(Off);
  160. Serial.print(F("\r\nMove"));
  161. Serial.print(F(" - "));
  162. PS3.printStatusString();
  163. }
  164. }
  165. if (printAngle) {
  166. Serial.print(F("\r\nPitch: "));
  167. Serial.print(PS3.getAngle(Pitch));
  168. Serial.print(F("\tRoll: "));
  169. Serial.print(PS3.getAngle(Roll));
  170. }
  171. else if (printTemperature) {
  172. Serial.print(F("\r\nTemperature: "));
  173. Serial.print(PS3.getTemperature());
  174. }
  175. }
  176. #endif
  177. }