Guest User

Untitled

a guest
Nov 1st, 2015
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Arduino Leonardo Joystick
  2. // based on the JoyState joystick modification in HID.cpp and USBAPI.c from here: http://www.imaginaryindustries.com/blog/?p=80
  3. // used for gear shifter for farming simulator 2013, sketch by modelleicher
  4. // only works on Arduino Leonardo! All Leonardo Types work (like Micro, Pro Micro)
  5. // Version of the Sketch: 2
  6. // addet steering wheel
  7.  
  8.  
  9. // initialize Joystick
  10. JoyState_t joySt;
  11.  
  12.  
  13. // define variables for the pins, 6 Shifter Buttons and 1 Clutch Pedal Button (in future maybe analog clutch..)
  14. //int g1 = 2;
  15. int g2 = 3;
  16. int g3 = 4;
  17. int g4 = 5;
  18. int g5 = 6;
  19. int g6 = 7;
  20. int g7 = 8;
  21. int g8 = 9;
  22. int g9 = 14;
  23. int g10 = 15;
  24. int g11 = 2;
  25.  
  26. int stateg10 = LOW;
  27. int stateg11 = LOW;
  28. int stateg10b = LOW;
  29. int stateg11b = LOW;
  30.  
  31. int steerValue = 0;
  32. int steerPotPin = 3;
  33.  
  34. // pedals
  35. int gasPotPin = 0;
  36. int gasValue = 0;
  37. int brakePotPin = 1;
  38. int brakeValue = 0;
  39. int clutchPotPin = 2;
  40. int clutchValue = 0;
  41.  
  42. int gasBrakeValue = 128;
  43.  
  44. int gasMax = 715;
  45. int gasMin = 115;
  46. int brakeMax = 1023;
  47. int brakeMin = 638;
  48. int clutchMax = 1023;
  49. int clutchMin = 674;
  50.  
  51. boolean combinedAxisMode = true;
  52.  
  53.  
  54. void setup()
  55. {
  56.   // pin Mode, all Input
  57.   //pinMode(g1, INPUT);
  58.   pinMode(g2, INPUT);
  59.   pinMode(g3, INPUT);
  60.   pinMode(g4, INPUT);
  61.   pinMode(g5, INPUT);
  62.   pinMode(g6, INPUT);
  63.   pinMode(g7, INPUT);
  64.   pinMode(g8, INPUT);  
  65.   pinMode(g9, INPUT);
  66.   pinMode(g10, INPUT);
  67.   pinMode(g11, INPUT);
  68.    
  69.   // Variables for the joystick values, we only use buttons // and xAxis for steering
  70.   joySt.xAxis = 128;
  71.   joySt.yAxis = 128;
  72.   joySt.zAxis = 128;
  73.   joySt.xRotAxis = 128;
  74.   joySt.yRotAxis = 128;
  75.   joySt.zRotAxis = 128;
  76.   joySt.throttle = 128;
  77.   joySt.rudder = 128;
  78.   joySt.hatSw1 = 8;
  79.   joySt.hatSw2 = 8;
  80.   joySt.buttons = 0;
  81.  
  82.  // Serial.begin(9600); debug
  83. }
  84.  
  85.  
  86. void loop()
  87. {
  88.  
  89.         // since buttons is a 32Bit Integer and each button is a byte we bitWrite the button Status
  90.         //bitWrite(joySt.buttons, 0, digitalRead(g1));
  91.         bitWrite(joySt.buttons, 1, digitalRead(g2));
  92.         bitWrite(joySt.buttons, 2, digitalRead(g3));
  93.         bitWrite(joySt.buttons, 3, digitalRead(g4));
  94.         bitWrite(joySt.buttons, 4, digitalRead(g5));
  95.         bitWrite(joySt.buttons, 5, digitalRead(g6));
  96.         bitWrite(joySt.buttons, 6, digitalRead(g7));
  97.         bitWrite(joySt.buttons, 7, digitalRead(g8));  
  98.         bitWrite(joySt.buttons, 8, digitalRead(g9));  
  99.        
  100.         //
  101.         stateg10 = digitalRead(g10);
  102.         stateg11 = digitalRead(g11);
  103.  
  104.         if (stateg10 == stateg10b) {
  105.           bitWrite(joySt.buttons, 9, LOW);
  106.         };  
  107.         if (stateg11 == stateg11b) {
  108.           bitWrite(joySt.buttons, 10, LOW);
  109.         };              
  110.         if (stateg10 != stateg10b) {
  111.           bitWrite(joySt.buttons, 9, HIGH);
  112.           stateg10b = stateg10;
  113.         };
  114.         if (stateg11 != stateg11b) {
  115.           bitWrite(joySt.buttons, 10, HIGH);
  116.           stateg11b = stateg11;
  117.         };        
  118.        
  119.         // gather analog values
  120.         gasValue = analogRead(gasPotPin);
  121.         brakeValue = analogRead(brakePotPin);
  122.         clutchValue = analogRead(clutchPotPin);
  123.        
  124.         //Serial.println(clutchValue);
  125.         if (combinedAxisMode == true) {
  126.           // map the values
  127.           gasValue = map(gasValue, gasMax, gasMin, 0, 127);
  128.           brakeValue = map(brakeValue, brakeMax, brakeMin, 0, 127);
  129.           clutchValue = map(clutchValue, clutchMax, clutchMin, 0, 255);    
  130.           //
  131.  
  132.  
  133.           gasBrakeValue = 128;
  134.           if (gasValue > brakeValue) {
  135.              gasBrakeValue = gasBrakeValue - gasValue;
  136.           }      
  137.           else if (gasValue < brakeValue) {
  138.              gasBrakeValue = gasBrakeValue + brakeValue;
  139.           }
  140.          
  141.           joySt.yAxis = gasBrakeValue; // set value to joystick axis
  142.           joySt.zAxis = clutchValue;
  143.           //
  144.         }
  145.         else if (combinedAxisMode == false) {
  146.           gasValue = map(gasValue, gasMax, gasMin, 0, 255);
  147.           brakeValue = map(brakeValue, brakeMax, brakeMin, 0, 255);
  148.           clutchValue = map(clutchValue, clutchMax, clutchMin, 0, 255);
  149.          
  150.           joySt.xAxis = gasValue;
  151.           joySt.yAxis = brakeValue;
  152.           joySt.zAxis = clutchValue;
  153.         }        
  154.         bitWrite(joySt.buttons, 6, digitalRead(clutch));
  155.        
  156.         // steering wheel part:
  157.         steerValue = analogRead(steerPotPin); // get analog value
  158.         joySt.xAxis = map(steerValue, 0, 1023, 0, 255); // map the 10 Bit (0-1023) Value from the ADC to 8 Bit (0-255) for the Joysticks needs  
  159.        
  160.  
  161.         // 'sending' the data to the PC
  162.     Joystick.setState(&joySt);      
  163. }
RAW Paste Data