Hola a todos,
Primero de todo tengo que decir que no soy desarrollador, mis nociones de programación son muy básicas y creo este hilo para ver si alguien me puede ayudar.
Vamos al grano, me han regalado un antiguo Joystick Saitek que utiliza puerto gameport de 15 pines, mi idea es hacerlo funcionar usando un arduino (Vinciduino en mi caso).
Es un hardware bastante simple...
- 3 potenciómetros para los ejes X,Y y el acelerador
- 4 botones que son 4 switches
- una palanca de 4 direcciones que son otros 4 switches
Investigando he dado con la librería "ArduinoJoystickLibrary"
He conseguido hacer funcionar los 4 botones y los 3 ejes, pero soy incapaz de comprender como hacer funcional la pequeña palanca de 4 direcciones.
La pequeña palanca que tiene 4 switches funciona de la siguiente manera..
Cuando pulso la palanca hacia arriba por ejemplo, lo que veo en las propiedades del controlador de windows, es que se presionan los 4 botones al mismo tiempo, y si pulso hacia abajo, se presionan los botones 1,2 y 3 al mismo tiempo
Pongo aquí como funcionan las cuatro direcciones...
Dirección --- Botones
Arriba ---> 1,2,3,4
Abajo ---> 1,2,3
Izquierda ---> 1,3
Derecha ---> 1,3,4
Por mas que busco no doy con la solución, como he dicho antes, programar no es lo mío, agradecería mucho que alguien me eche una mano.
Aqui el codigo que estoy utilizando:
// Saitek Cyborg 2000 Gameport to USB
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//------------------------------------------------------------
// PC Gameport (Joystick) pinout
// 1 +5V --> +5 VDC
// 2 /B1 <-- Button 1
// 3 X1 <-- Joystick 1 - X
// 4 GND --- Ground (for switch 1)
// 5 GND --- Ground (for switch 2)
// 6 Y1 <-- Joystick 1 - Y
// 7 /B2 <-- Button 2
// 8 +5V --> +5 VDC
// 9 +5V --> +5 VDC
// 10 /B3 <-- Button 3
// 11 X2 <-- Joystick 2 - X
// 12 GND --- Ground (for switch 3,4)
// 13 Y2 <-- Joystick 2 - Y
// 14 /B4 <-- Button 4
// 15 +5V --> +5 VDC or N/C
//change these to define which pins your potentiometers are connected.
//to change button connections, scroll down to loop()
#define X_PIN A0
#define Y_PIN A1
#define T_PIN A2
//change these to change trim and limits. Calibrating your joystick in Windows achieves the same thing
#define X_MIN 0
#define X_MAX 1023
#define X_TRIM 0
#define X_INVERT -1
//to invert an axis, change 1 to -1
#define Y_MIN 0
#define Y_MAX 1023
#define Y_TRIM 0
#define Y_INVERT -1
#define T_MIN 0
#define T_MAX 1023
#define T_TRIM 0
#define T_INVERT 1
#include <Joystick.h>
Joystick_ Joystick(0x04,JOYSTICK_TYPE_JOYSTICK,
4, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, true, // Yes rudder, yes throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
// Initialize Button Pins
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
//pinMode(6, INPUT_PULLUP);
//pinMode(7, INPUT_PULLUP);
//pinMode(8, INPUT_PULLUP);
//pinMode(9, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin(false); //false = dont send automatically. We will sendState() at the end of the loop
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
Joystick.setThrottleRange(-512, 512);
}
void loop() {
//read buttons. Change pins and button numbers here, if you want to have different number connected to different pin
Joystick.setButton(0, !digitalRead(2)); //pin 2 LOW means button 0 PRESSED
Joystick.setButton(1, !digitalRead(4)); //etc.
Joystick.setButton(2, !digitalRead(3));
Joystick.setButton(3, !digitalRead(5));
//Joystick.setButton(4, !digitalRead(7));
//Joystick.setButton(5, !digitalRead(6));
//Joystick.setButton(6, !digitalRead(8));
//Joystick.setButton(7, !digitalRead(9));
//read analog axes
int value = map(analogRead(X_PIN) + X_TRIM , X_MIN, X_MAX, -512, 512) * X_INVERT;
Joystick.setXAxis(value);
value = map(analogRead(Y_PIN) + Y_TRIM , Y_MIN, Y_MAX, -512, 512) *Y_INVERT;
Joystick.setYAxis(value);
value = map(analogRead(T_PIN) + T_TRIM , T_MIN, T_MAX, -512, 512) * T_INVERT;
Joystick.setThrottle(value);
Joystick.sendState();
delay(10);
}
Tengo comentados 4 botones extra, porque en un principio pensé que serian botones independientes, pero no ha sido así y he terminado atascado en este punto.
Si me ha faltado poner algo mas de información, decídmelo y lo añado.
Saludos