2 Example sketch for the PS4 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
10 // Satisfy the IDE, which needs to see the include statment in the ino too.
12 #include <spi4teensy3.h>
17 //USBHub Hub1(&Usb); // Some dongles have a hub inside
18 BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
20 /* You can create the instance of the PS4BT class in two ways */
21 // This will start an inquiry and then pair with the PS4 controller - you only have to do this once
22 // You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in paring mode
23 PS4BT PS4(&Btd, PAIR);
25 // After that you can simply create the instance like so and then press the PS button on the device
28 bool printAngle, printTouch;
29 uint8_t oldL2Value, oldR2Value;
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\nPS4 Bluetooth Library Started"));
45 if (PS4.connected()) {
46 if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 || PS4.getAnalogHat(RightHatY) < 117) {
47 Serial.print(F("\r\nLeftHatX: "));
48 Serial.print(PS4.getAnalogHat(LeftHatX));
49 Serial.print(F("\tLeftHatY: "));
50 Serial.print(PS4.getAnalogHat(LeftHatY));
51 Serial.print(F("\tRightHatX: "));
52 Serial.print(PS4.getAnalogHat(RightHatX));
53 Serial.print(F("\tRightHatY: "));
54 Serial.print(PS4.getAnalogHat(RightHatY));
57 if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2)) { // These are the only analog buttons on the PS4 controller
58 Serial.print(F("\r\nL2: "));
59 Serial.print(PS4.getAnalogButton(L2));
60 Serial.print(F("\tR2: "));
61 Serial.print(PS4.getAnalogButton(R2));
63 if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value) // Only write value if it's different
64 PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
65 oldL2Value = PS4.getAnalogButton(L2);
66 oldR2Value = PS4.getAnalogButton(R2);
68 if (PS4.getButtonClick(PS)) {
69 Serial.print(F("\r\nPS"));
73 if (PS4.getButtonClick(TRIANGLE)) {
74 Serial.print(F("\r\nTraingle"));
75 PS4.setRumbleOn(RumbleLow);
77 if (PS4.getButtonClick(CIRCLE)) {
78 Serial.print(F("\r\nCircle"));
79 PS4.setRumbleOn(RumbleHigh);
81 if (PS4.getButtonClick(CROSS)) {
82 Serial.print(F("\r\nCross"));
83 PS4.setLedFlash(10, 10); // Set it to blink rapidly
85 if (PS4.getButtonClick(SQUARE)) {
86 Serial.print(F("\r\nSquare"));
87 PS4.setLedFlash(0, 0); // Turn off blinking
90 if (PS4.getButtonClick(UP)) {
91 Serial.print(F("\r\nUp"));
93 } if (PS4.getButtonClick(RIGHT)) {
94 Serial.print(F("\r\nRight"));
96 } if (PS4.getButtonClick(DOWN)) {
97 Serial.print(F("\r\nDown"));
99 } if (PS4.getButtonClick(LEFT)) {
100 Serial.print(F("\r\nLeft"));
104 if (PS4.getButtonClick(L1))
105 Serial.print(F("\r\nL1"));
106 if (PS4.getButtonClick(L3))
107 Serial.print(F("\r\nL3"));
108 if (PS4.getButtonClick(R1))
109 Serial.print(F("\r\nR1"));
110 if (PS4.getButtonClick(R3))
111 Serial.print(F("\r\nR3"));
113 if (PS4.getButtonClick(SHARE))
114 Serial.print(F("\r\nShare"));
115 if (PS4.getButtonClick(OPTIONS)) {
116 Serial.print(F("\r\nOptions"));
117 printAngle = !printAngle;
119 if (PS4.getButtonClick(TOUCHPAD)) {
120 Serial.print(F("\r\nTouchpad"));
121 printTouch = !printTouch;
124 if (printAngle) { // Print angle calculated using the accelerometer only
125 Serial.print(F("\r\nPitch: "));
126 Serial.print(PS4.getAngle(Pitch));
127 Serial.print(F("\tRoll: "));
128 Serial.print(PS4.getAngle(Roll));
131 if (printTouch) { // Print the x, y coordinates of the touchpad
132 if (PS4.isTouching(0) || PS4.isTouching(1)) // Print newline and carriage return if any of the fingers are touching the touchpad
133 Serial.print(F("\r\n"));
134 for (uint8_t i = 0; i < 2; i++) { // The touchpad track two fingers
135 if (PS4.isTouching(i)) { // Print the position of the finger if it is touching the touchpad
136 Serial.print(F("X")); Serial.print(i + 1); Serial.print(F(": "));
137 Serial.print(PS4.getX(i));
138 Serial.print(F("\tY")); Serial.print(i + 1); Serial.print(F(": "));
139 Serial.print(PS4.getY(i));
140 Serial.print(F("\t"));