Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.47 KB | None | 0 0
  1. // Maggi's Shitty Sequential Shifter + Hand Brake handler v0.01a
  2. // Author: Magnus Thor Jonsson
  3. //--------------------------------------------------------------------
  4.  
  5.  
  6. // Initializing the Joystick library
  7. #include <Joystick.h>
  8. Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  9.   2, 0,                 // Button Count, Hat Switch Count
  10.   false, false, false,  // X but no Y or Z Axis
  11.   false, false, false,  // No Rx, Ry, or Rz
  12.   false, false,         // No rudder or throttle
  13.   false, true, false);  // No accelerator, 1 brake, and no steering
  14.  
  15. // Sequential shifter
  16. int inputSequentialUp   = 11;
  17. int inputSequentialDown = 12;
  18. int lastSequentialButtonState[2] = {0,0};
  19.  
  20. // Hand brake
  21. int inputHandBrake = A5;
  22. unsigned int baselineBrakePressure  = 100;
  23. unsigned int currentBrakePressure   = 0;
  24. unsigned int pressureDeviation      = 5;   // Hard value not percentage
  25.  
  26. // Iterators
  27. unsigned int index = 0;
  28.  
  29.  
  30. void setup()
  31. {
  32.   Joystick.setBrakeRange(0, 1000);
  33.  
  34.   // Initialize Button Pins
  35.   pinMode(inputSequentialUp, INPUT_PULLUP);
  36.   pinMode(inputSequentialDown, INPUT_PULLUP);
  37.  
  38.   // Initialize Joystick Library
  39.   Joystick.begin();
  40.   // Start brake on 0
  41.   Joystick.setBrake(0);
  42. }
  43.  
  44.  
  45. void loop()
  46. {
  47.   handleShifter();
  48.   handleHandbrake();
  49.  
  50.   delay(50);
  51. }
  52.  
  53.  
  54. /**
  55.  * Handles sequential shifter buttons
  56.  */
  57. void handleShifter()
  58. {
  59.   for (index = 0; index < 2; index++)
  60.   {
  61.     int currentSequentialButtonState = !digitalRead(index + 11);
  62.     if (currentSequentialButtonState != lastSequentialButtonState[index])
  63.     {
  64.       Joystick.setButton(index, currentSequentialButtonState);
  65.       lastSequentialButtonState[index] = currentSequentialButtonState;
  66.     }
  67.   }
  68. }
  69.  
  70.  
  71. /**
  72.  * Handles handbrake pressure sensor
  73.  */
  74. void handleHandbrake()
  75. {
  76.   int currentBrakePressure = analogRead(inputHandBrake);
  77.  
  78.   // Normalize to zero
  79.   if (currentBrakePressure < baselineBrakePressure)
  80.     currentBrakePressure = 0;
  81.   else
  82.     currentBrakePressure = currentBrakePressure - baselineBrakePressure;
  83.  
  84.   if (currentBrakePressure > pressureDeviation)
  85.   {
  86.     currentBrakePressure = currentBrakePressure * 10; // Turn 100 into 1000
  87.    
  88.     // Linear conversion from 2000 max to 1000 new_value = ( (old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min
  89.     currentBrakePressure = (int)(((float)currentBrakePressure / 2000.0) * 1000);
  90.     Joystick.setBrake(currentBrakePressure);
  91.   }
  92.   //Serial.println(currentBrakePressure);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement