아두이노 조이스틱 (Arduino Joystick For X-wing Alliance)

X-wing 얼라이언스가 스팀으로 발매되었지만 게임패드가 없으면 실행도 안되고 조이스틱이 없으면 실질적으로 진행이 불가능하다.

그래서

만들것이다.

  • 아날로그 조이스틱 → USB조이스틱
  • 버튼 6개 추가 (스틱에 2개, 본체에 4개)
  • 쓰로틀 추가

준비물

배선

  • 노랑 - 그라운드
  • 빨강 - 단추1
  • 주황 - 2
  • 보라 - 3
  • 회색 - 4

저항연결법

버튼연결법

저항없이 버튼 연결하는 방법

f2nszumibl629k9.medium.jpg

초기화를 다르게 해줘야 한다.

코드

/*
  Wingman Extreme Analog Joystick Modification
 
   Arduino Leonado or micro is needed
 
  // http://www.instructables.com/id/Arduino-Button-with-no-resistor/?ALLSTEPS
 
   Note: on most Arduinos there is already an LED on the board
  attached to pin 13.
 
*/
 
#include "Joystick.h"
// https://github.com/MHeironimus/ArduinoJoystickLibrary
 
// Create Joystick
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
                   JOYSTICK_TYPE_JOYSTICK, 10, 1,
                   true, true, false, false, false, false,
                   false, true, false, false, false);
/*
  uint8_t hidReportId,   uint8_t joystickType,  uint8_t buttonCount,  uint8_t hatSwitchCount,
  bool includeXAxis,  bool includeYAxis,  bool includeZAxis,  bool includeRxAxis,  bool includeRyAxis,  bool includeRzAxis,
  bool includeRudder, bool includeThrottle,  bool includeAccelerator,  bool includeBrake,  bool includeSteering
*/
 
// constants won't change. They're used here to
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
const int hatSwitch = 0;
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
 
int xAxis;
int yAxis;
int hatRead;
 
void setup() {
 
  // Set Range Values
  Joystick.setXAxisRange(-127, 127);
  Joystick.setYAxisRange(-127, 127);
 
  Joystick.begin();
 
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  for (int button = 0; button < 10; button++)
  {
    pinMode(button, INPUT_PULLUP);
  }
}
 
void loop() {
 
  /*
     analog axis
  */
  xAxis = (analogRead(A0) - 512) / 4;
  yAxis = (analogRead(A1) - 512) / 4;
  Joystick.setXAxis(xAxis);
  Joystick.setYAxis(yAxis);
 
  /*
      Throttle
  */
  Joystick.setThrottle (analogRead(A2)) ;
 
  /*
     Hat switch
     http://www.epanorama.net/documents/joystick/pc_joystick.html#connector
  */
  int hatRead = analogRead(A3);
 
  // not pressed : 82kohm 107-108
  // 0 : 200ohm 1003
  // 90 : 20kohm 333-334
  // 180 : 41kohm 195-196
  // 270 : 61kohm 140-141
 
  if ((hatRead > 900)  ) {
    Joystick.setHatSwitch(hatSwitch, 0);
  }
  if ((hatRead > 300) && (hatRead < 360)) {
    Joystick.setHatSwitch(hatSwitch, 90);
  }
  if ((hatRead > 180) && (hatRead < 220)) {
    Joystick.setHatSwitch(hatSwitch, 180);
  }
  if ((hatRead > 130) && (hatRead < 160)) {
    Joystick.setHatSwitch(hatSwitch, 270);
  }
  if (hatRead < 120) {
    Joystick.setHatSwitch(hatSwitch, -1);
  }
 
  /*
     Buttons
  */
 
  // read the state of pins:
  for (int i = 0; i < 10; i++) {
 
    // check if the pushbutton is pressed.
    // http://www.instructables.com/id/Arduino-Button-with-no-resistor/?ALLSTEPS
    buttonState = digitalRead(i);
    if ((buttonState == LOW)  ) {
      Joystick.pressButton(i  );
      // turn LED on:
      digitalWrite(ledPin, HIGH);
    } else {
      Joystick.releaseButton(i );
      // turn LED off:
      digitalWrite(ledPin, LOW);
    }
  }
 
}

미세조정 하는 방법