Using Leonardo for Logitech G27 standalone shifter

EDIT: Forget method 2. Focusing on method 1. Replied with full sketch below.

Gonna try to keep it brief.

It went from bad to worse. I tried two different tutorials to get this build to work.

The first method only recognized gears 1 through 6 but not 7. Gear 7 is used as reverse gear. Gear 7 is actually only recognized for 1 to 2 minutes while it's searching for gears 5 and 6. Once gears 5 and 6 are eventually discovered, gear 7 can never be used again. This happened twice.

The second method didn't recognize any input from my shifter and then I ended up with this after retrying multiple times:

C:\Users\Name\AppData\Local\Temp\ccX6YReG.ltrans0.ltrans.o: In function main': C:\Users\Name\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/main.cpp:43: undefined reference to setup'
C:\Users\Name\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/main.cpp:46: undefined reference to `loop'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

The second method (when compiling still worked) gave me this: "Error: 13 INTERNAL: Library install failed: library not valid" no matter how many times I try.

Hi @neptunespear.

Too brief. There isn't enough information for us to be able to help you.

Please post your full sketch.

  1. Auto Format your code by following these instructions:
    • If using Arduino IDE: select Tools > Auto Format from the menus.
    • If using Arduino Cloud Editor: press Ctrl+B
  2. Click on the window that contains your sketch code.
  3. Press Ctrl+A.
    This will select all the text.
  4. Press Ctrl+C.
    This will copy the selected text to the clipboard.
  5. In a forum reply here, click on the reply field.
  6. Click the </> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
    Code block
  7. Press Ctrl+V.
    This will paste the compilation output into the code block.
  8. Move the cursor outside of the code block markup before you add any additional text to your reply.
  9. Repeat the above process if your sketch has multiple tabs.
  10. Click the Reply button to post the output.

Please post a link to where you downloaded the library file you are attempting to install.

If you are following some tutorial, please post a link to it.

Because of this type of errors, your topic has been moved to the Programming Questions section of the forum.

Full sketch:

/*
 *  Project     Sim Racing Library for Arduino
 *  @author     David Madison
 *  @link       github.com/dmadison/Sim-Racing-Arduino
 *  @license    LGPLv3 - Copyright (c) 2022 David Madison
 *
 *  This file is part of the Sim Racing Library for Arduino.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @brief   Emulates the shifter as a joystick over USB.
 * @example ShiftJoystick.ino
 */

// This example requires the Arduino Joystick Library
// Download Here: https://github.com/MHeironimus/ArduinoJoystickLibrary

#include <SimRacing.h>
#include <Joystick.h>

// Set this option to 'true' to send the shifter's X/Y position
// as a joystick. This is not needed for most games.
const bool SendAnalogAxis = false;

// Set this option to 'true' to send the raw state of the reverse
// trigger as its own button. This is not needed for any racing
// games, but can be useful for custom controller purposes.
const bool SendReverseRaw = false;

const int Pin_ShifterX = A0;
const int Pin_ShifterY = A2;
const int Pin_ShifterRev = 2;

SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);

const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 };
const int NumGears = sizeof(Gears) / sizeof(Gears[0]);

const int ADC_Max = 1023;  // 10-bit on AVR

Joystick Joystick(
  JOYSTICK_DEFAULT_REPORT_ID,                                      // default report (no additional pages)
  JOYSTICK_TYPE_JOYSTICK,                                          // so that this shows up in Windows joystick manager
  NumGears + SendReverseRaw,                                       // number of buttons (7 gears: reverse and 1-6)
  0,                                                               // number of hat switches (none)
  SendAnalogAxis, SendAnalogAxis,                                  // include X and Y axes for analog output, if set above
  false, false, false, false, false, false, false, false, false);  // no other axes

void updateJoystick();  // forward-declared function for non-Arduino environments


void setup() {
  shifter.begin();

  // if you have one, your calibration line should go here

  Joystick.begin(false);  // 'false' to disable auto-send
  Joystick.setXAxisRange(0, ADC_Max);
  Joystick.setYAxisRange(ADC_Max, 0);  // invert axis so 'up' is up

  updateJoystick();  // send initial state
}

void loop() {
  shifter.update();

  if (SendAnalogAxis == true || shifter.gearChanged()) {
    updateJoystick();
  }
}

void updateJoystick() {
  // set the buttons corresponding to the gears
  for (int i = 0; i < NumGears; i++) {
    if (shifter.getGear() == Gears[i]) {
      Joystick.pressButton(i);
    } else {
      Joystick.releaseButton(i);
    }
  }

  // set the analog axes (if the option is set)
  if (SendAnalogAxis == true) {
    int x = shifter.getPosition(SimRacing::X, 0, ADC_Max);
    int y = shifter.getPosition(SimRacing::Y, 0, ADC_Max);
    Joystick.setXAxis(x);
    Joystick.setYAxis(y);
  }

  // set the reverse button (if the option is set)
  if (SendReverseRaw == true) {
    bool reverseState = shifter.getReverseButton();
    Joystick.setButton(NumGears, reverseState);  // "NumGears" is the 0-indexed max gear + 1
  }

  Joystick.sendState();
}

Error message:

C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:55:3: error: 'JOYSTICK_DEFAULT_REPORT_ID' was not declared in this scope
   JOYSTICK_DEFAULT_REPORT_ID,                                      // default report (no additional pages)
   ^~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:56:3: error: 'JOYSTICK_TYPE_JOYSTICK' was not declared in this scope
   JOYSTICK_TYPE_JOYSTICK,                                          // so that this shows up in Windows joystick manager
   ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino: In function 'void setup()':
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:70:12: error: 'class Joystick' has no member named 'begin'
   Joystick.begin(false);  // 'false' to disable auto-send
            ^~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:71:12: error: 'class Joystick' has no member named 'setXAxisRange'
   Joystick.setXAxisRange(0, ADC_Max);
            ^~~~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:72:12: error: 'class Joystick' has no member named 'setYAxisRange'
   Joystick.setYAxisRange(ADC_Max, 0);  // invert axis so 'up' is up
            ^~~~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino: In function 'void updateJoystick()':
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:89:16: error: 'class Joystick' has no member named 'pressButton'
       Joystick.pressButton(i);
                ^~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:91:16: error: 'class Joystick' has no member named 'releaseButton'
       Joystick.releaseButton(i);
                ^~~~~~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:99:14: error: 'class Joystick' has no member named 'setXAxis'
     Joystick.setXAxis(x);
              ^~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:100:14: error: 'class Joystick' has no member named 'setYAxis'
     Joystick.setYAxis(y);
              ^~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:106:14: error: 'class Joystick' has no member named 'setButton'
     Joystick.setButton(NumGears, reverseState);  // "NumGears" is the 0-indexed max gear + 1
              ^~~~~~~~~
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202318-19952-vbxhwq.5d0rr\ShiftJoystick\ShiftJoystick.ino:109:12: error: 'class Joystick' has no member named 'sendState'
   Joystick.sendState();
            ^~~~~~~~~

exit status 1

Compilation error: 'JOYSTICK_DEFAULT_REPORT_ID' was not declared in this scope

Joystick library master zip that I used because the one linked in YouTube video doesn't work:

Here's the tutorial I'm following for this build:

Since it doesn't include the error messages you shared in your previous post, I'll assume you already solved that problem:

I'll provide instructions you can follow to install it:

  1. Click the following link to open the library's GitHub repository:
    GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
  2. Click the "<> Code ▾" button.
  3. Select Download ZIP from the menu.
  4. Wait for the download to finish.
  5. Select Sketch > Include library > Add .ZIP Library from the Arduino IDE menus.
  6. Select the downloaded file.
  7. Click the Open button.
  8. Wait for Arduino IDE to show a "Successfully installed library ... notification.

If you follow those instructions, you should not get the error you reported previously:

Yea I'm not having trouble with the original error message anymore.

Build Method #1:
The "library not valid" error was actually for another zip, the one from my second method. I don't want to use the second method if I don't have to. The first method is good but I just need to figure out why it's not recognizing button 7.

Build Method #2:
The one that gave me the "library not valid" is this one: SHIFTERS – AMSTUDIO
Scroll all the way down to the " DIY LOGITECH USB SHIFTER ADAPTER" section. The one I downloaded from there is giving me the "library not valid" error message.
I managed to find their sketch on github: LOGITECH_USB_ADAPTER/Logitech_Shifter_USB.ino at master · AM-STUDIO/LOGITECH_USB_ADAPTER · GitHub
How can I load that sketch along with the ArduinoJoystickLibrary together into my Leonardo?

Judging by the error messages, the library you chose is not compatible with the code.
Use the one that came with the video

Just removed the library that gave me the error message and used the library that came with the video. Got a new error message:

Full sketch:

/*
 *  Project     Sim Racing Library for Arduino
 *  @author     David Madison
 *  @link       github.com/dmadison/Sim-Racing-Arduino
 *  @license    LGPLv3 - Copyright (c) 2022 David Madison
 *
 *  This file is part of the Sim Racing Library for Arduino.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

 /**
 * @brief   Emulates the shifter as a joystick over USB.
 * @example ShiftJoystick.ino
 */

// This example requires the Arduino Joystick Library
// Download Here: https://github.com/MHeironimus/ArduinoJoystickLibrary

#include <SimRacing.h>
#include <Joystick.h>

// Set this option to 'true' to send the shifter's X/Y position
// as a joystick. This is not needed for most games.
const bool SendAnalogAxis = false;

// Set this option to 'true' to send the raw state of the reverse
// trigger as its own button. This is not needed for any racing
// games, but can be useful for custom controller purposes.
const bool SendReverseRaw = false;

const int Pin_ShifterX   = A0;
const int Pin_ShifterY   = A2;
const int Pin_ShifterRev = 2;

SimRacing::LogitechShifter shifter(Pin_ShifterX, Pin_ShifterY, Pin_ShifterRev);

const int Gears[] = { 1, 2, 3, 4, 5, 6, -1 };
const int NumGears = sizeof(Gears) / sizeof(Gears[0]);

const int ADC_Max = 1023;  // 10-bit on AVR

Joystick_ Joystick(
	JOYSTICK_DEFAULT_REPORT_ID,      // default report (no additional pages)
	JOYSTICK_TYPE_JOYSTICK,          // so that this shows up in Windows joystick manager
	NumGears + SendReverseRaw,       // number of buttons (7 gears: reverse and 1-6)
	0,                               // number of hat switches (none)
	SendAnalogAxis, SendAnalogAxis,  // include X and Y axes for analog output, if set above
	false, false, false, false, false, false, false, false, false);  // no other axes

void updateJoystick();  // forward-declared function for non-Arduino environments


void setup() {
	shifter.begin();

	// if you have one, your calibration line should go here
	
	Joystick.begin(false);  // 'false' to disable auto-send
	Joystick.setXAxisRange(0, ADC_Max);
	Joystick.setYAxisRange(ADC_Max, 0);  // invert axis so 'up' is up

	updateJoystick();  // send initial state
}

void loop() {
	shifter.update();

	if (SendAnalogAxis == true || shifter.gearChanged()) {
		updateJoystick();
	}
}

void updateJoystick() {
	// set the buttons corresponding to the gears
	for (int i = 0; i < NumGears; i++) {
		if (shifter.getGear() == Gears[i]) {
			Joystick.pressButton(i);
		}
		else {
			Joystick.releaseButton(i);
		}
	}

	// set the analog axes (if the option is set)
	if (SendAnalogAxis == true) {
		int x = shifter.getPosition(SimRacing::X, 0, ADC_Max);
		int y = shifter.getPosition(SimRacing::Y, 0, ADC_Max);
		Joystick.setXAxis(x);
		Joystick.setYAxis(y);
	}

	// set the reverse button (if the option is set)
	if (SendReverseRaw == true) {
		bool reverseState = shifter.getReverseButton();
		Joystick.setButton(NumGears, reverseState);  // "NumGears" is the 0-indexed max gear + 1
	}

	Joystick.sendState();
}

Error message:

C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:54:1: error: 'Joystick_' does not name a type; did you mean 'Joystick'?
 Joystick_ Joystick(
 ^~~~~~~~~
 Joystick
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino: In function 'void setup()':
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:70:10: error: expected unqualified-id before '.' token
  Joystick.begin(false);  // 'false' to disable auto-send
          ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:71:10: error: expected unqualified-id before '.' token
  Joystick.setXAxisRange(0, ADC_Max);
          ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:72:10: error: expected unqualified-id before '.' token
  Joystick.setYAxisRange(ADC_Max, 0);  // invert axis so 'up' is up
          ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino: In function 'void updateJoystick()':
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:89:12: error: expected unqualified-id before '.' token
    Joystick.pressButton(i);
            ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:92:12: error: expected unqualified-id before '.' token
    Joystick.releaseButton(i);
            ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:100:11: error: expected unqualified-id before '.' token
   Joystick.setXAxis(x);
           ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:101:11: error: expected unqualified-id before '.' token
   Joystick.setYAxis(y);
           ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:107:11: error: expected unqualified-id before '.' token
   Joystick.setButton(NumGears, reverseState);  // "NumGears" is the 0-indexed max gear + 1
           ^
C:\Users\Name\AppData\Local\Temp\.arduinoIDE-unsaved202319-25432-lof0ey.zfy7i\ShiftJoystick\ShiftJoystick.ino:110:10: error: expected unqualified-id before '.' token
  Joystick.sendState();
          ^
Multiple libraries were found for "SimRacing.h"
  Used: C:\Users\Name\OneDrive\Documents\Arduino\libraries\Sim-Racing-Arduino-1.1.3
  Not used: C:\Users\Name\OneDrive\Documents\Arduino\libraries\Sim_Racing_Library
exit status 1

Compilation error: 'Joystick_' does not name a type; did you mean 'Joystick'?

Could you please show the file Joystick.h ?

Having trouble locating the sketch to copy and paste here. It's this one: Release Version 1.1.3 · dmadison/Sim-Racing-Arduino · GitHub

Sorry, gave the wrong link in previous post. Refresh please.

No, wait. You asked for the joystick.h. It's this one: GitHub - MHeironimus/ArduinoJoystickLibrary: An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.

Are you installed exact this Joystick library?
The library from the link has type Joystick_, so you hardly faced with errors that you showed above.

You need to install correct library.

Yes, installed this exact one. Downloaded it a few times now. I download the zip file. In Aruduino IDE, I went to Sketch>Include Library>Add .ZIP Library and added this specific library. I've removed and added and deleted and downloaded multiple times to make sure. Still getting the same error message.

Your code compiled fine for me. I installed Sim-Racing and ArduinoJoystick libraries from your links and all compiled without errors.

So you should check your installation.
I use Arduino IDE v1.8.19 on Fedora Linux

I just uninstalled everything and installed it again. I'm back to square one again using method 1. My shifter is partially working. It recognizes gears 1 through 4 and 7 temporarily as it searches for gears 5 and 6. Once it recognizes gears 5 and 6, gear 7 can never be activated again. Is there something in the sketch I'm missing?

I don't see gear 7 in your code, it includes gears from 1 to 6 only:

I guess I should've said button 7. It's the reverse gear. It requires the shifter to be pushed down before shifting into 6th gear to activate the reverse gear. When the shifter is plugged into the steering wheel it came with, it shows up as Button 7.

Plugging the shifter back into the original steering wheel, all the buttons and gears still work normally. Once I use it as a standalone unit using the Leonardo, button 7/reverse gear doesn't work.

Is there something I can add to the sketch for it to recognize the down press on the shifter before moving into 6th gear for it to recognize as button 7? I have no knowledge of coding but I can copy and paste lines of code.

I've ran out of replies here as a new user and will need to wait 21 hours before I can submit another reply. Here's a video of Button 7/Reverse Gear. Hope you see this. How To Shift Into Reverse (Button 15) On Logitech G27 - YouTube

I don't understand what you mean as Button 7, I don't see anything similar in your code. Please point me a line, where this button defined or where it used.