2 Example sketch for the PS3 USB 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
9 // Satisfy the IDE, which needs to see the include statment in the ino too.
11 #include <spi4teensy3.h>
16 /* You can create the instance of the class in two ways */
17 PS3USB PS3(&Usb); // This will just create the instance
18 //PS3USB PS3(&Usb,0x00,0x15,0x83,0x3D,0x0A,0x57); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
25 #if !defined(__MIPSEL__)
26 while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
28 if (Usb.Init() == -1) {
29 Serial.print(F("\r\nOSC did not start"));
32 Serial.print(F("\r\nPS3 USB Library Started"));
37 if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
38 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) {
39 Serial.print(F("\r\nLeftHatX: "));
40 Serial.print(PS3.getAnalogHat(LeftHatX));
41 Serial.print(F("\tLeftHatY: "));
42 Serial.print(PS3.getAnalogHat(LeftHatY));
43 if (PS3.PS3Connected) { // The Navigation controller only have one joystick
44 Serial.print(F("\tRightHatX: "));
45 Serial.print(PS3.getAnalogHat(RightHatX));
46 Serial.print(F("\tRightHatY: "));
47 Serial.print(PS3.getAnalogHat(RightHatY));
50 // Analog button values can be read from almost all buttons
51 if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
52 Serial.print(F("\r\nL2: "));
53 Serial.print(PS3.getAnalogButton(L2));
54 if (!PS3.PS3NavigationConnected) {
55 Serial.print(F("\tR2: "));
56 Serial.print(PS3.getAnalogButton(R2));
59 if (PS3.getButtonClick(PS))
60 Serial.print(F("\r\nPS"));
62 if (PS3.getButtonClick(TRIANGLE))
63 Serial.print(F("\r\nTraingle"));
64 if (PS3.getButtonClick(CIRCLE))
65 Serial.print(F("\r\nCircle"));
66 if (PS3.getButtonClick(CROSS))
67 Serial.print(F("\r\nCross"));
68 if (PS3.getButtonClick(SQUARE))
69 Serial.print(F("\r\nSquare"));
71 if (PS3.getButtonClick(UP)) {
72 Serial.print(F("\r\nUp"));
76 if (PS3.getButtonClick(RIGHT)) {
77 Serial.print(F("\r\nRight"));
81 if (PS3.getButtonClick(DOWN)) {
82 Serial.print(F("\r\nDown"));
86 if (PS3.getButtonClick(LEFT)) {
87 Serial.print(F("\r\nLeft"));
92 if (PS3.getButtonClick(L1))
93 Serial.print(F("\r\nL1"));
94 if (PS3.getButtonClick(L3))
95 Serial.print(F("\r\nL3"));
96 if (PS3.getButtonClick(R1))
97 Serial.print(F("\r\nR1"));
98 if (PS3.getButtonClick(R3))
99 Serial.print(F("\r\nR3"));
101 if (PS3.getButtonClick(SELECT)) {
102 Serial.print(F("\r\nSelect - "));
103 PS3.printStatusString();
105 if (PS3.getButtonClick(START)) {
106 Serial.print(F("\r\nStart"));
107 printAngle = !printAngle;
110 Serial.print(F("\r\nPitch: "));
111 Serial.print(PS3.getAngle(Pitch));
112 Serial.print(F("\tRoll: "));
113 Serial.print(PS3.getAngle(Roll));
116 else if (PS3.PS3MoveConnected) { // One can only set the color of the bulb, set the rumble, set and get the bluetooth address and calibrate the magnetometer via USB
118 PS3.moveSetRumble(0);
119 PS3.moveSetBulb(Off);
120 } else if (state == 1) {
121 PS3.moveSetRumble(75);
122 PS3.moveSetBulb(Red);
123 } else if (state == 2) {
124 PS3.moveSetRumble(125);
125 PS3.moveSetBulb(Green);
126 } else if (state == 3) {
127 PS3.moveSetRumble(150);
128 PS3.moveSetBulb(Blue);
129 } else if (state == 4) {
130 PS3.moveSetRumble(175);
131 PS3.moveSetBulb(Yellow);
132 } else if (state == 5) {
133 PS3.moveSetRumble(200);
134 PS3.moveSetBulb(Lightblue);
135 } else if (state == 6) {
136 PS3.moveSetRumble(225);
137 PS3.moveSetBulb(Purble);
138 } else if (state == 7) {
139 PS3.moveSetRumble(250);
140 PS3.moveSetBulb(White);