Skip to content

Instantly share code, notes, and snippets.

@robo8080
Created August 11, 2017 12:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robo8080/66c56148c548e9e7d81a432f725590fc to your computer and use it in GitHub Desktop.
Save robo8080/66c56148c548e9e7d81a432f725590fc 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
//Arduino PWM Speed Control:
int E1 = 5;
int M1 = 4;
int E2 = 6;
int M2 = 7;
int value1 = 0;
int value2 = 0;
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() {
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
analogWrite(E1, value1); //PWM Speed Control
analogWrite(E2, value2); //PWM Speed Control
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));
value1 = (int)((((float)PS3.getAnalogHat(LeftHatY) - 128.0f)/128.0f)*255.0f);
value2 = (int)((((float)PS3.getAnalogHat(RightHatY) - 128.0f)/128.0f)*255.0f);
Serial.print(F("\tvalue1: "));
Serial.print(value1);
Serial.print(F("\tvalue2: "));
Serial.print(value2);
}
else {
value1 = 0;
value2 = 0;
}
if(value1 >= 0) {
digitalWrite(M1,HIGH);
} else {
digitalWrite(M1,LOW);
value1 *= -1;
}
if(value2 >= 0) {
digitalWrite(M2,HIGH);
} else {
digitalWrite(M2,LOW);
value2 *= -1;
}
analogWrite(E1, value1); //PWM Speed Control
analogWrite(E2, value2); //PWM Speed Control
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