I need help with a code!

I'm trying to upload code provided by "beavismotorspor" (BEAVIS Motorsport) but I'm having this problem:

C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino: In function 'void setup()':
C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino:24:3: error: 'Joystick' was not declared in this scope

  • Joystick.begin();*
  • ^~~~~~~~*
    C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino:24:3: note: suggested alternative: 'Joystick_'
  • Joystick.begin();*
  • ^~~~~~~~*
  • Joystick_*
    C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino: In function 'void loop()':
    C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino:34:3: error: 'Joystick' was not declared in this scope
  • Joystick.setButton(0,!Up);*
  • ^~~~~~~~*
    C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino:34:3: note: suggested alternative: 'Joystick_'
  • Joystick.setButton(0,!Up);*
  • ^~~~~~~~*
  • Joystick_*

exit status 1

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

Here is the code:

#include <Joystick.h>

//declare our digital input pins on the board
int upPin = 9;
int downPin = 5;

//this is the value of the input from the switches
int Up = 0;
int Down = 0;

void setup() {
  //setup our pins
  pinMode(upPin, INPUT_PULLUP);
  pinMode(downPin, INPUT_PULLUP);

  //setup the joystick library
  Joystick.begin();
}

void loop( ){

  //read our values from the switches to the digital input pins
  Up = digitalRead(upPin);
  Down = digitalRead(downPin);

  //write the value of the input to the joystick buttons.
  Joystick.setButton(0,!Up);
  Joystick.setButton(1,!Down);

  //wait a moment before rechecking the status of the inputs
  delay(50);
}

It might be something really simple, but as I'm new to the "code world" I'm stuck at this error. Can you help me??

Quick questions:

Did you

Ensure you have your board type set to Arduino/Genuino Micro

and have you installed the Jostick library that seems to have come from the same website, was it all in a zip package? It isn't the Joystick.h you'd get using the IDE Library manager, I think.

a7

Joystick.begin();*
^~~~~~~~*
C:\Users\marco\Downloads\DIYSimShifter\DIYSimShifter.ino:24:3: note: suggested alternative: 'Joystick_'
Joystick.begin();*
^~~~~~~~*
Joystick_*

seems the error indicates a case or misstype somewhere.

That project used an antiquated version of the indicated Joystick library. It's incompatible with the 2.0 version of that library that was released 8 years ago. Which has 108 open issues. Gulp. Good luck.

1 Like

Hi, @maaarquinhoo
Welcome to the forum.

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I didn't look far enough, but the implication of something I read on the linked website suggested that the necessary library was bundled, somehow, with the code.

It might be a tossup - if the older version did work with the provided sketch, it might be easiest to use, or if it didn't come on the package, hunt down and find the correct version.

On the other hand, using the logic in the sketch and adjusting it for the best joystick library of this moment might be a better path, especially if there are to be enhancements and modifications.

a7

If you are restricted to that hardware you might look into writing your own library that is purpose specific which you can then modify as needed. Would recommend looking up other joystick libraries and designing to your requirements and specifications.

https://docs.arduino.cc/learn/contributions/arduino-creating-library-guide/

The example at the library repository begins with

#include <Joystick.h>

Joystick_ Joystick;

which now I see is conspicuously missing from the posted code.

@maaarquinhoo can you provide a link to whatever you downloaded expecting to receive the software for your project?

The sketch makes minimal use of the library, so it is likely that any joystick library could be used with but minor changes.

a7

Article: "I won't detail the finer process on how to program an Arduino, but I have included my code for you with the files to download, plus the Joystick Library by Matthew Heironimus."

Do you have the library installed?

According to a trusted source

so if @maaarquinhoo decides to try making the code work without having to think about it, he must download/install the 1.x version.

I think there is some library installed, just not the correct one and this

is just plainly true. The definition is missing.

a7

Once I found the old library, the sketch did compile. Can't say if it worked, but it did compile. The old library was not in the usual form for Arduino libraries, so installing it was a very manual process and took more knowledge of Github than I think the average person would have.

LOL. The average person has zero knowledge of GitHub, and as many times as I've thrown myself at it my knowledge still qualifies as very close to zero.

Did the code not need

Joystick_ Joystick;

to have a Joystick_ object?

a7

If the 1.0 version is difficult, why not use the version 2 library with its 4-button example:

Or since the task is just translating two buttons into two HID Joystick buttons, surely there's an easy-to-load
HID joystick library available within the IDE/LibraryManager with its own button example that could replace the sketch?

With the ancient library, the code as shown in post #1 compiled. I didn't really delve into the whys or wherefores; presumably the Joystick instance is declared within the library.

arduino-cli compile -b SparkFun:avr:promicro:cpu=16MHzatmega32U4 --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Pro_Micro/test)
Sketch uses 6328 bytes (22%) of program storage space. Maximum is 28672 bytes.
Global variables use 224 bytes (8%) of dynamic memory, leaving 2336 bytes for local variables. Maximum is 2560 bytes.
Used library Version Path
Joystick             /home/me/Documents/sketchbook/libraries/Joystick
HID          1.0     /home/me/.arduino15/packages/arduino/hardware/avr/1.8.3/libraries/HID
Used platform Version Path
SparkFun:avr  1.1.13  /home/me/.arduino15/packages/SparkFun/hardware/avr/1.1.13
arduino:avr   1.8.3   /home/me/.arduino15/packages/arduino/hardware/avr/1.8.3
Compilation finished successfully.

For what it's worth, here's what I extracted from Github that got the sketch to compile. No guarantees on anything, let alone it working.

Joystick.zip (6.0 KB)

The directory contained within can be unzipped into your sketchbook/libraries directory. No knowledge of Github required. :slight_smile:

Yes. In version 1, a Joystick_ object is defined in the *.cpp file.

The version 2 library will compile the OP's sketch if only one adds

Joystick_ Joystick;

to the sketch; version 2 does not make any Jyostick_ object.

Given the simplicity of the sketch, Imma guess that both ways of getting it to compile (switch library versions or add the missing Joystick_) will work.

a7

1 Like

This problem seems to crop up over and over. Looking at the "Related Topics" at the bottom of the page:

And it's an issue closed in 2017:

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