Home
About
Blog
Media Gallery

DJ DNR Real Automatic


The code for my real automatic controllers for truck sim. 6 push (temporary ON) buttons on V1 and a real device as V2 connected to a programmed Arduino Micro.

MHeironimus/ArduinoJoystickLibrary
Media Gallery

Arduino IDE for DNR V1
(6 push buttons with temporary ON states)
#include <Joystick.h>

// GND til digitale pins 2-12 for knappetrykk.
int AntallKnapper = 6;

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() {
  for (int n=2; n<=AntallKnapper+1; n++) {
    pinMode(n, INPUT_PULLUP);
  }
  Joystick.begin();
}

int LastButtonStates[11] = {0};
int TmpButtonState = 0;

void loop() {
  TmpButtonState = 0;

  for (int n=2; n<=AntallKnapper+1; n++) {
    TmpButtonState = !digitalRead(n);
    if (TmpButtonState != LastButtonStates[n-2]) {

      // Hold R D aktivert hvis de blir trykket på.
      if (TmpButtonState == 0 && (n==2 || n==6)) {
        TmpButtonState = LastButtonStates[n-2];
      }

      // Overstyr R D aktive statuser hvis R N D blir trykket.
      if (TmpButtonState != 0 && n==2) {
        Joystick.setButton(6-2, 0);
        LastButtonStates[6-2] = 0;
      }
      if (TmpButtonState != 0 && n==6) {
        Joystick.setButton(2-2, 0);
        LastButtonStates[2-2] = 0;
      }
      if (TmpButtonState != 0 && n==7) {
        Joystick.setButton(2-2, 0);
        Joystick.setButton(6-2, 0);
        LastButtonStates[2-2] = 0;
        LastButtonStates[6-2] = 0;
      }

      // Oppdater knappestatus.
      Joystick.setButton(n-2, TmpButtonState);
      LastButtonStates[n-2] = TmpButtonState;
    }
  }

  delay(50);
}


Arduino IDE for DNR V2
(simpler, because positions are held)
#include <Joystick.h>

int AntallKnapper = 2;

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() {
  pinMode(2, INPUT);  
  pinMode(3, INPUT); 
  
  Joystick.begin();
}

void loop() {
  Joystick.setButton(0, digitalRead(2));
  Joystick.setButton(1, digitalRead(3));
  
  delay(50);
}


Original Post: Feb 2nd, '22 18:42 CET.
Updated: Feb 2nd, '22 18:45 CET.

Tags: Arduino