Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino Leonardo Joystick
- // based on the JoyState joystick modification in HID.cpp and USBAPI.c from here: http://www.imaginaryindustries.com/blog/?p=80
- // used for gear shifter for farming simulator 2013, sketch by modelleicher
- // only works on Arduino Leonardo! All Leonardo Types work (like Micro, Pro Micro)
- // Version of the Sketch: 2
- // addet steering wheel
- // initialize Joystick
- JoyState_t joySt;
- // define variables for the pins, 6 Shifter Buttons and 1 Clutch Pedal Button (in future maybe analog clutch..)
- //int g1 = 2;
- int g2 = 3;
- int g3 = 4;
- int g4 = 5;
- int g5 = 6;
- int g6 = 7;
- int g7 = 8;
- int g8 = 9;
- int g9 = 14;
- int g10 = 15;
- int g11 = 2;
- int stateg10 = LOW;
- int stateg11 = LOW;
- int stateg10b = LOW;
- int stateg11b = LOW;
- int steerValue = 0;
- int steerPotPin = 3;
- // pedals
- int gasPotPin = 0;
- int gasValue = 0;
- int brakePotPin = 1;
- int brakeValue = 0;
- int clutchPotPin = 2;
- int clutchValue = 0;
- int gasBrakeValue = 128;
- int gasMax = 715;
- int gasMin = 115;
- int brakeMax = 1023;
- int brakeMin = 638;
- int clutchMax = 1023;
- int clutchMin = 674;
- boolean combinedAxisMode = true;
- void setup()
- {
- // pin Mode, all Input
- //pinMode(g1, INPUT);
- pinMode(g2, INPUT);
- pinMode(g3, INPUT);
- pinMode(g4, INPUT);
- pinMode(g5, INPUT);
- pinMode(g6, INPUT);
- pinMode(g7, INPUT);
- pinMode(g8, INPUT);
- pinMode(g9, INPUT);
- pinMode(g10, INPUT);
- pinMode(g11, INPUT);
- // Variables for the joystick values, we only use buttons // and xAxis for steering
- joySt.xAxis = 128;
- joySt.yAxis = 128;
- joySt.zAxis = 128;
- joySt.xRotAxis = 128;
- joySt.yRotAxis = 128;
- joySt.zRotAxis = 128;
- joySt.throttle = 128;
- joySt.rudder = 128;
- joySt.hatSw1 = 8;
- joySt.hatSw2 = 8;
- joySt.buttons = 0;
- // Serial.begin(9600); debug
- }
- void loop()
- {
- // since buttons is a 32Bit Integer and each button is a byte we bitWrite the button Status
- //bitWrite(joySt.buttons, 0, digitalRead(g1));
- bitWrite(joySt.buttons, 1, digitalRead(g2));
- bitWrite(joySt.buttons, 2, digitalRead(g3));
- bitWrite(joySt.buttons, 3, digitalRead(g4));
- bitWrite(joySt.buttons, 4, digitalRead(g5));
- bitWrite(joySt.buttons, 5, digitalRead(g6));
- bitWrite(joySt.buttons, 6, digitalRead(g7));
- bitWrite(joySt.buttons, 7, digitalRead(g8));
- bitWrite(joySt.buttons, 8, digitalRead(g9));
- //
- stateg10 = digitalRead(g10);
- stateg11 = digitalRead(g11);
- if (stateg10 == stateg10b) {
- bitWrite(joySt.buttons, 9, LOW);
- };
- if (stateg11 == stateg11b) {
- bitWrite(joySt.buttons, 10, LOW);
- };
- if (stateg10 != stateg10b) {
- bitWrite(joySt.buttons, 9, HIGH);
- stateg10b = stateg10;
- };
- if (stateg11 != stateg11b) {
- bitWrite(joySt.buttons, 10, HIGH);
- stateg11b = stateg11;
- };
- // gather analog values
- gasValue = analogRead(gasPotPin);
- brakeValue = analogRead(brakePotPin);
- clutchValue = analogRead(clutchPotPin);
- //Serial.println(clutchValue);
- if (combinedAxisMode == true) {
- // map the values
- gasValue = map(gasValue, gasMax, gasMin, 0, 127);
- brakeValue = map(brakeValue, brakeMax, brakeMin, 0, 127);
- clutchValue = map(clutchValue, clutchMax, clutchMin, 0, 255);
- //
- gasBrakeValue = 128;
- if (gasValue > brakeValue) {
- gasBrakeValue = gasBrakeValue - gasValue;
- }
- else if (gasValue < brakeValue) {
- gasBrakeValue = gasBrakeValue + brakeValue;
- }
- joySt.yAxis = gasBrakeValue; // set value to joystick axis
- joySt.zAxis = clutchValue;
- //
- }
- else if (combinedAxisMode == false) {
- gasValue = map(gasValue, gasMax, gasMin, 0, 255);
- brakeValue = map(brakeValue, brakeMax, brakeMin, 0, 255);
- clutchValue = map(clutchValue, clutchMax, clutchMin, 0, 255);
- joySt.xAxis = gasValue;
- joySt.yAxis = brakeValue;
- joySt.zAxis = clutchValue;
- }
- bitWrite(joySt.buttons, 6, digitalRead(clutch));
- // steering wheel part:
- steerValue = analogRead(steerPotPin); // get analog value
- 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
- // 'sending' the data to the PC
- Joystick.setState(&joySt);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement