Código: Seleccionar todo
#include "PS2X_lib.h" // para la v1.6
#include "Servo.h"
PS2X ps2x; // Creamos un Controlador clase PS2
Servo UpLServo; // Creamos un objeto ESC arriba izquierda
Servo UpRServo; // Creamos un objeto ESC arriba derecha
Servo HorizLServo; // Creamos un objeto ESC horizontal izquierda
Servo HorizRServo; // Creamos un objeto ESC horizontal derecha
Servo CamServo; // Creamos un objeto servo para el pitch de la camara
const int LED_PIN_1 = 30; // Creamos un LED Verde en el pin 30
boolean ledState; // Guarda el estado del LED on/of
int ForwardVal = 0; // valor leído en palo PS2 derecho arriba/abajo
int YawLeftVal = 0; // valor leído en palo PS2 derecho derecha/izquierda
int UpVal = 0; // valor leído en palo PS2 izquierdo arriba/abajo
int RollLeftVal = 0; // valor leído en palo PS2 izquierdo derecha/izquierda
int CamPitch = 90;
int upLraw = 0; // Variables para transportar el valor actual de los datos a los ESC
int upRraw = 0;
int HLraw = 0;
int HRraw = 0;
int CSraw = 0;
int auxMillis = 0;
int intervaloEncendido = 1000;
void setup() {
Serial.begin(9600);
Serial.flush(); // Depura la informacion del puerto Serie
// Configuracion de los pines y ajustes: GamePad (clock, command, attention, data, Pressures?, Rumble?) verifica el error
ps2x.config_gamepad(22, 23, 24, 25, true, true);
pinMode(LED_PIN_1, OUTPUT); // Establece el pin del LED como salida
UpLServo.attach(32); // El servo arriba izquierda ataca en el pin 32
UpRServo.attach(34); // El servo arriba derecha ataca en el pin 34
HorizLServo.attach(36); // El servo horizontal izquierda ataca en el pin 36
HorizRServo.attach(38); // El servo horizontal derecha ataca en el pin 38
CamServo.attach(40); // El servo del pitch de la camara ataca en el pi 40
delay (1000);
}
void loop() {
if ( millis() - auxMillis > intervaloEncendido) {
ps2x.read_gamepad(); // Lee el controlador
bool ButtonPressed; // Sera verdad cuando se presione el botón, 1 vez ON / 1 vez OF
bool Button; // Sera verdad mientras se mantenga el boton presionado
if (ps2x.Button(PSB_START)) { // Sera verdadero cuando se presione el botón
Serial.println(F("\n Botón Start esta siendo presionado"));
}
if (ps2x.Button(PSB_SELECT)) {
Serial.println(F("\n Botón Select esta siendo presionado"));
}
ButtonPressed = ps2x.ButtonPressed(PSB_PAD_UP);
if ( ButtonPressed == true)
{
Serial.println(F("\n Botón izquierda arriba presionado:"));
if (ledState == true)
{
digitalWrite(LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_PAD_DOWN);
if (ButtonPressed == true)
{
Serial.println(F("\n Botón izquierda abajo presinado:"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite(LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_PAD_RIGHT);
if (ButtonPressed == true)
{
Serial.println(F("\n Botón izquierda derecha presionado:"));
if (ledState == true)
{
digitalWrite(LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_PAD_LEFT);
if (ButtonPressed == true)
{
Serial.println(F("\n Botón Izquierda Izquierda presionado:"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_GREEN);
if (ButtonPressed == true)
{
Serial.println(F("\n Triangulo presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_RED);
if (ButtonPressed == true)
{
Serial.println(F("\n Circulo presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_PINK);
if (ButtonPressed == true)
{
Serial.println(F("\n Cuadrado presinado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_BLUE);
if (ButtonPressed == true)
{
Serial.println(F("\n X presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_L1);
if (ButtonPressed == true)
{
Serial.println(F("\n L1 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_L2);
if (ButtonPressed == true)
{
Serial.println(F("\n L2 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_L3);
if (ButtonPressed == true)
{
Serial.println(F("\n L3 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
Button = ps2x.Button(PSB_R1);
if (Button == true)
{
CamPitch = CamPitch + 1;
Serial.println(F("\n R1 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
Button = ps2x.Button(PSB_R2);
if (Button == true)
{
CamPitch = CamPitch - 1;
Serial.println(F("\n R2 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ButtonPressed = ps2x.ButtonPressed(PSB_R3);
if (ButtonPressed == true)
{
Serial.println(F("\n R3 presionado"));
if (ledState == true)
{
digitalWrite (LED_PIN_1, HIGH);
}
else
{
digitalWrite (LED_PIN_1, LOW);
}
}
ForwardVal = ps2x.Analog(PSS_RY);
YawLeftVal = ps2x.Analog(PSS_RX);
UpVal = ps2x.Analog(PSS_LY);
RollLeftVal = ps2x.Analog(PSS_LX);
upLraw = (128 - UpVal) + (128 - RollLeftVal) / 2; // esto será hasta un valor de 192
upRraw = (128 - UpVal) - (128 - RollLeftVal) / 2; // esto será hasta un valor de 192
HLraw = (128 - ForwardVal) + (128 - YawLeftVal); // esto será hasta un valor de 256
HRraw = (128 - ForwardVal) - (128 - YawLeftVal); // esto será hasta un valor de 256
// Escala los valores para que sean adecuados para los ESC y los servos
upLraw = map(upLraw, -193, 193, 0, 179);
upRraw = map(upRraw, -193, 198, 0, 179);
HLraw = map(HLraw, -256, 256, 0, 179);
HRraw = map(HRraw, -256, 256, 0, 179);
// escribe el valor 0 en los servos
UpLServo.write(upLraw);
UpRServo.write(upRraw);
HorizLServo.write(HLraw);
HorizRServo.write(HRraw);
CamPitch = constrain(CamPitch, 30, 149); // mantiene el servo de la camara dentro del limite de 30 - 149
CamServo.write(CamPitch); // Mueve el servo de la camara
digitalWrite(LED_PIN_1, ledState); // Enciende el Led Verde
}
}