Raspberry Pico HID Gamepad send repeating button presses while button is hold

Hi All,

Total novice here, I would like to get some help with getting my code right.

Unfortunately all this is required to get the Joystick working correctly with FS2020 g1000, the joystick will be installed in a G1000 sim that I have build with a pair of Arduino Mega boards and MobiFlight software. However to replicate the actual behaviour of a joystick I need it to appear to the sim as if there are multiple button presses when the button is pressed and hold, ie HIGH.

So the desired response is to send a signal to the PC that looks like the button is being pressed>released>pressed>released cycling every so many milliseconds until the button is actually released.

To make it a little more tricky the push/center button is also being pressed with any of the direction buttons, so this press needs to be omitted when any of the direction buttons are pressed, this is the unfortunate design of ALPS RKJXT1F42001 4 way switch/ encoder that I'm using for this project.

I would appreciate any help, comments or opinions that might help me get that to work :slight_smile:

This is a library that I'm using:

And here is the code I've got so far:

#include <PicoGamepad.h>

PicoGamepad gamepad;

// 16 bit integer for holding input values
int val;

void setup() {  

  // Button on pin 
  pinMode(21, INPUT_PULLUP);
  pinMode(17, INPUT_PULLUP);
  pinMode(20, INPUT_PULLUP);
  pinMode(16, INPUT_PULLUP);
  pinMode(22, INPUT_PULLUP);

void loop() {

  if (!digitalRead(21) and !digitalRead(22)){
      gamepad.SetButton(0,!digitalRead(21) );

  } else if (!digitalRead(17)and !digitalRead(22)){
    gamepad.SetButton(1, !digitalRead(17));

  } else if (!digitalRead(20)and !digitalRead(22)){
    gamepad.SetButton(2, !digitalRead(20));

  } else if (!digitalRead(16)&& !digitalRead(22)){
    gamepad.SetButton(3, !digitalRead(16));

  } else if (!digitalRead(22)){
    gamepad.SetButton(4, !digitalRead(22));

  }else {
    gamepad.SetButton(0, !digitalRead(21));
    gamepad.SetButton(1, !digitalRead(17));
    gamepad.SetButton(2, !digitalRead(20));
    gamepad.SetButton(3, !digitalRead(16));
    gamepad.SetButton(4, !digitalRead(22));

  // Send all inputs via HID 
  // Nothing is send to your computer until this is called.

  // Flash the LED just for fun
  digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); 

Take a look at the State Change Detection example in the IDE (File->examples->02.digital->State Change Detection) and implement that for all your buttons. Also, look at the Blink Without Delay example to learn how to track elapsed time. Putting that all together, you will have to detect when a button is pressed and start your press/release cycle until you detect the button has been released.

Thanks, I'll sure check them out :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.