Problem programming a Teensy LC in Joystick

M@shPro

New member
Hello everyone.

Here's a sketch of a gamepad for a Teensy LC. It works perfectly under Windows with the game controller; it's detected as a keyboard/mouse/joystick. When I connect this gamepad to a Raspberry Pi Zero with Retropie 4.8, when mapping the UP to SELECT keys, the 13 buttons are activated, but the analog stick isn't detected. When injecting the code, it was recommended to select USB TYPE: JOYSTICK, but this option doesn't exist, which is why I selected KEYBOARD/MOUSE/JOYSTICK.
After visiting a few forums, the problem stems from the USB selection in the Arduino IDE.

How can I get the JOYSTICK selection in the USB TYPE?

My Arduino IDE version 2.3.6 and Teensy 1.59.0

The sketch:
// ****************************************************************************
// Teensy LC Gamepad (1 stick analogique gauche + 13 boutons)
// ****************************************************************************

#include <Bounce.h>

// ---------------------------------------------------------------------------
// Declaration of buttons
// ---------------------------------------------------------------------------
const int buttonPins[] = {
0, // Up
1, // Down
2, // Left
3, // Right
4, // A
5, // B
6, // X
7, // Y
8, // L1
9, // L2
10, // R1
11, // R2
20, // Start
21 // Select
};
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);

Bounce *buttons[numButtons];

// Stick analogique gauche
const int an_L_x = A0;
const int an_L_y = A1;

void setup() {
// Initialisation des boutons
for (int i = 0; i < numButtons; i++) {
pinMode(buttonPins, INPUT_PULLUP);
buttons = new Bounce(buttonPins, 10);
}

// Axes analogiques
pinMode(an_L_x, INPUT);
pinMode(an_L_y, INPUT);
}

void loop() {
// --- Gestion des boutons ---
for (int i = 0; i < numButtons; i++) {
buttons->update();

if (buttons->fallingEdge()) {
Joystick.button(i + 1, 1);
}
if (buttons->risingEdge()) {
Joystick.button(i + 1, 0);
}
}

// --- Stick analogique gauche ---
int lx = analogRead(an_L_x);
int ly = analogRead(an_L_y);

Joystick.X(lx);
Joystick.Y(ly);
}
 
I don't see how the code above could ever have run. You declare an array of buttonPins but never use an array subscript when addressing the array items. In fact as written the program WILL NOT COMPILE!
I have corrected your code below which does compile:
Also when posting code use the </> button. It presents a form to post the code into. This allows the code formatting to be maintained as seen in my posting of your corrected code below.
Code:
// ****************************************************************************
// Teensy LC Gamepad (1 stick analogique gauche + 13 boutons)
// ****************************************************************************

#include <Bounce.h>

// ---------------------------------------------------------------------------
// Declaration of buttons
// ---------------------------------------------------------------------------
uint8_t buttonPins[] = {  // WAS:- const int buttonPins[] = {

0, // Up
1, // Down
2, // Left
3, // Right
4, // A
5, // B
6, // X
7, // Y
8, // L1
9, // L2
10, // R1
11, // R2
20, // Start
21 // Select
};
const int numButtons = sizeof(buttonPins) / sizeof(buttonPins[0]);

Bounce* buttons[numButtons];

// Stick analogique gauche
const int an_L_x = A0;
const int an_L_y = A1;

void setup() {
    // Initialisation des boutons
    for (int i = 0; i < numButtons; i++) {
        pinMode(buttonPins[i], INPUT_PULLUP);            // WAS:- pinMode(buttonPins, INPUT_PULLUP);
        buttons[i] = new Bounce(buttonPins[i], 10);        // WAS:- buttons = new Bounce(buttonPins, 10);
    }

    // Axes analogiques
    pinMode(an_L_x, INPUT);
    pinMode(an_L_y, INPUT);
}

void loop() {
    // --- Gestion des boutons ---
    for (int i = 0; i < numButtons; i++) {
        buttons[i]->update();                            // WAS:- buttons[i]->update();

        if (buttons[i]->fallingEdge()) {                // WAS:- if (buttons[i]->fallingEdge()) {
            Joystick.button(i + 1, 1);
        }
        if (buttons[i]->risingEdge()) {                    // WAS:- if (buttons[i]->risingEdge()) {
            Joystick.button(i + 1, 0);
        }
    }

    // --- Stick analogique gauche ---
    int lx = analogRead(an_L_x);
    int ly = analogRead(an_L_y);

    Joystick.X(lx);
    Joystick.Y(ly);
}
 
Last edited:
Yes - this is a problem when users fail to use the </> code button, and use a variable named i to index an array in their sketch. The BBcode misleadingly interprets the [i] as an instruction to start italic text. Every instance is swallowed, making nonsense of the code and eliciting no warning from xenForo about multiple unterminated formatting markups.

It doesn't help that it's not exactly obvious that </> means "I want to put some code in", and that since the forum changeover (quite some time ago now...) all the big warnings about using code tags have disappeared, never to reappear.
 
Back
Top