Using leojoy as a base and some other stuff. I'm having trouble getting my code to work. This code works fine for leonardo based boards. But I can't get it to even compile when it's set to the Due.
I haven't bought the Due yet, but no point in buying it if it won't compile.
This is the same code that works for the Leonardos.
/*
Arduino Leonardo Joystick!
Modified heavily for 10 Bit resolution on all Axis and rotations.
Note you need the modified HID.cpp in this file and the USBAPI.h with it!
*/
/*
* Joystick Configuration
* ----------------------
*
* Enable or Disable each item you are using.
*
* They will still appear in the control panel. Except when disabled, they will be locked to mid axis
* or not respond to random noise on unterminated pins.
*
* It is a good idea to disable any axis you do not have connected to a potentiometer or button. This
* will prevent noise from accidently triggering inputs in the game.
*
*/
bool xAxis_Enable = true;
bool yAxis_Enable = true;
bool zAxis_Enable = false;
bool xRotAxis_Enable = false;
bool yRotAxis_Enable = false;
bool zRotAxis_Enable = false;
bool throttle_Enable = false;
bool rudder_Enable = false;
bool buttons_Enable = true;
/*
Button pin assignments... nice and easy to remap them here
*/
int button1 = 0;
int button2 = 1;
int button3 = 2;
int button4 = 3;
int button5 = 4;
int button6 = 5;
int button7 = 6;
int button8 = 7;
int button9 = 8;
int button10 = 9;
int button11 = 10;
int yValue;
// Create a Joystick object by calling a JoyState_t class object and calling it joySt.
JoyState_t joySt;
// Declare globally used variables
int buttons = 0;
void setup()
{
// Enable Serial port for basic console debugging.
Serial.begin(9600);
/*
* This sets the pull up on each of the buttons.
*
* Meaning you don't have to externally pull up the button pin. Just connect the switch to this pin and ground.
*
*/
pinMode(0, INPUT_PULLUP); // Gamepad Button 1
pinMode(1, INPUT_PULLUP); // Gamepad Button 2
pinMode(2, INPUT_PULLUP); // Gamepad Button 3
pinMode(3, INPUT_PULLUP); // Gamepad Button 4
pinMode(4, INPUT_PULLUP); // Gamepad Button 5
pinMode(5, INPUT_PULLUP); // Gamepad Button 6
pinMode(6, INPUT_PULLUP); // Gamepad Button 7
pinMode(7, INPUT_PULLUP); // Gamepad Button 8
pinMode(8, INPUT_PULLUP); // Gamepad Button 9
pinMode(9, INPUT_PULLUP); // Gamepad Button 10
pinMode(10, INPUT_PULLUP); // Gamepad Button 11
pinMode(14, INPUT_PULLUP); // Gamepad Button 12
pinMode(15, INPUT_PULLUP); // Gamepad Button 13
pinMode(16, INPUT_PULLUP); // Gamepad Button 14
// Zero the initial values of all the inputs
joySt.xAxis = 0;
joySt.yAxis = 0;
joySt.zAxis = 0;
joySt.xRotAxis = 0;
joySt.yRotAxis = 0;
joySt.zRotAxis = 0;
joySt.throttle = 0;
joySt.rudder = 0;
joySt.buttons = 0;
}// end setup
//----------------------------------------------------------------------------------------------------------------
/*
* Main Program loop.
*
* Gathers all the inputs, puts them in the appropiate places and then sends them to the PC
*
* Nice and easy.
*/
void loop()
{
// Read all the buttons
joySt.buttons = 0;
if (buttons_Enable){
buttons = (digitalRead(button1) << 0) + (digitalRead(button2) << 1) + (digitalRead(button3) << 2)
+ (digitalRead(button4) << 3) + (digitalRead(button5) << 4) + (digitalRead(button6) << 5)
+ (digitalRead(button7) << 6) + (digitalRead(button8) << 7) + (digitalRead(button9) << 8) + (digitalRead(button10) << 9)
+ (digitalRead(button11) << 10);
//amount of buttons are settable in the HID.cpp
joySt.buttons = ~buttons; // logic high is a one, so read the NOT version of the buttons
}
// Read all the Analogue Axis
joySt.xAxis = 512;
if (xAxis_Enable){
joySt.xAxis = analogRead(A0); // Default A0 pin
}
joySt.yAxis = 512;
yValue = analogRead(A1);
yValue = 1023 - yValue;
if(yAxis_Enable) {
joySt.yAxis = yValue; // Default A1 pin
}
joySt.zAxis = 512;
if(zAxis_Enable) {
joySt.zAxis = analogRead(A2); // Default A2 pin
}
joySt.xRotAxis = 512;
if(xRotAxis_Enable) {
joySt.xRotAxis = analogRead(A2); // Default A0
}
joySt.yRotAxis = 512;
if(yRotAxis_Enable) {
joySt.yRotAxis = analogRead(A2); // Default A0
}
joySt.zRotAxis = 512;
if(zRotAxis_Enable) {
joySt.zRotAxis = analogRead(A2); // Default A0
}
joySt.throttle = 512;
if(throttle_Enable) {
joySt.throttle = analogRead(A2); // Default A0
}
joySt.rudder = 512;
if(rudder_Enable) {
joySt.rudder = analogRead(A2); // Default A0
}
// Complete! Send all these values to Joystick.setState(&joySt) in the HID.cpp file to send it to the PC! >>>>>>>>>>>>>>>>
Joystick.setState(&joySt);
}// end main program loop
Here's the error i'm getting.
SBcontroller.cpp.o: In function `loop':
C:\Program Files (x86)\Arduino/SBcontroller.ino:170: undefined reference to `Joystick'
C:\Program Files (x86)\Arduino/SBcontroller.ino:170: undefined reference to `Joystick_::setState(JoyState*)'
collect2.exe: error: ld returned 1 exit status
Error compiling.
And i'll attach my sam-core/usb HID.cpp and USBAPI.h files.
I should note, it seems like it's not even seeing the hid file. so i'm not sure if i have to add a reference to it in another files. My SAM core files didn't even have the hid file, i used one edited from Arduino DUE as a HID Joystick and PPM Decoder - Arduino Due - Arduino Forum
And tweaked it using code from my other leondardo hid file, but neither work. (tweaked for 10bit resolution)
It would be nice to get joystick, and keyboard all working together. Mouse is less important.
Reason for going the Due route? I need a LOT of buttons and axis. Why so many? Modified steel Battalion controller.
HID.cpp (17.5 KB)
USBAPI.h (7.29 KB)