Porsche RSR 

KA-Race-Team #Nürburgring

Projekte

G25 shifter USB Adapter mit einem Arduino Pro Micro

Nach einem Umstieg auf ein neues Lenkrad lag mein G25 Shifter unnütz in der Ecke... Klar es gibt fertige Adapter zu kaufen, allerdings sind die Versandkosten aus dem Ausland oft etwas hoch und somit entschied ich mich das Ganze mit einem Arduino zu realisiseren.

 

Ich habe lange gesucht und immer nur Lösungen mit dem Teensy gefunden. Der ist in meinen Augen nur unnötig teurer.. 

 

 

 

hier mein Arduino Sketch:

 

// G25 shifter USB Adapter powered by Arduino Pro Micro without handbrake mode

//  G25 shifter pinout
//
//  DB9  Color    Shifter Description       
//  1    Black           +5V               
//  2    Grey            Button Data       
//  3    Yellow          Button !CS & !PL  
//  4    Orange          Shifter X axis     (A0)
//  5    Red             SPI input 
//  6    White           GND               
//  7    Purple          Button Clock      
//  8    Green           Shifter Y axis    (A1)
//  9    Black           +5V

// Gear definitions
// H Shifter
// gear = 0       neutral
// gear = 1       first gear
// gear = 2       second gear
// gear = 3       third gear
// gear = 4       fourth gear
// gear = 5       fifth gear
// gear = 6       sixth gear
// gear = 99      reverse gear
// 
// Sequential shifter
// gear = 7       upshift
// gear = 8       downshift
// gear = 88      no shift

 

#include <Joystick.h>
Joystick_ Joystick;

int Mode = 10;      //A10 PWM
int Clock = 15;     //SCLK
int DataIN = 14;   //MISO
int Xaxis = A0;         // Analoginput 0
int Yaxis = A1;         // Analoginput 1

int Xa = 0;
int Ya = 0;

// Buttons
int Button0 = 0;
int Button1 = 0;
int Button2 = 0;
int Button3 = 0;
int Button4 = 0;
int Button5 = 0;
int Button6 = 0;
int Button7 = 0;
int Button8 = 0;
int Button9 = 0;
int Button10 = 0;
int Button11 = 0;
int Button12 = 0;
int Button13 = 0;
int Button14 = 0;
int Button15 = 0;

 

 

void setup() {
  
  // G25 shift register interface configuration 
  pinMode(DataIN, INPUT);         // Data input
  pinMode(Mode, OUTPUT);           // Mode
  pinMode(Clock, OUTPUT);          // Clock

  // Digital outputs initialization
  digitalWrite(Mode, HIGH);
  digitalWrite(Clock, HIGH); 

  // Initialize Joystick Library
  Joystick.begin();
  Joystick.setXAxisRange(0, 0);
  Joystick.setYAxisRange(0, 0);
}

