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: kristianl@tkjelectronics.com
6 This example show how one can combine all the difference Bluetooth services in one single code.
8 You will need a Arduino Mega 1280/2560 to run this sketch,
9 as a normal Arduino (Uno, Duemilanove etc.) doesn't have enough SRAM and FLASH
16 // Satisfy the IDE, which needs to see the include statment in the ino too.
18 #include <spi4teensy3.h>
23 //USBHub Hub1(&Usb); // Some dongles have a hub inside
25 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
27 /* You can create the instances of the bluetooth services in two ways */
28 SPP SerialBT(&Btd); // This will set the name to the defaults: "Arduino" and the pin to "0000"
29 //SPP SerialBTBT(&Btd,"Lauszus's Arduino","0000"); // You can also set the name and pin like so
30 PS3BT PS3(&Btd); // This will just create the instance
31 //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
33 bool firstMessage = true;
34 String output = ""; // We will store the data in this string
37 Serial.begin(115200); // This wil lprint the debugging from the libraries
38 #if !defined(__MIPSEL__)
39 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
41 if (Usb.Init() == -1) {
42 Serial.print(F("\r\nOSC did not start"));
45 Serial.print(F("\r\nBluetooth Library Started"));
46 output.reserve(200); // Reserve 200 bytes for the output string
49 Usb.Task(); // The SPP data is actually not send until this is called, one could call SerialBT.send() directly as well
51 if (SerialBT.connected) {
54 SerialBT.println(F("Hello from Arduino")); // Send welcome message
56 if (Serial.available())
57 SerialBT.write(Serial.read());
58 if (SerialBT.available())
59 Serial.write(SerialBT.read());
64 if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
65 output = ""; // Reset output string
66 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) {
67 output += "LeftHatX: ";
68 output += PS3.getAnalogHat(LeftHatX);
69 output += "\tLeftHatY: ";
70 output += PS3.getAnalogHat(LeftHatY);
71 if (PS3.PS3Connected) { // The Navigation controller only have one joystick
72 output += "\tRightHatX: ";
73 output += PS3.getAnalogHat(RightHatX);
74 output += "\tRightHatY: ";
75 output += PS3.getAnalogHat(RightHatY);
78 //Analog button values can be read from almost all buttons
79 if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
83 output += PS3.getAnalogButton(L2);
84 if (PS3.PS3Connected) {
86 output += PS3.getAnalogButton(R2);
90 Serial.println(output);
91 if (SerialBT.connected)
92 SerialBT.println(output);
93 output = ""; // Reset output string
95 if (PS3.getButtonClick(PS)) {
100 if (PS3.getButtonClick(TRIANGLE))
101 output += " - Traingle";
102 if (PS3.getButtonClick(CIRCLE))
103 output += " - Circle";
104 if (PS3.getButtonClick(CROSS))
105 output += " - Cross";
106 if (PS3.getButtonClick(SQUARE))
107 output += " - Square";
109 if (PS3.getButtonClick(UP)) {
111 if (PS3.PS3Connected) {
116 if (PS3.getButtonClick(RIGHT)) {
117 output += " - Right";
118 if (PS3.PS3Connected) {
123 if (PS3.getButtonClick(DOWN)) {
125 if (PS3.PS3Connected) {
130 if (PS3.getButtonClick(LEFT)) {
132 if (PS3.PS3Connected) {
138 if (PS3.getButtonClick(L1))
140 if (PS3.getButtonClick(L3))
142 if (PS3.getButtonClick(R1))
144 if (PS3.getButtonClick(R3))
147 if (PS3.getButtonClick(SELECT)) {
148 output += " - Select";
150 if (PS3.getButtonClick(START))
151 output += " - Start";
154 String string = "PS3 Controller" + output;
155 Serial.println(string);
156 if (SerialBT.connected)
157 SerialBT.println(string);