1

I'm creating some Joystick-like controllers to augment simulator games. I usually use the Joystick library from MHieronimus which has worked great so far.

Now, I'm using some non-momentary rocking switches and would like to change the behaviour of some of them depending on the game I'm currently playing:

  • In one mode, the according Joystick button should be "pressed" and "released" depending on the switch. So, if I flip the switch to "ON", the Joystick button is set to "pressed" until I flip the switch to "OFF".
  • In the other mode, the according Joystick button should be pressed and released every time I flip the switch (for games that don't support rocking switches/maintained activation of buttons).

I could of course reprogram the SparkFun Pro Micro every time but I'm looking for an easier solution that can be operated from windows. I'm thinking of some sort of program (like the Logitech Profiler, Roccat Swarm or any other helper Software for input devices). That program should tell the Arduino how to act on switch flips / inputs.

Is it possible? What's the recommended way to do this?

Example code:

#include <Joystick.h>

Joystick_ Joystick;

int timer = 50;
int pins[] = {2, 4, 5, 6};
int btns[]=  {2, 4, 5, 6};
int btncount = sizeof pins;
int last_states[sizeof pins];

void setup() {
  for (int i=0; i < btncount; i++) {
    pinMode(pins[i], INPUT_PULLUP);
    last_states[i] = 0;
  }
  Joystick.begin();
}

void loop() {
  for (int i = 0; i < btncount; i++) {
    int current_state = !digitalRead(pins[i]);
    if (current_state != last_states[i]) {
      Joystick.setButton(btns[i], current_state);
      last_states[i] = current_state;
    }
  }
  delay(timer);
}

Let's say pins 4, 5 and 6 are rocking switches.

3
  • the switches should be debounced, otherwise, multiple switch transfers will be registered – jsotola Dec 21 '20 at 5:00
  • I know. I'm already taking care of that in another project. Since that part doesn't matter here, I didn't overcomplicate the example. – wullxz Dec 21 '20 at 5:35
  • I plan to try this one day devicedruid.com – Juraj Dec 21 '20 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.