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.

PS3SPP.ino 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Example sketch for the 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. This example show how one can combine all the difference Bluetooth services in one single code.
  6. Note:
  7. You will need a Arduino Mega 1280/2560 to run this sketch,
  8. as a normal Arduino (Uno, Duemilanove etc.) doesn't have enough SRAM and FLASH
  9. */
  10. #include <PS3BT.h>
  11. #include <SPP.h>
  12. #include <usbhub.h>
  13. // Satisfy the IDE, which needs to see the include statment in the ino too.
  14. #ifdef dobogusinclude
  15. #include <spi4teensy3.h>
  16. #include <SPI.h>
  17. #endif
  18. USB Usb;
  19. //USBHub Hub1(&Usb); // Some dongles have a hub inside
  20. BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
  21. /* You can create the instances of the bluetooth services in two ways */
  22. SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "0000"
  23. //SPP SerialBTBT(&Btd,"Lauszus's Arduino","0000"); // You can also set the name and pin like so
  24. PS3BT PS3(&Btd); // This will just create the instance
  25. //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
  26. bool firstMessage = true;
  27. String output = ""; // We will store the data in this string
  28. void setup() {
  29. Serial.begin(115200); // This wil lprint the debugging from the libraries
  30. #if !defined(__MIPSEL__)
  31. while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
  32. #endif
  33. if (Usb.Init() == -1) {
  34. Serial.print(F("\r\nOSC did not start"));
  35. while (1); //halt
  36. }
  37. Serial.print(F("\r\nBluetooth Library Started"));
  38. output.reserve(200); // Reserve 200 bytes for the output string
  39. }
  40. void loop() {
  41. Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
  42. if (SerialBT.connected) {
  43. if (firstMessage) {
  44. firstMessage = false;
  45. SerialBT.println(F("Hello from Arduino")); // Send welcome message
  46. }
  47. if (Serial.available())
  48. SerialBT.write(Serial.read());
  49. if (SerialBT.available())
  50. Serial.write(SerialBT.read());
  51. }
  52. else
  53. firstMessage = true;
  54. if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
  55. output = ""; // Reset output string
  56. 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) {
  57. output += "LeftHatX: ";
  58. output += PS3.getAnalogHat(LeftHatX);
  59. output += "\tLeftHatY: ";
  60. output += PS3.getAnalogHat(LeftHatY);
  61. if (PS3.PS3Connected) { // The Navigation controller only have one joystick
  62. output += "\tRightHatX: ";
  63. output += PS3.getAnalogHat(RightHatX);
  64. output += "\tRightHatY: ";
  65. output += PS3.getAnalogHat(RightHatY);
  66. }
  67. }
  68. //Analog button values can be read from almost all buttons
  69. if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
  70. if (output != "")
  71. output += "\r\n";
  72. output += "L2: ";
  73. output += PS3.getAnalogButton(L2);
  74. if (PS3.PS3Connected) {
  75. output += "\tR2: ";
  76. output += PS3.getAnalogButton(R2);
  77. }
  78. }
  79. if (output != "") {
  80. Serial.println(output);
  81. if (SerialBT.connected)
  82. SerialBT.println(output);
  83. output = ""; // Reset output string
  84. }
  85. if (PS3.getButtonClick(PS)) {
  86. output += " - PS";
  87. PS3.disconnect();
  88. }
  89. else {
  90. if (PS3.getButtonClick(TRIANGLE))
  91. output += " - Traingle";
  92. if (PS3.getButtonClick(CIRCLE))
  93. output += " - Circle";
  94. if (PS3.getButtonClick(CROSS))
  95. output += " - Cross";
  96. if (PS3.getButtonClick(SQUARE))
  97. output += " - Square";
  98. if (PS3.getButtonClick(UP)) {
  99. output += " - Up";
  100. if (PS3.PS3Connected) {
  101. PS3.setLedOff();
  102. PS3.setLedOn(LED4);
  103. }
  104. }
  105. if (PS3.getButtonClick(RIGHT)) {
  106. output += " - Right";
  107. if (PS3.PS3Connected) {
  108. PS3.setLedOff();
  109. PS3.setLedOn(LED1);
  110. }
  111. }
  112. if (PS3.getButtonClick(DOWN)) {
  113. output += " - Down";
  114. if (PS3.PS3Connected) {
  115. PS3.setLedOff();
  116. PS3.setLedOn(LED2);
  117. }
  118. }
  119. if (PS3.getButtonClick(LEFT)) {
  120. output += " - Left";
  121. if (PS3.PS3Connected) {
  122. PS3.setLedOff();
  123. PS3.setLedOn(LED3);
  124. }
  125. }
  126. if (PS3.getButtonClick(L1))
  127. output += " - L1";
  128. if (PS3.getButtonClick(L3))
  129. output += " - L3";
  130. if (PS3.getButtonClick(R1))
  131. output += " - R1";
  132. if (PS3.getButtonClick(R3))
  133. output += " - R3";
  134. if (PS3.getButtonClick(SELECT)) {
  135. output += " - Select";
  136. }
  137. if (PS3.getButtonClick(START))
  138. output += " - Start";
  139. if (output != "") {
  140. String string = "PS3 Controller" + output;
  141. Serial.println(string);
  142. if (SerialBT.connected)
  143. SerialBT.println(string);
  144. }
  145. }
  146. delay(10);
  147. }
  148. }