Home
About
Blog
Media Gallery

DJ 6S Manual


The code for my custom Logitech Driving Force, converted to standalone USB joystick device. The 2 internal potentiometers were connected to an Arduino Micro board, as well as the switch to detect reverse position.

MHeironimus/ArduinoJoystickLibrary
Media Gallery

Arduino IDE
#include <Joystick.h>

// 6 gir + revers.
int AntallKnapper = 7;

// Potmetere på stikke.
int X = A0;
int Y = A1;

// Grenser lest ut via konsoll debugging.
int GearPositionsXY[6][2] = {
  {291, 924}, // 1st
  {288, 112}, // 2nd
  {503, 926}, // 3rd
  {505, 101}, // 4th
  {699, 945}, // 5th
  {704, 113}  // 6th & R
};
int PositionMargin = 50;

Joystick_ Joystick(
  0x03,   // hidReportId
  0x04,   // joystickType
  AntallKnapper,     // buttonCount
  0,      // hatSwitchCount
  false,  // includeXAxis
  false,  // includeYAxis 
  false,  // includeZAxis
  false,  // includeRxAxis
  false,  // includeRyAxis
  false,  // includeRzAxis
  false,  // includeRudder
  false,  // includeThrottle
  false,  // includeAccelerator
  false,  // includeBrake
  false   // includeSteering
);

void setup() {
  // Bryter i bunn av stikke som avgjør om den er 
  // trykket ned og skal aktivere reverse i HØYRE-NED.
  pinMode(2, INPUT); 
  Joystick.begin();

  /*
  // DEBUGGING
  Serial.begin(9600);
  */
}

void loop() {

  // Revers. Denne har en pull-down resistor siden 
  // stikka bryter oransj mot 5V og tar den HIGH.
  if (
    analogRead(X) >= (GearPositionsXY[5][0]-PositionMargin) && 
    analogRead(Y) <= (GearPositionsXY[5][1]+PositionMargin) &&
    digitalRead(2) == HIGH
  )
    Joystick.setButton(0, 1);
  else
    Joystick.setButton(0, 0);



  // 1st
  if (
    analogRead(X) <= (GearPositionsXY[0][0]+PositionMargin) && 
    analogRead(Y) >= (GearPositionsXY[0][1]-PositionMargin)
  )
    Joystick.setButton(1, 1);
  else
    Joystick.setButton(1, 0);



  // 2nd
  if (
    analogRead(X) <= (GearPositionsXY[1][0]+PositionMargin) && 
    analogRead(Y) <= (GearPositionsXY[1][1]+PositionMargin)
  )
    Joystick.setButton(2, 1);
  else
    Joystick.setButton(2, 0);



  // 3rd
  if (
    analogRead(X) <= (GearPositionsXY[2][0]+PositionMargin) && 
    analogRead(X) >= (GearPositionsXY[2][0]-PositionMargin) && // Rundt 500 et sted.
    analogRead(Y) >= (GearPositionsXY[2][1]-PositionMargin)
  )
    Joystick.setButton(3, 1);
  else
    Joystick.setButton(3, 0);



  // 4th
  if (
    analogRead(X) <= (GearPositionsXY[3][0]+PositionMargin) && 
    analogRead(X) >= (GearPositionsXY[3][0]-PositionMargin) && // Rundt 500 et sted.
    analogRead(Y) <= (GearPositionsXY[3][1]+PositionMargin)
  )
    Joystick.setButton(4, 1);
  else
    Joystick.setButton(4, 0);



  // 5th
  if (
    analogRead(X) >= (GearPositionsXY[4][0]-PositionMargin) && 
    analogRead(Y) >= (GearPositionsXY[4][1]-PositionMargin)
  )
    Joystick.setButton(5, 1);
  else
    Joystick.setButton(5, 0);



  // 6th
  if (
    analogRead(X) >= (GearPositionsXY[5][0]-PositionMargin) && 
    analogRead(Y) <= (GearPositionsXY[5][1]+PositionMargin) && 
    digitalRead(2) == LOW
  )
    Joystick.setButton(6, 1);
  else
    Joystick.setButton(6, 0);

  /*
  // DEBUGGING
  // Les ut X og Y akse for å debugge girposisjonsverdier.
  Serial.print("X akse: ");
  Serial.print(analogRead(X));
  Serial.print(", Y akse: ");
  Serial.println(analogRead(Y));
  */

  delay(10);
}


Original Post: Feb 2nd, '22 18:37 CET.

Tags: Arduino