Leonardo Not Showing As Controller

I am trying to make a button box controller for PC gaming. I have installed these two libraries, the sketch uploads with no errors, and the board works fine:

#include <Keypad.h>
#include <Joystick.h>

I can’t figure out the code needed to get it to work as a controller.

#include <Bounce2.h>
#include <Keypad.h>
#include <Joystick.h>

const int     BUTTON_COUNT = 4;
const int16_t DEBOUNCE_DELAY = 50;
const uint8_t buttonPins[BUTTON_COUNT] = {2, 4, 6, 8};
const uint8_t ledPins[BUTTON_COUNT] = {3, 5, 7, 9};

Bounce buttons[BUTTON_COUNT];
uint8_t ledStates[BUTTON_COUNT];

void setup() {
    for (int i = 0; i < BUTTON_COUNT; i++) {
        buttons[i].attach(buttonPins[i], INPUT);
        buttons[i].interval(DEBOUNCE_DELAY);
        pinMode(ledPins[i], OUTPUT);
        ledStates[i] = LOW;
        digitalWrite(ledPins[i], ledStates[i]);
    }
}

void loop() {

    // Update the debouncers.
    for (int i = 0; i < BUTTON_COUNT; i++)
        buttons[i].update();

    // When the first button is pressed:
    if (buttons[0].rose()) {

        // Light up the first LED.
        digitalWrite(ledPins[0], HIGH);

        // and turn off all the others.
        for (int i = 1; i < BUTTON_COUNT; i++) {
            ledStates[i] = LOW;
            digitalWrite(ledPins[i], ledStates[i]);
        }
    }

    // When the first button is released:
    if (buttons[0].fell()) {

        // turn off the first LED.
        digitalWrite(ledPins[0], LOW);
    }

    // When any other button is pressed:
    for (int i = 1; i < BUTTON_COUNT; i++) if (buttons[i].rose()) {

        // Toggle its state.
        ledStates[i] = !ledStates[i];
        digitalWrite(ledPins[i], ledStates[i]);
    }
}

(deleted)

I am trying to make my PC recognise my Leo as a controller. I have managed to make it appear in controllers now, but it still doesn't register button presses.

I added this code:

Joystick_ Joystick;

Keyboard.begin();
     Joystick.begin();

spycatcher2k:
Please describe the issue a bit better!

(deleted)

I am trying, but it's a whole new language. I tried adding the following code.

{
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

spycatcher2k:
I don't see where in your code you press the buttons. Perhaps you need to read, and understand the examples supplied with the library. :slight_smile:

When you modify code, please post the complete new code.

It’s not quite clear to me what you want to achieve; I’m not a gamer :wink:

If you want to use the Leonardo as a joystick, you do not need Keypad.h and Joystick.h; those are for keypads and to connect a joystick to an Arduino respectively. You need Keyboard.h and Mouse.h (to send keystrokes and control the cursor / click); see Mouse and Keyboard libraries for available methods. There are three examples in the IDE as well how to use it.

If this is a new language to you, start simple; see below

3a)
Send a text to e.g. notepad

#include <Keyboard.h>
void setup()
{
  Keyboard.begin();
  pinMode(A0, INPUT_PULLUP);
}

void loop() {

  if (digitalRead(A0) == LOW)
  {
    Keyboard.print("Hello World");
    Keyboard.write(KEY_RETURN);
    delay(1000);
  }
}

3b)
Move the mouse

#include <Mouse.h>
void setup()
{
  Mouse.begin();
  pinMode(A0, INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(A0) == LOW)
  {
    Mouse.move(10, 10, 0);
    delay(1000);
  }
}

Note:
Make sure you have a means to stop sending keystrokes / keypresses to the PC or you will have difficulty uploading code. The above achieves that by waiting for the pin A0 to go low; you can use any button.

Thanks for the tips, I'll give it a go.

sterretje:
1)
When you modify code, please post the complete new code.

It's not quite clear to me what you want to achieve; I'm not a gamer :wink:

If you want to use the Leonardo as a joystick, you do not need Keypad.h and Joystick.h; those are for keypads and to connect a joystick to an Arduino respectively. You need Keyboard.h and Mouse.h (to send keystrokes and control the cursor / click); see Mouse and Keyboard libraries for available methods. There are three examples in the IDE as well how to use it.

If this is a new language to you, start simple; see below

Note:
Make sure you have a means to stop sending keystrokes / keypresses to the PC or you will have difficulty uploading code. The above achieves that by waiting for the pin A0 to go low; you can use any button.