Advertisement
LandoRo

Openai sim pedals ex 1

Mar 13th, 2025
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_SSD1306.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_ADS1X15.h>
  5. #include <HID-Project.h>
  6. #include <HID-Settings.h>
  7.  
  8. // Define I2C pins for ESP32
  9. #define SDA_PIN 21
  10. #define SCL_PIN 22
  11.  
  12. // Define ADS1115
  13. Adafruit_ADS1115 ads;
  14.  
  15. // Define SSD1306 OLED screen
  16. #define SCREEN_WIDTH 128
  17. #define SCREEN_HEIGHT 64
  18. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  19.  
  20. // Pedal analog pins for brake, gas, and clutch (A0, A1, A2)
  21. #define GAS_PIN 0
  22. #define BRAKE_PIN 1
  23. #define CLUTCH_PIN 2
  24.  
  25. // Variables for pedal values
  26. int gasValue = 0;
  27. int brakeValue = 0;
  28. int clutchValue = 0;
  29.  
  30. // Initialize HID joystick
  31. HID_Joystick_Desc joystickDesc = {
  32.   HID_USAGE_PAGE_GENERIC,
  33.   HID_USAGE_JOYSTICK,
  34.   HID_USAGE_JOYSTICK
  35. };
  36.  
  37. void setup() {
  38.   // Initialize serial communication for debugging
  39.   Serial.begin(115200);
  40.  
  41.   // Initialize I2C
  42.   Wire.begin(SDA_PIN, SCL_PIN);
  43.  
  44.   // Initialize ADS1115 (I2C address is 0x48 by default)
  45.   if (!ads.begin()) {
  46.     Serial.println("Failed to initialize ADS1115");
  47.     while (1);
  48.   }
  49.  
  50.   // Set the gain for the ADS1115 (default to 1x gain, +/- 4.096V)
  51.   ads.setGain(GAIN_ONE);
  52.  
  53.   // Initialize the SSD1306 OLED screen
  54.   if (!display.begin(SSD1306_I2C_ADDRESS, SDA_PIN, SCL_PIN)) {
  55.     Serial.println(F("SSD1306 allocation failed"));
  56.     for (;;);
  57.   }
  58.   display.display();
  59.   delay(2000);
  60.   display.clearDisplay();
  61.  
  62.   // Initialize the HID joystick device
  63.   HID.begin(HID_REPORT_TYPE_JOYSTICK, joystickDesc);
  64.  
  65.   // Set initial joystick axes to 0 (rest position)
  66.   HID.setJoystickAxis(0, 0);  // X-axis
  67.   HID.setJoystickAxis(1, 0);  // Y-axis
  68.   HID.setJoystickAxis(2, 0);  // Z-axis
  69. }
  70.  
  71. void loop() {
  72.   // Read pedal values from the ADS1115
  73.   gasValue = map(ads.readADC_SingleEnded(GAS_PIN), 0, 32767, 0, 1023); // Gas
  74.   brakeValue = map(ads.readADC_SingleEnded(BRAKE_PIN), 0, 32767, 0, 1023); // Brake
  75.   clutchValue = map(ads.readADC_SingleEnded(CLUTCH_PIN), 0, 32767, 0, 1023); // Clutch
  76.  
  77.   // Map brake pedal value to 0-100 PSI (voltage range 0.5-4.5V)
  78.   float brakeVoltage = map(brakeValue, 0, 1023, 500, 4500) / 1000.0; // Map to 0.5-4.5V
  79.   int brakePSI = map(brakeVoltage * 1000, 500, 4500, 0, 100); // Map to 0-100 PSI
  80.  
  81.   // Update joystick axes values based on pedal inputs
  82.   HID.setJoystickAxis(0, clutchValue);  // Clutch axis
  83.   HID.setJoystickAxis(1, brakeValue);   // Brake axis
  84.   HID.setJoystickAxis(2, gasValue);     // Gas axis
  85.  
  86.   // Display brake PSI on the SSD1306
  87.   display.clearDisplay();
  88.   display.setTextSize(1);
  89.   display.setTextColor(SSD1306_WHITE);
  90.   display.setCursor(0, 0);
  91.   display.print("Brake Pedal PSI: ");
  92.   display.print(brakePSI);
  93.   display.print(" PSI");
  94.   display.display();
  95.  
  96.   delay(50);  // Small delay to allow updates to occur
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement