Conexión Arduino PS3 USB
Conexión Arduino PS3 USB
com/arduino-en/usb-shield-arduino-servo-control-ps3-2/
The USB port is perhaps one of the ports most used, thousands of devices work through
a USB port, but we are accustomed to external devices using this port and we think that
can only be connected to a desktop PC or laptop, but there are cards that allow us to
perform the same function of a PC and control devices using this port and this way you
can create applications which can connect an external device such as control PS3,
mouse, keyboard, camera, code scanner, a cellphone, among many others.
This is the case of the USB module for Arduino projects, known as USB Host Shield.
It is called the host, because acts as a master controller.
How to connect an USB Shield Arduino and PS3
control to move servo?
Assembled with a development board Arduino Uno or Arduino Mega, you can control
external devices and conduct applications that allow to interact with them.
This time we will have a very simple test, making the management of a servo motor
through a PS3 control.
The library that we will use for this test you found in the following LINK and should
download it and add in Arduino programming software libraries file.
Having done that, we add the library to control the servo motor and the instructions
that will move the servo to left and right.
In the video below we show step by step where adding instructions and how to test the
PS3 controller with the servo.
The following links are to download the complete code to control servo motor and the
link to download the USB Host Shield library:
USB Usb;
/* You can create the instance of the class in two ways */
PS3USB PS3(&Usb); // This will just create the instance
//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
bool printAngle;
uint8_t state = 0;
void setup() {
Serial.begin(9600);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo,
Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); //halt
}
Serial.print(F("\r\nPS3 USB Library Started"));
}
void loop() {
Usb.Task();
if (PS3.PS3Connected || PS3.PS3NavigationConnected) {
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\nStickIzquierdoX: "));
Serial.print(PS3.getAnalogHat(LeftHatX));
Serial.print(F("\tLStickIzquierdoY: "));
Serial.print(PS3.getAnalogHat(LeftHatY));
if (PS3.PS3Connected) { // The Navigation controller only have one
joystick
Serial.print(F("\tStickDerechoX: "));
Serial.print(PS3.getAnalogHat(RightHatX));
Serial.print(F("\tStickDerechoY: "));
Serial.print(PS3.getAnalogHat(RightHatY));
}
}
// Analog button values can be read from almost all buttons
if (PS3.getAnalogButton(L2) || PS3.getAnalogButton(R2)) {
Serial.print(F("\r\nBotonIzquierdo2: "));
Serial.print(PS3.getAnalogButton(L2));
if (!PS3.PS3NavigationConnected) {
Serial.print(F("\tBotonDerecho2: "));
Serial.print(PS3.getAnalogButton(R2));
}
}
if (PS3.getButtonClick(PS))
Serial.print(F("\r\nBotonPS"));
if (PS3.getButtonClick(TRIANGLE))
Serial.print(F("\r\nTriangulo"));
if (PS3.getButtonClick(CIRCLE))
Serial.print(F("\r\nCirculo"));
if (PS3.getButtonClick(CROSS))
Serial.print(F("\r\nAspa"));
if (PS3.getButtonClick(SQUARE))
Serial.print(F("\r\nCuadrado"));
if (PS3.getButtonClick(UP)) {
Serial.print(F("\r\nArriba"));
PS3.setLedOff();
PS3.setLedOn(LED4);
}
if (PS3.getButtonClick(RIGHT)) {
Serial.print(F("\r\nDerecha"));
PS3.setLedOff();
PS3.setLedOn(LED1);
}
if (PS3.getButtonClick(DOWN)) {
Serial.print(F("\r\nAbajo"));
PS3.setLedOff();
PS3.setLedOn(LED2);
}
if (PS3.getButtonClick(LEFT)) {
Serial.print(F("\r\nIzquierda"));
PS3.setLedOff();
PS3.setLedOn(LED3);
}
if (PS3.getButtonClick(L1))
Serial.print(F("\r\nL1"));
if (PS3.getButtonClick(L3))
Serial.print(F("\r\nBoton L3"));
if (PS3.getButtonClick(R1))
Serial.print(F("\r\nR1"));
if (PS3.getButtonClick(R3))
Serial.print(F("\r\nBoton R3"));
if (PS3.getButtonClick(SELECT)) {
Serial.print(F("\r\nBoton Select - "));
PS3.printStatusString();
}
if (PS3.getButtonClick(START)) {
Serial.print(F("\r\nBoton Start"));
printAngle = !printAngle;
}
if (printAngle) {
Serial.print(F("\r\nAngulo Pitch: "));
Serial.print(PS3.getAngle(Pitch));
Serial.print(F("\tAngulo Roll: "));
Serial.print(PS3.getAngle(Roll));
}
}
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
if (state == 0) {
PS3.moveSetRumble(0);
PS3.moveSetBulb(Off);
} else if (state == 1) {
PS3.moveSetRumble(75);
PS3.moveSetBulb(Red);
} else if (state == 2) {
PS3.moveSetRumble(125);
PS3.moveSetBulb(Green);
} else if (state == 3) {
PS3.moveSetRumble(150);
PS3.moveSetBulb(Blue);
} else if (state == 4) {
PS3.moveSetRumble(175);
PS3.moveSetBulb(Yellow);
} else if (state == 5) {
PS3.moveSetRumble(200);
PS3.moveSetBulb(Lightblue);
} else if (state == 6) {
PS3.moveSetRumble(225);
PS3.moveSetBulb(Purble);
} else if (state == 7) {
PS3.moveSetRumble(250);
PS3.moveSetBulb(White);
}
state++;
if (state > 7)
state = 0;
delay(1000);
}
}