I'm using a USB Host Shield 2.0 from:
http://www.circuitsathome.com/products-page/arduino-shields/usb-host-shield-2-0-for-arduino. I will be minimizing from the Uno and the USB Host in the near future.
I started with the USB Host library:
https://github.com/felis/USB_Host_Shield_2.0 and
http://barrettsprojects.wordpress.com/2013/01/13/driving-an-rc-car-with-a-ps3-controller/.
Here is my work in progress. It is throttle only at this time. I'm coding an activation sequence next, before adding lights. You will have to clean up the code yourself, but throttle is functional. I want the throttle settings just right before coding add-ons. Caveat: reverse cuts out at max, forward is beautiful.
#include <PS3BT.h>
#include <Servo.h>
USB Usb;
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,0x1F,0x81,0x00,0x08,0x30); // This will also store the bluetooth address - this can be obtained from the dongle when running the sketch
Servo servoL; // create servo object, max 8
Servo servoR;
int leftHatX, leftHatY, analogL2;
int speedL, speedR;
//int minSpeed = 180; // vex motor servo values for min and max speed
int zeroSpeed = 90;
//int maxSpeed = 0;
//int minPS3 = 0; // ps3 controller range of values returned
//int maxPS3 = 255;
//long deltaSpeed = maxSpeed - minSpeed;
//long deltaPS3 = maxPS3 - minPS3;
//int ledRed=2, ledGreen=4; // status of bluetooth, green = connected
int ps3connected = 0; // status of bluetooth, 1 = connected
void setup() {
Serial.begin(9600);
servoL.attach(5,741,2400); // attaches the servo on pin 5 to the servo object
servoR.attach(6);
//pinMode(ledRed,OUTPUT);
//pinMode(ledGreen,OUTPUT);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
//digitalWrite(ledGreen,LOW);
//digitalWrite(ledRed,HIGH);
}
void loop() {
Usb.Task();
if(PS3.PS3Connected || PS3.PS3NavigationConnected) {
if(ps3connected==0) {
//digitalWrite(ledGreen,HIGH);
//digitalWrite(ledRed,LOW);
ps3connected=1;
}
// =================================================
//leftHatX = PS3.getAnalogHat(LeftHatX);
leftHatY = PS3.getAnalogHat(LeftHatY);
//rightHatX = PS3.getAnalogHat(RightHatX);
//rightHatY = PS3.getAnalogHat(RightHatY);
//analogL2 = PS3.getAnalogButton(L2);
if( leftHatY < 117 || leftHatY >137 ) {
//Serial.print("\n");
//speedR= leftHatY*deltaSpeed/deltaPS3+minSpeed;
//Serial.print(leftHatY);
//Serial.print(" ");
//Serial.print(speedR);
//servoR.write(speedR);
Serial.print("\n");
speedL = map(leftHatY, 0, 255, 2400, 741);
Serial.print(leftHatY);
Serial.print(" ");
Serial.print(speedL);
servoL.write(speedL);
} else {
servoL.write(zeroSpeed);
//servoR.write(zeroSpeed);
}
// =================================================
//// analog buttons - L2 and R2
//if(PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
//Serial.print(F("\r\nL2: "));
//Serial.print(PS3.getAnalogButton(L2));
//if(!PS3.PS3NavigationConnected) {
//Serial.print(F("\tR2: "));
//Serial.print(PS3.getAnalogButton(R2));
//}
}
// =================================================
// digital buttons
// disconnect on PS button press
if(PS3.getButtonClick(PS)) {
Serial.print(F("\r\nPS"));
PS3.disconnect();
//digitalWrite(ledGreen,LOW);
//digitalWrite(ledRed,HIGH);
ps3connected=0;
}
//}
}