void loop() {
 
  // Reading shifter analog values
  
  Xa = analogRead(Xaxis);
  Ya = analogRead(Yaxis);
  
  // Reading of button states from G25 shift register
  int b[16];
  
  digitalWrite(Mode, LOW);         // Parallel mode: inputs are read into shift register
  delayMicroseconds(10);               // Wait for signal to settle
  digitalWrite(Mode, HIGH);        // Serial mode: data bits are output on clock falling edge
  
  for(int i=0; i<16; i++)              // Iteration over both 8 bit registers
  {
    digitalWrite(Clock, LOW);      // Generate clock falling edge
    delayMicroseconds(10);             // Wait for signal to settle
  
    b[i]=digitalRead(DataIN);     // Read data bit and store it into bit array
    
    digitalWrite(Clock, HIGH);     // Generate clock rising edge
    delayMicroseconds(10);             // Wait for signal to settle
   }


  // button bit assignment to variables
  Button0 = b[15]; // DPAD_TOP
  Button1 = b[14]; // DPAD_DOWN
  Button2 = b[13]; // DPAD_LEFT
  Button3 = b[12]; // DPAD_RIGHT
  Button4 = b[11]; // BLACK_DOWN
  Button5 = b[10]; // BLACK_LEFT
  Button6 = b[9]; // BLACK_RIGHT
  Button7 = b[8]; // BLACK_TOP
  Button8 = b[7]; // RED_LEFT
  Button9 = b[6]; // RED_RIGHT
  Button10 = b[5]; //RED_MIDDLE_LEFT
  Button11 = b[4]; //RED_MIDDLE_RIGHT
  Button12 = b[3]; // H-shifter = 0
  Button13 = b[2]; // ??
  Button14 = b[1]; // SHIFTER_DOWN
  Button15 = b[0];  // ??


  // Current gear calculation
  int gear=0;                          // Default value is neutral
  if(Button12==0)                             // H-shifter mode?
  {
    if(Xa<300)                                // Shifter on the left?
    {
      if(Ya>850)          gear=1;             // 1st gear
      if(Ya<180)          gear=2;             // 2nd gear
    }
    else if(Xa>600)                           // Shifter on the right?
    {
      if(Ya>850) gear=5;                      // 5th gear
      if(Ya<180) gear=6;                      // 6th gear
      if(Ya<180 && Button14 == 1) gear=99;    // R gear
    }
    else                                      // Shifter is in the middle
    {
      if(Ya>850) gear=3;       // 3rd gear
      if(Ya<180) gear=4;       // 4th gear
    }
  }

  // sequential shifter mode
    if(Button12==1)             // Shifter mode
    {
     if(Ya>650) gear=7;             // upshift
     if((Ya<650)&&(Ya>350)) gear=88;  // no shift
     if(Ya<350) gear=8;             // downshift
    }

///////////////////////////////////////////////////////////////////
// Joystick buttons DPAD 
   if(Button0==1) {Joystick.setButton(0, 1);}   else {Joystick.setButton(0, 0);}
   if(Button1==1) {Joystick.setButton(1, 1);}   else {Joystick.setButton(1, 0);}
   if(Button2==1) {Joystick.setButton(2, 1);}   else {Joystick.setButton(2, 0);}
   if(Button3==1) {Joystick.setButton(3, 1);}   else {Joystick.setButton(3, 0);}

// Joystick buttons BLACK
   if(Button4==1) {Joystick.setButton(4, 1);}   else {Joystick.setButton(4, 0);}
   if(Button5==1) {Joystick.setButton(5, 1);}   else {Joystick.setButton(5, 0);}
   if(Button6==1) {Joystick.setButton(6, 1);}   else {Joystick.setButton(6, 0);}
   if(Button7==1) {Joystick.setButton(7, 1);}   else {Joystick.setButton(7, 0);}

// Joystick buttons RED
   if(Button8==1) {Joystick.setButton(8, 1);}   else {Joystick.setButton(8, 0);}
   if(Button9==1) {Joystick.setButton(9, 1);}   else {Joystick.setButton(9, 0);}
   if(Button10==1) {Joystick.setButton(10, 1);} else {Joystick.setButton(10, 0);}
   if(Button11==1) {Joystick.setButton(11, 1);} else {Joystick.setButton(11, 0);}

// Joystick H-shifter    
   if(gear==1) {Joystick.setButton(12, 1);}     else {Joystick.setButton(12, 0);}  // 1.
   if(gear==2) {Joystick.setButton(13, 1);}     else {Joystick.setButton(13, 0);}  // 2.
   if(gear==3) {Joystick.setButton(14, 1);}     else {Joystick.setButton(14, 0);}  // 3.
   if(gear==4) {Joystick.setButton(15, 1);}     else {Joystick.setButton(15, 0);}  // 4.
   if(gear==5) {Joystick.setButton(16, 1);}     else {Joystick.setButton(16, 0);}  // 5.
   if(gear==6) {Joystick.setButton(17, 1);}     else {Joystick.setButton(17, 0);}  // 6.   
   if(gear==99) {Joystick.setButton(18, 1);}    else {Joystick.setButton(18, 0);}  // Reverse

// Joystick button Shifter *when you push the stick down during seq mode
   if((Button14==1) && (Button12==1)) {Joystick.setButton(19, 1);} else {Joystick.setButton(19, 0);}

// Joystick Sequential Shifter   
   if(gear==7) {Joystick.setButton(20, 1);}     else {Joystick.setButton(20, 0);} // upshift
   if(gear==8) {Joystick.setButton(21, 1);}     else {Joystick.setButton(21, 0);} // downshift

 

   
  // uncomment serial output for debugging
//  Serial.println("G25Buttons_SPI_joystick.ino @samaha73");
//  Serial.print("Digital inputs: ");
//  for(int i=0; i<16; i++)Serial.print(b[i]);
//  Serial.println("#");
//  Serial.print("Xaxis: ");
//  Serial.print(Xa);
//  Serial.print("; Yaxis: ");
//  Serial.print(Ya);
//  Serial.print(" = ");
//  Serial.println(gear, DEC);
//  Serial.print("#Button0:  ");
//  Serial.print(Button0, DEC);
//  Serial.print("    Button1:  ");
//  Serial.print(Button1, DEC);
//  Serial.print("    Button2:  ");
//  Serial.print(Button2, DEC);  
//  Serial.print("    Button3:  ");
//  Serial.println(Button3, DEC);
//  Serial.print("#Button4:  ");
//  Serial.print(Button4, DEC);  
//  Serial.print("    Button5:  ");
//  Serial.print(Button5, DEC);
//  Serial.print("    Button6:  ");
//  Serial.print(Button6, DEC); 
//  Serial.print("    Button7:  ");
//  Serial.println(Button7, DEC);
//  Serial.print("#Button8:  ");
//  Serial.print(Button8, DEC);
//  Serial.print("    Button9:  ");
//  Serial.print(Button9, DEC);  
//  Serial.print("    Button10: ");
//  Serial.print(Button10, DEC);
//  Serial.print("    Button11: ");
//  Serial.println(Button11, DEC);
//  Serial.print("#Button12: ");
//  Serial.print(Button12, DEC); 
//  Serial.print("    Button13: ");
//  Serial.print(Button13, DEC);
//  Serial.print("    Button14: ");
//  Serial.print(Button14, DEC);          
//  Serial.print("    Button15: ");
//  Serial.println(Button15, DEC);     
//  delay(1000);
  // serial output end
  
  

}

iRacing

Druckversion Druckversion | Sitemap
© samaha.de