Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_ADS1X15.h>
- #include <HID-Project.h>
- #include <HID-Settings.h>
- // Define I2C pins for ESP32
- #define SDA_PIN 21
- #define SCL_PIN 22
- // Define ADS1115
- Adafruit_ADS1115 ads;
- // Define SSD1306 OLED screen
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
- // Pedal analog pins for brake, gas, and clutch (A0, A1, A2)
- #define GAS_PIN 0
- #define BRAKE_PIN 1
- #define CLUTCH_PIN 2
- // Variables for pedal values
- int gasValue = 0;
- int brakeValue = 0;
- int clutchValue = 0;
- // Initialize HID joystick
- HID_Joystick_Desc joystickDesc = {
- HID_USAGE_PAGE_GENERIC,
- HID_USAGE_JOYSTICK,
- HID_USAGE_JOYSTICK
- };
- void setup() {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Initialize I2C
- Wire.begin(SDA_PIN, SCL_PIN);
- // Initialize ADS1115 (I2C address is 0x48 by default)
- if (!ads.begin()) {
- Serial.println("Failed to initialize ADS1115");
- while (1);
- }
- // Set the gain for the ADS1115 (default to 1x gain, +/- 4.096V)
- ads.setGain(GAIN_ONE);
- // Initialize the SSD1306 OLED screen
- if (!display.begin(SSD1306_I2C_ADDRESS, SDA_PIN, SCL_PIN)) {
- Serial.println(F("SSD1306 allocation failed"));
- for (;;);
- }
- display.display();
- delay(2000);
- display.clearDisplay();
- // Initialize the HID joystick device
- HID.begin(HID_REPORT_TYPE_JOYSTICK, joystickDesc);
- // Set initial joystick axes to 0 (rest position)
- HID.setJoystickAxis(0, 0); // X-axis
- HID.setJoystickAxis(1, 0); // Y-axis
- HID.setJoystickAxis(2, 0); // Z-axis
- }
- void loop() {
- // Read pedal values from the ADS1115
- gasValue = map(ads.readADC_SingleEnded(GAS_PIN), 0, 32767, 0, 1023); // Gas
- brakeValue = map(ads.readADC_SingleEnded(BRAKE_PIN), 0, 32767, 0, 1023); // Brake
- clutchValue = map(ads.readADC_SingleEnded(CLUTCH_PIN), 0, 32767, 0, 1023); // Clutch
- // Map brake pedal value to 0-100 PSI (voltage range 0.5-4.5V)
- float brakeVoltage = map(brakeValue, 0, 1023, 500, 4500) / 1000.0; // Map to 0.5-4.5V
- int brakePSI = map(brakeVoltage * 1000, 500, 4500, 0, 100); // Map to 0-100 PSI
- // Update joystick axes values based on pedal inputs
- HID.setJoystickAxis(0, clutchValue); // Clutch axis
- HID.setJoystickAxis(1, brakeValue); // Brake axis
- HID.setJoystickAxis(2, gasValue); // Gas axis
- // Display brake PSI on the SSD1306
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0);
- display.print("Brake Pedal PSI: ");
- display.print(brakePSI);
- display.print(" PSI");
- display.display();
- delay(50); // Small delay to allow updates to occur
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement