2 Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
3 This example show how one can use multiple controllers with the library
4 For more information visit my blog: http://blog.tkjelectronics.dk/ or
5 send me an e-mail: kristianl@tkjelectronics.com
11 // Satisfy the IDE, which needs to see the include statment in the ino too.
13 #include <spi4teensy3.h>
18 //USBHub Hub1(&Usb); // Some dongles have a hub inside
20 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
21 PS3BT *PS3[2]; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
22 const uint8_t length = sizeof(PS3) / sizeof(PS3[0]); // Get the lenght of the array
23 bool printAngle[length];
24 bool oldControllerState[length];
27 for (uint8_t i = 0; i < length; i++) {
28 PS3[i] = new PS3BT(&Btd); // Create the instances
29 PS3[i]->attachOnInit(onInit); // onInit() is called upon a new connection - you can call the function whatever you like
33 #if !defined(__MIPSEL__)
34 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
36 if (Usb.Init() == -1) {
37 Serial.print(F("\r\nOSC did not start"));
40 Serial.print(F("\r\nPS3 Bluetooth Library Started"));
45 for (uint8_t i = 0; i < length; i++) {
46 if (PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) {
47 if (PS3[i]->getAnalogHat(LeftHatX) > 137 || PS3[i]->getAnalogHat(LeftHatX) < 117 || PS3[i]->getAnalogHat(LeftHatY) > 137 || PS3[i]->getAnalogHat(LeftHatY) < 117 || PS3[i]->getAnalogHat(RightHatX) > 137 || PS3[i]->getAnalogHat(RightHatX) < 117 || PS3[i]->getAnalogHat(RightHatY) > 137 || PS3[i]->getAnalogHat(RightHatY) < 117) {
48 Serial.print(F("\r\nLeftHatX: "));
49 Serial.print(PS3[i]->getAnalogHat(LeftHatX));
50 Serial.print(F("\tLeftHatY: "));
51 Serial.print(PS3[i]->getAnalogHat(LeftHatY));
52 if (PS3[i]->PS3Connected) { // The Navigation controller only have one joystick
53 Serial.print(F("\tRightHatX: "));
54 Serial.print(PS3[i]->getAnalogHat(RightHatX));
55 Serial.print(F("\tRightHatY: "));
56 Serial.print(PS3[i]->getAnalogHat(RightHatY));
59 //Analog button values can be read from almost all buttons
60 if (PS3[i]->getAnalogButton(L2) || PS3[i]->getAnalogButton(R2)) {
61 Serial.print(F("\r\nL2: "));
62 Serial.print(PS3[i]->getAnalogButton(L2));
63 if (PS3[i]->PS3Connected) {
64 Serial.print(F("\tR2: "));
65 Serial.print(PS3[i]->getAnalogButton(R2));
68 if (PS3[i]->getButtonClick(PS)) {
69 Serial.print(F("\r\nPS"));
71 oldControllerState[i] = false; // Reset value
74 if (PS3[i]->getButtonClick(TRIANGLE))
75 Serial.print(F("\r\nTraingle"));
76 if (PS3[i]->getButtonClick(CIRCLE))
77 Serial.print(F("\r\nCircle"));
78 if (PS3[i]->getButtonClick(CROSS))
79 Serial.print(F("\r\nCross"));
80 if (PS3[i]->getButtonClick(SQUARE))
81 Serial.print(F("\r\nSquare"));
83 if (PS3[i]->getButtonClick(UP)) {
84 Serial.print(F("\r\nUp"));
85 if (PS3[i]->PS3Connected) {
87 PS3[i]->setLedOn(LED4);
90 if (PS3[i]->getButtonClick(RIGHT)) {
91 Serial.print(F("\r\nRight"));
92 if (PS3[i]->PS3Connected) {
94 PS3[i]->setLedOn(LED1);
97 if (PS3[i]->getButtonClick(DOWN)) {
98 Serial.print(F("\r\nDown"));
99 if (PS3[i]->PS3Connected) {
101 PS3[i]->setLedOn(LED2);
104 if (PS3[i]->getButtonClick(LEFT)) {
105 Serial.print(F("\r\nLeft"));
106 if (PS3[i]->PS3Connected) {
108 PS3[i]->setLedOn(LED3);
112 if (PS3[i]->getButtonClick(L1))
113 Serial.print(F("\r\nL1"));
114 if (PS3[i]->getButtonClick(L3))
115 Serial.print(F("\r\nL3"));
116 if (PS3[i]->getButtonClick(R1))
117 Serial.print(F("\r\nR1"));
118 if (PS3[i]->getButtonClick(R3))
119 Serial.print(F("\r\nR3"));
121 if (PS3[i]->getButtonClick(SELECT)) {
122 Serial.print(F("\r\nSelect - "));
123 PS3[i]->printStatusString();
125 if (PS3[i]->getButtonClick(START)) {
126 Serial.print(F("\r\nStart"));
127 printAngle[i] = !printAngle[i];
131 Serial.print(F("\r\nPitch: "));
132 Serial.print(PS3[i]->getAngle(Pitch));
133 Serial.print(F("\tRoll: "));
134 Serial.print(PS3[i]->getAngle(Roll));
137 /* I have removed the PS3 Move code as an Uno will run out of RAM if it's included */
138 //else if(PS3[i]->PS3MoveConnected) {
143 for (uint8_t i = 0; i < length; i++) {
144 if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) {
145 oldControllerState[i] = true; // Used to check which is the new controller
146 PS3[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"