Hello everyone, thanks for all the help. I'm new here, but checked forum and google already doesn't find any proper solution. If anyone can help, that would be great. I'm gonna share the problem first, if anyone interested can follow rest of the topic.
The code below works perfectly when upload to the Leonardo and connect it the FIRST time, but if you unplug and connect again Logitech Shifter effects the handbrake (z axis) but Handbrake itself(10k potantiometer) doesn't works. also it happens if it stays plugged for a while without use.
#include <Joystick.h>
#include <SimRacing.h>
// Joystick definition
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
17, // 10 buttons (2 sequential + 8 H-shifter) + 7 Logitech gears
0, // No hat switch
false, false, // No X and Y axes
true, false, false, false, false, false, false, false, false); // Z axis (handbrake)
// Sequential shifter buttons
const int ButtonUpPin = 2;
const int ButtonDownPin = 3;
// H-shifter buttons (8 buttons)
const int HShifterPins[] = {4, 5, 6, 7, 8, 9, 10, 11};
// Analog handbrake pin
const int HandbrakePin = A5;
// Logitech shifter pins
const int Pin_ShifterX = A0;
const int Pin_ShifterY = A2;
const int Pin_ShifterRev = 12; // Logitech reverse gear pin
SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);
const int Gears[] = {1, 2, 3, 4, 5, 6, -1};
const int NumGears = sizeof(Gears) / sizeof(Gears[0]);
// State variables
int ButtonUpState = 0;
int ButtonDownState = 0;
int HShifterStates[8] = {0};
// ADC values for handbrake and Logitech shifter
const int ADC_Max = 1023;
void setup() {
// Sequential shifter and H-shifter inputs
pinMode(ButtonUpPin, INPUT_PULLUP);
pinMode(ButtonDownPin, INPUT_PULLUP);
for (int i = 0; i < 8; i++) {
pinMode(HShifterPins[i], INPUT_PULLUP);
}
// Handbrake
pinMode(HandbrakePin, INPUT);
Joystick.setZAxisRange(0, ADC_Max);
// Logitech shifter
shifter.begin();
// Joystick initialization
Joystick.begin(false);
Joystick.setXAxisRange(0, ADC_Max);
Joystick.setYAxisRange(ADC_Max, 0); // X and Y are inverted (if necessary)
// Initial state transmission
updateJoystick();
}
void loop() {
// Sequential shifter
int currentUpState = !digitalRead(ButtonUpPin);
if (currentUpState != ButtonUpState) {
Joystick.setButton(0, currentUpState); // Button 0: up
ButtonUpState = currentUpState;
}
int currentDownState = !digitalRead(ButtonDownPin);
if (currentDownState != ButtonDownState) {
Joystick.setButton(1, currentDownState); // Button 1: down
ButtonDownState = currentDownState;
}
// H-shifter
for (int i = 0; i < 8; i++) {
int currentHState = !digitalRead(HShifterPins[i]);
if (currentHState != HShifterStates[i]) {
Joystick.setButton(i + 2, currentHState); // Buttons 2-9: H-shifter
HShifterStates[i] = currentHState;
}
}
// Logitech shifter
shifter.update();
for (int i = 0; i < NumGears; i++) {
if (shifter.getGear() == Gears[i]) {
Joystick.pressButton(i + 10); // Buttons 10-16: Logitech gears
} else {
Joystick.releaseButton(i + 10);
}
}
// Handbrake
int handbrakeValue = analogRead(HandbrakePin);
Joystick.setZAxis(handbrakeValue);
// Sending joystick state
Joystick.sendState();
delay(10);
}
void updateJoystick() {
// Update all initial states
loop();
}
Only solution I found is reconnect all cables from breadboard and leonardo and build all circuit again. As you can understand this is not a real or sustainable solution.
I'm building my own racing simulation devices using arduino leonardo. All the projects I'm trying to do works separetely, while combine them facing with issues. In the end I find myself opening topic here.
All I need is building a hub to connect all devices separetely. Devices will be
- DB9 rs232 port(For Logitech Shifter) 5V GND 2Analog pin 1DigitalPin
- Custom made H Shifter for 7 + 1 Rear Gear (8 Limit switches) Gnd 1digital pin Each
- Sequential Shifter for Up and down Gear (2 Limit Switches) Gnd 1digital pin Each
- HandBrake (10k Potantiometer) 5V GND 1 Analog pin
- 1x Dot matrix for Gear Indicator 5V GND 3DigitalPin
- 2x ws2812b-8 for Rev light 5V GND 1 Digital Pin
Do you belive 1 arduino will be enough for all these?
Do I need any extra power suply ?
What can I do decrease to digital pin numbers to finish all project with one leonardo? (I have 74HC595 but couldn't figured out how to use it yet.
After finish Arduino part I'm gonna do the GUI interface like Logitech GHub.
But I'm wondering when all conneccted to the hub, Is it possible to create code blogs and control devices one by one?
Example activate or deactive ports.
Thanks again, I hope someone can help me to find real solution.