Skip to content

Instantly share code, notes, and snippets.

@robo8080
Last active November 10, 2017 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robo8080/4603a1dc678cdf1cdc2ef66ff449fb70 to your computer and use it in GitHub Desktop.
Save robo8080/4603a1dc678cdf1cdc2ef66ff449fb70 to your computer and use it in GitHub Desktop.
/*
Example sketch for the PS3 Bluetooth library - developed by Kristian Lauszus
For more information visit my blog: http://blog.tkjelectronics.dk/ or
send me an e-mail: kristianl@tkjelectronics.com
*/
#include <PS3BT.h>
#include <usbhub.h>
// Satisfy IDE, which only needs to see the include statment in the ino.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <Servo.h>
Servo myservo; // create servo object to control a servo
Servo myservo1; // create servo object to control a servo
int pos = 90; // variable to store the servo position
int pos1 = 90; // variable to store the servo position
USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the class in two ways */
PS3BT PS3(&Btd); // This will just create the instance
//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
void setup() {
myservo.attach(5); // attaches the servo on pin 5 to the servo object
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo1.attach(9); // attaches the servo on pin 9 to the servo object
myservo1.write(pos1); // tell servo to go to position in variable 'pos'
Serial.begin(115200);
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}
void loop() {
Usb.Task();
if (PS3.PS3Connected) {
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) {
Serial.print(F("\r\nLeftHatX: "));
Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLeftHatY: "));
Serial.print(PS3.getAnalogHat(LeftHatY));
Serial.print(F("\tRightHatX: "));
Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tRightHatY: "));
Serial.print(PS3.getAnalogHat(RightHatY));
pos = (int)( ( (float)PS3.getAnalogHat(RightHatY)/255.0f )*180.0f);
pos1 = (int)( ( (float)PS3.getAnalogHat(RightHatX)/255.0f )*180.0f);
}
else {
pos = 90;
pos1 = 90;
}
myservo.write(pos); // tell servo to go to position in variable 'pos'
myservo1.write(pos1); // tell servo to go to position in variable 'pos1'
if (PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS"));
PS3.disconnect();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment