Leonardo Gamepad Analog as Digital inputs?

The datasheet is your friend :slight_smile: But yes, they can be used as digital inputs.

You will have to show that code :wink: In principle it will result in lag; but when working with buttons you usually have to take debouncing into account so you will have lag unless you go for hardware solutions.

how noticable is another question.

As @sterretje already pointed out, this is for debouncing reasons. To debounce a button it must not be read too often, and a delay in loop is the most simple way to achive this. If you can lower the delaytime depends on your buttons and their bouncing time.

Ahh debouncing didnt know about that. Once i get this wired up to the buttons will test it fully. It seemed to be a lot more responsive than the hacked up keyboard encoder that is currently being used.
I was going on the thought that the delay might have been added to maybe not flood the usb interface with too much data or something...

iv been searching up on this and found this info:

fastest button presser in the world held the record at 16 presses per second. using an online calculator it worked out to 16 presses per second = 62.5ms delay between presses.
so depending on where the timing is between presses would a delay of even 30ms be more than sufficient for gaming needs?

@MicroBahner
your code works great, iv adapted it to work on the pro micro board too by changing the pins in the array. very happy with this cant thank you enough.

iv managed to change the board id name so i can use them both at the same time. should work out well i hope. also cant believe how much smaller the micro pro boards are.


Iv been reading around on these forums and other sites and see that using delay is frowned upon and recommended to use millis() insted.
Is this something i should be looking to do or is it not necessary due to the application im using it in (gamepad)?

Referring to the code in the opening post, you don't have to worry about a 10ms delay.

The trick is to keep your application responsive. Imagine that you add a delay for every button by placing it inside the for-loop; you will than have a 12 x 10ms delay in total.

  // Read pin values
  for (int index = 0; index < 12; index++)
  {
    int currentButtonState = !digitalRead(index + 2);
    if (currentButtonState != lastButtonState[index])
    {
      ...
    }
    delay(10);
  }

That will be noticable and if you're playing a first person shooter you might be dead because of that :slight_smile:

With a responsive application, you can react on multiple events near simultaneously or do multiple things seemingly at the same time.

Ahh thanks for clearing that up. I overlooked that individual delays would add to the total thinking that the button presses would be seen as separate threads and executed simultaneously with the delays not compounding into an overall delay.

I have tried your original code but get an error compiling with reference to Joystick Joystick. Which Joystick library did you use.

#include <Joystick.h>



// Gamepad that uses digital pins for D pad and 8 fire buttons
//
// The digital pins 2 - 13 are grounded when they are pressed.
// Pin 2 = UP
// Pin 3 = RIGHT
// Pin 4 = DOWN
// Pin 5 = LEFT
// Pin 6 = FIRE1
// Pin 7 = FIRE2
// Pin 8 = FIRE3
// Pin 9 = FIRE4
// Pin 10 = FIRE5
// Pin 11 = FIRE6
// Pin 12 = FIRE7
// Pin 13 = FIRE8
//
// NOTE: This sketch file is for use with Arduino Leonardo and
//       Arduino Micro only.
//
//--------------------------------------------------------------------

#include <Joystick.h>

Joystick_ Joystick(0x15, JOYSTICK_TYPE_GAMEPAD,
                   8, 0,                  // Button Count, Hat Switch Count
                   true, true, false,     // X and Y, but no Z Axis
                   false, false, false,   // No Rx, Ry, or Rz
                   false, false,          // No rudder or throttle
                   false, false, false);  // No accelerator, brake, or steering

void setup() {
  // Initialize Button Pins
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
  Joystick.setXAxisRange(-1, 1);
  Joystick.setYAxisRange(-1, 1);
}

// Last state of the buttons
int lastButtonState[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void loop() {

  // Read pin values
  for (int index = 0; index < 12; index++)
  {
    int currentButtonState = !digitalRead(index + 2);
    if (currentButtonState != lastButtonState[index])
    {
      switch (index) {
        case 0: // UP
          if (currentButtonState == 1) {
            Joystick.setYAxis(-1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 1: // RIGHT
          if (currentButtonState == 1) {
            Joystick.setXAxis(1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
        case 2: // DOWN
          if (currentButtonState == 1) {
            Joystick.setYAxis(1);
          } else {
            Joystick.setYAxis(0);
          }
          break;
        case 3: // LEFT
          if (currentButtonState == 1) {
            Joystick.setXAxis(-1);
          } else {
            Joystick.setXAxis(0);
          }
          break;
        case 4: // FIRE1
          Joystick.setButton(0, currentButtonState);
          break;
        case 5: // FIRE2
          Joystick.setButton(1, currentButtonState);
          break;
        case 6: // FIRE3
          Joystick.setButton(2, currentButtonState);
          break;
        case 7: // FIRE4
          Joystick.setButton(3, currentButtonState);
          break;
        case 8: // FIRE5
          Joystick.setButton(4, currentButtonState);
          break;
        case 9: // FIRE6
          Joystick.setButton(5, currentButtonState);
          break;
        case 10: // FIRE7
          Joystick.setButton(6, currentButtonState);
          break;
        case 11: // FIRE8
          Joystick.setButton(7, currentButtonState);
          break;

      }
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(10);
}

@robertfm, which errors? Which board?

https://www.dfrobot.com/product-1075.html
beetle

Leonardo board, which code is for. I was referring to OP Slybund's 'working code' When I compiled it stops at Joystick_ Joystick(0x15, JOYSTICK_TYPE_GAMEPAD, and error exit 1 'was not declared in Scope

thats odd, i dont get any errors in the compile and its working great on the leonardo and on the 2 micro pros i have.

@runaway_pancake what board is that?

pics attached to show how it was originally someone had hacked up a keyboard encoder and used it. arduino approach is infinitely better with no lags and jamming up when 2 players play.



do you have the joystick library installed? its referenced in the very first post of this thread.

DFRobot 'Beetle' Click that link.

Yes from https://www.instructables.com/id/Arduino-LeonardoMicro-as-Game-ControllerJoystick/ Just don't get it. It sees the Joystick, opens the Windows calibrator is in the calibrator but does do anything in my original code. In yours I can't get passed the error message, I tried deleting the 'joystick etc code and then it went to another error to do with axis.

I've just copied your code again from your post and entered on a another version of Arduino on a different computer and still the same error. I am at a loss.

Screenshot or copy and paste the error you get here

Arduino: 1.8.16 (Windows 10), TD: 1.55, Board: "Arduino Leonardo"

sketch_oct14a:24:3: error: expected unqualified-id before numeric constant

8, 0, // Button Count, Hat Switch Count

^

C:\Users\Robert\Documents\Arduino\sketch_oct14a\sketch_oct14a.ino: In function 'void setup()':

sketch_oct14a:47:12: error: 'class Joystick_' has no member named 'setXAxisRange'; did you mean 'setXAxis'?

Joystick.setXAxisRange(-1, 1);

        ^~~~~~~~~~~~~

        setXAxis

sketch_oct14a:48:12: error: 'class Joystick_' has no member named 'setYAxisRange'; did you mean 'setYAxis'?

Joystick.setYAxisRange(-1, 1);

        ^~~~~~~~~~~~~

        setYAxis

Multiple libraries were found for "Joystick.h"

Used: C:\Users\Robert\Documents\Arduino\libraries\Joystick

Not used: C:\Users\Robert\Documents\Arduino\libraries\arduino-JOYSTICK-master

exit status 1

expected unqualified-id before numeric constant

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

It then repeats the same error for each line below.

8, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering

You sure you dont have additional joystick libraries installed which will cause conflicts?
Iv seen someone else have this issue and they had more than one joystick.h installed from different sources.