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_* 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
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??
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.
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.
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.
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.
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."
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.
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.
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.