So this is my first arduino project and having a little difficulty. the setup will be used for sim racing and the toggle switches should just send a quick pulse (not continuous). same for the button.
- each toggle should only light up in the on position.
- the left most toggle (ignition) should "control" the button. when on, the button led lights up and is able to start the sim car.
- i was able to get 1 toggle working pretty reliably.
- once i add 2 more toggles (3 in line) they all stop working (haven't even attempted the 4th toggle and button yet)
i am trying to use the joystick library, and ill be honest, AI, to create the sketch. i guess id like to know, does my diagram look sound? is there anything i should look to use in the sketch? maybe an example i can use to compare with what i have
(diagram using parts as close as i could find in cirkit designer)
really appreciate any guidance
/*
* Arduino Pro Micro (ATmega32U4)
*
* Three SPST toggle switches as pulsed joystick buttons.
*
* No external resistors required.
* Uses internal INPUT_PULLUP.
*
* Switch 1: D2 -> Joystick Button 1
* Switch 2: D3 -> Joystick Button 2
* Switch 3: D4 -> Joystick Button 3
*
* Wiring per switch:
* Contact A -> GND
* Contact B -> Digital input pin
*
* Switch ON = LOW
* Switch OFF = HIGH
*
* Every stable toggle transition (ON or OFF)
* generates one short joystick button press.
*/
#include <Joystick.h>
const uint8_t NUM_SWITCHES = 3;
const uint8_t SENSE_PIN[NUM_SWITCHES] = {
2, 3, 4
};
const uint8_t RX_LED_PIN = 17;
// Timing
const unsigned long DEBOUNCE_MS = 25;
const unsigned long PULSE_MS = 40;
// Joystick configuration
Joystick_ Joystick(
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_JOYSTICK,
NUM_SWITCHES,
0,
false, false, false,
false, false, false,
false, false,
false, false, false
);
// Switch tracking
bool lastReportedState[NUM_SWITCHES];
bool lastReading[NUM_SWITCHES];
unsigned long lastDebounceTime[NUM_SWITCHES];
unsigned long pulseEndTime[NUM_SWITCHES];
void setup() {
for (uint8_t i = 0; i < NUM_SWITCHES; i++) {
pinMode(SENSE_PIN[i], INPUT_PULLUP);
lastReading[i] = digitalRead(SENSE_PIN[i]);
lastReportedState[i] = lastReading[i];
lastDebounceTime[i] = 0;
pulseEndTime[i] = 0;
Joystick.releaseButton(i);
}
// RX LED mirrors switch 1
pinMode(RX_LED_PIN, OUTPUT);
digitalWrite(
RX_LED_PIN,
lastReading[0] == LOW ? LOW : HIGH
);
Joystick.begin();
}
void loop() {
for (uint8_t i = 0; i < NUM_SWITCHES; i++) {
handleSwitch(i);
}
}
void handleSwitch(uint8_t i) {
bool reading = digitalRead(SENSE_PIN[i]);
// Detect a change in raw input
if (reading != lastReading[i]) {
lastDebounceTime[i] = millis();
lastReading[i] = reading;
}
// Wait for stable input
if ((millis() - lastDebounceTime[i]) > DEBOUNCE_MS) {
if (reading != lastReportedState[i]) {
lastReportedState[i] = reading;
// Generate one joystick pulse
pulseEndTime[i] = millis() + PULSE_MS;
Joystick.pressButton(i);
// RX LED mirrors switch 1
if (i == 0) {
digitalWrite(
RX_LED_PIN,
reading == LOW ? LOW : HIGH
);
}
}
}
// Release joystick button after pulse
if (
pulseEndTime[i] != 0 &&
(long)(millis() - pulseEndTime[i]) >= 0
) {
Joystick.releaseButton(i);
pulseEndTime[i] = 0;
}
}