Setting up joystick (throttle quadrant) with pro micro/leonardo

It will take some programming but it’s really only just enabling the analog port inputs and switch inputs. Maybe someone can post some sample code, I took a quick look and can’t find mine.
Keep at it, it’s worth it! Here are some notes hope it helps.
Arduino Leonardo Game Controller! This sketch code will turn an arduino Leonardo into a USB gaming Controller.
This code is heavily commented. My intent was to add as much commenting of code so that people new to Arduino could understand how to use this code and truely learn from it.

This code is modified from code I obtained through the following link:

The instructable, the unmodified code, and the Joystick library were created by Matthew Heironimus. All credit for this code belongs to him.
Without his work this code would not be possible.

This modified Game Controller code can be found at:
https://codebender.cc/sketch:232199

This code requires that you have the arduino IDE version 1.6.6 or later. It WILL NOT work with earlier versions of the Arduino IDE.
This code also requires the “Joystick” library to work and it is NOT a library the Arduino IDE has by default. There is a link to it in the above instructable. Or through this link:

Download the Joystick library and add it to the Arduino IDE. If you do not know how to do this then google it. It’s pretty easy.
***********NOTE: The Joystick library download included three seperate libraries, being; Joystick, Joystick2, and Joystick3.
You will want to add the Joystick library to the arduino IDE. The Joystick2, and Joystick3 libraries do not have to be added to the Arduino IDE for this code to work.
Adding the other two libraries (Joystick2 and Joystick3) will not stop this code from working, this code just does not use them.

Here is all the code associated with the “Joystick Library”: (These are the commands, API “Application Program Interface” that interface with the Joystick library)
**NOTE: This code only uses the Joystick Library, not the Joystick2, or Joystick 3 libraries so only the commands for the Joystick library can be used with this code.
I’ve included all the commands for all three libraries for anyone who may want to use those libraries and modify this code.

Arduino Joystick Library
Arduino IDE 1.6.6 (or above) library that add a joystick to the list of HID devices an Arduino Leonardo or Arduino Micro (or any Arduino clone that is based on the ATmega32u4) can support. This will not work with Arduino IDE 1.6.5 (or below).

Support for one, two, or three joysticks

This library comes in three flavors:

Joystick - adds a single joystick that contains an X, Y, and Z axis (including rotation), 32 buttons, 2 hat switches, a throttle, and a rudder.
Joystick2 - adds two simple joysticks that contain an X and Y axis and 16 buttons.
Joystick3 - adds three simple joysticks that contain an X and Y axis and 16 buttons.
Installation Instructions

Copy one or more of the folders (Joystick, Joystick2, and Joystick3) to the Arduino libraries folder (typically %userprofile%\Documents\Arduino\libraries). The library (or libraries) should now appear in the Arduino IDE list of libraries.

Joystick Library API

The following API is available if the Joystick library is included in a sketch file.

Joystick.begin(bool initAutoSendState)

Starts emulating a game controller connected to a computer. By default all methods update the game controller state immediately. If initAutoSendState is set to false, the Joystick.sendState method must be called to update the game controller state.

Joystick.end()

Stops the game controller emulation to a connected computer.

Joystick.setXAxis(byte value)

Sets the X axis value. Range -127 to 127 (0 is center).

Joystick.setYAxis(byte value)

Sets the Y axis value. Range -127 to 127 (0 is center).

Joystick.setZAxis(byte value)

Sets the Z axis value. Range -127 to 127 (0 is center).

Joystick.setXAxisRotation(int value)

Sets the X axis rotation value. Range 0° to 360°.

Joystick.setyAxisRotation(int value)

Sets the Y axis rotation value. Range 0° to 360°.

Joystick.setZAxisRotation(int value)

Sets the Z axis rotation value. Range 0° to 360°.

Joystick.setButton(byte button, byte value)

Sets the state (0 or 1) of the specified button (0 - 31). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.). The value is 1 if the button is pressed and 0 if the button is released.

Joystick.pressButton(byte button)

Press the indicated button (0 - 31). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.releaseButton(byte button)

Release the indicated button (0 - 31). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick.setThrottle(byte value)

Sets the throttle value. Range 0 to 255.

Joystick.setRudder(byte value)

Sets the rudder value. Range 0 to 255.

Joystick.setHatSwitch(byte hatSwitch, int value)

Sets the value of the specified hat switch. The hatSwitch is 0-based (i.e. hat switch #1 is 0 and hat switch #2 is 1). The value is from 0° to 360°, but in 45° increments. Any value less than 45° will be rounded down (i.e. 44° is rounded down to 0°, 89° is rounded down to 45°, etc.). Set the value to -1 to release the hat switch.

Joystick.sendState()

Sends the updated joystick state to the host computer. Only needs to be called if AutoSendState is false (see Joystick.begin for more details).

Joystick2 and Joystick3 Library API

The comments below this line are used with the “Joystick2” and “Joystick3” libraries. My code for this sketch ONLY uses the “Joystick1” library.****************************

The following API is available if the Joystick2 or Joystick3 library in included in a sketch file.

The joystickIndex is 0-based (i.e. the first game controller has a joystickIndex of 0, the second has a joystickIndex of 1, and the third has a joystickIndex of 2).

Joystick[joystickIndex].begin(bool initAutoSendState)

Starts emulating a game controller connected to a computer. By default all methods update the game controller state immediately. If initAutoSendState is set to false, the Joystick[joystickIndex].sendState method must be called to update the game controller state.

Joystick[joystickIndex].end()

Stops the game controller emulation to a connected computer.

Joystick[joystickIndex].setXAxis(byte value)

Sets the X axis value. Range -127 to 127 (0 is center).

Joystick[joystickIndex].setYAxis(byte value)

Sets the Y axis value. Range -127 to 127 (0 is center).

Joystick[joystickIndex].setButton(byte button, byte value)

Sets the state (0 or 1) of the specified button (0 - 15). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.). The value is 1 if the button is pressed and 0 if the button is released.

Joystick[joystickIndex].pressButton(byte button)

Press the indicated button (0 - 15). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick[joystickIndex].releaseButton(byte button)

Release the indicated button (0 - 15). The button is the 0-based button number (i.e. button #1 is 0, button #2 is 1, etc.).

Joystick[joystickIndex].sendState()

Sends the updated joystick state to the host computer. Only needs to be called if AutoSendState is false (see Joystick[joystickIndex].begin for more details).

*/
#include <Joystick.h> // with this line of code you tell the ardino IDE to reference the Joystick library. Without this line the code will not compile and will return errors.
// The ardino IDE needs to have the Joystick library installed or this code will not work. (see comments at the top of the page for more info)

// The next two lines of code assign button variables to Arduino digital pins

int button_0 = 2; // button 0 is connected to pin 2. We will call our button 0 “button_0” which is an int variable (integer variable)
int button_0_state = 0; // the “joystick” library was written so that the value of the button should be 1 when pressed and 0 when released. Here we initialize it to 0, the not pressed state.
int button_1 = 3; // button 1 is connected to pin 3
int button_value = 0; // this is the variable we will use temporarily store the buttons value. Either HIGH or LOW. HIGH when not being pressed. LOW when pressed.

// The following variable declarations are for our 8 axes that the “Joystick” library can use: Here I am initializing them all to zero so that when the code starts the axes are all set to there zero position.
unsigned int xAxis_ = 0; // the XAxis needs to be a byte value. The library needs the value to be between (-127 and 127) to work properly. Here we are making a
// variable to hold our xAxis value. We are calling this variable “xAxis_” Unsigned is used because unsigned numbers can be negative.
unsigned int yAxis_ = 0; // the YAxis also needs to be a byte value between -127 and 127
unsigned int zAxis_ = 0; // the ZAxis also needs to be a byte value between -127 and 127
int xAxisRotation_ = 0; // the XAxisRotation needs to be an integer value. The library needs the value to be between 0 and 360 to work properly. Here we are making a
// variable to hold our XAxisRotation value. We are calling this variable “xAxisRotation_” 0 to 360 represents 0 to 360 degrees for a joysticks x rotation.
int yAxisRotation_ = 0; // the YAxisRotation also needs to be an interger variable between 0 and 360
int zAxisRotation_ = 0; // the ZAxisRotation also needs to be an interger variable between 0 and 360 int Throttle_ = 0; // the Throttle needs to be byte value. The library needs the value to be between (0 and 255) to work proberly. Here we are making a variable
// to hold our Throttle value. We are calling this varialbe “Throttle_”
int Rudder_ = 0; // the Rudder also needs to be a byte value between 0 and 255
int Throttle_ = 0; // the Throttle also needs to be a byte value between 0 ad 255

// Set “initAutoSendState” to true for “Auto Send” mode or false for “Manual Send” mode (what this means):

      // By default, the "Joystick" library was created so that all methods update the game controller state immediately when they are called.
      // If initAutoSendState is set to false, the joystick.SendState method must be called to update the game controller state. Logically you would do this once after reading the joystick and all the buttons connected to your Arduino Leonardo

const bool initAutoSendState = true; // const stands for constant. Constants cannot be changed actively while the arduino is running. They are set as one or the other before uploading.
//const bool testAutoSendState = false; // bool stands for boolean. Boolean variables or constants are always set to be “true” or “false”

void setup() // Here we declare everything that you want to happen the first time the Joystick “Arduino Leonardo” is powered up.
{
Joystick.begin(); // this starts up, initializes, the joystick library
pinMode(button_0, INPUT); // Sets button_0 as an input Digital pins must be declared as an input or an output in the setup section. Here we are declaring pin 2 “button_0” as an input.
pinMode(button_1, INPUT); // sets button_1 as an input
// We did not have to declare our analog pins (A0 - A5) as input pins. They are always input pins for the Arduino Leonardo.
// We had to declare the digital pins we used because digital pins can be either input or output.
}

void loop() // This is the beginning of the looping code. You want to put all your code to continuously check the game controller within the loop section.
{
// I have choosen and declared 2 button inputs (“button_0” and “button_1”) to use in the setup. You can have more, just add them in the setup and declare your new button variables
// (with there accociated digital pin) as I have done in the above code.

// The next 2 lines of code show how to read a button and update the button with the “Joystick” library so your computer knows when a button was pressed.

button_value = digitalRead(button_0); // this line reads button_0, which is connected to digital pin 2, and stores the current value of that pin (0 or LOW if pressed, 1 or HIGH if not pressed) to the variable button_value
Joystick.setButton(0,!button_value); // this line writes a logic “NOT” “!” (which means opposite value of what the button_value is) to the Joystick library for button 0. This writes a 1 to the Library if the button_value
// is 0 and a 0 to the library if the button is 1.
// the “!” for this code is used for switches that are hooked up as pull down switches that use pull up resistors. This means that the switch is wired to always read HIGH to the Arduino
// input pin when not being pressed. Then when you press the switch the input pin will read LOW.
//Joystick.setButton(0,button_value); // this line of code would be used instead if your button switch was hooked up as as pull up switch with a pull down resistor. This would mean that
// the switch is wired to always read LOW to the arduino input pin when not being pressed. Then when you press the switch the input pin will read high.
// if this seems confusing then google how to wire a switch with a pull up or pull down resistor.
// My two buttons I’m using are connected inside two joysticks. Like an xbox left joystick. When pressed down it closes a button. (usually crouch in most first person shooter games Like Halo)
// the way the button was manufactured is as a pull down switch with a pull up resistor. I am unable to to use the switch any other way. So my button code is written for pull up resistor connected switches.
// If your are wireing your own switches you can design the style you want to use and then use the appropriate code as I have described.

button_value = digitalRead(button_1); // read button 1
Joystick.setButton(1,!button_value); // updates button 1 to the “Joystick” library

// ************* NOTE: The Joystick library can support up to 8 analog inputs. (x axis, y axis, z axis, x rotation, y rotation, z rotation, throttle, and rudder)
// ************* However, your Arduino Leonardo only has 6 analog inputs.(A0 - A5) So you have to choose 6 out of the 8 analog inputs you want. I included all of them. Uncomment or comment them
// ************* to fit your needs for your joystick setup.

// The following lines of code will read the analog inputs, that are uncommented, and assign the values read to the corresponding joystick in the “Joystick” library.

xAxis_ = analogRead(A0)/4; // This reads the position of a joystick connected to the Leonardo’s analog input “A0” and stores the positon of the x axis joystick to the “xAxis” variable.
// This value is being divided by 4 because your computer needs to see a joysticks range as being one of 0 to 255 possible values. (a byte is 256 values) The
// arduino reads your joystick position as one of 0 to 1023 possible values.(a kilobyte is 1024 values) Dividing by four converts your arduino value to a value the computer needs (between 0 and 255) because 1024/4 = 256
xAxis_ = map(xAxis_,0,255,-127,127); // the x axis value still needs to be converted from the range of 0 to 255 to the range of -127 to 127 (for the “Joystick” library). This line does just that.
// …think of the joystick pushed all the way to the left as being -127, all the way to the right as 127, and centered as 0. (-127 to positive 127 is 256 possible values just as 0 to 255 is 256 possible values)
//xAxis_ = map(xAxis_,0,255,127,-127); // comment out the above line AND uncomment out this line of you want the x axis joystick to be inverted.
Joystick.setXAxis(xAxis_); // This line of code will update the value stored in the variable xAxis_ to the Joystick library, immediately, for the XAxis. Immediately because we declared initAutoSendState as TRUE.
// This would not be immediately updated to the “Joystick” library if we declared initAutoSendState to false.

yAxis_ = analogRead(A1)/4; // read the y axis connected to analog input pin A1
//yAxis_ = map(yAxis_,0,255,-127,127);
yAxis_ = map(yAxis_,0,255,127,-127); // use this line instead of the above line for an inverted y axis
Joystick.setYAxis(yAxis_);

//zAxis_ = analogRead(A2)/4; // read the z axis connected to analog input pin A2
//zAxis_ = map(zAxis_,0,255,-127,127);
//zAxis_ = map(zAxis_,0,255,127,-127); // use this line instead of the above line for an inverted z axis
//Joystick.setZAxis(zAxis_);

//xAxisRotation_ = analogRead(A3)/4;
//xAxisRotation_ = map(xAxisRotation_,0,255,0,360);
//xAxisRotation_ = map(xAxisRotation_,0,255,360,0); // use this line instead of the above line for an inverted x rotation
//Joystick.setXAxisRotation(xAxisRotation_);

//yAxisRotation_ = analogRead(A4)/4;
//yAxisRotation_ = map(yAxisRotation_,0,255,0,360);
//yAxisRotation_ = map(yAxisRotation_,0,255,360,0); // use this line instead of the above line for an inverted y rotation
//Joystick.setYAxisRotation(yAxisRotation_);

//zAxisRotation_ = analogRead(A5)/4;
//zAxisRotation_ = map(zAxisRotation_,0,255,0,360);
//zAxisRotation_ = map(zAxisRotation_,0,255,360,0); // use this line instead of the above line for an inverted z rotation
//Joystick.setZAxisRotation(zAxisRotation_);

//Throttle_ = analogRead(A4)/4;
//Throttle_ = map(Throttle_,0,255,255,0); // use this line instead of the above line for an inverted throttle
//Joystick.setThrottle(Throttle_);

//Rudder_ = analogRead(A5)/4;
//Rudder_ = map(Rudder_,0,255,255,0); // use this line instead of the above line for an inverted throttle
//Joystick.setRudder(Rudder_);

//Joystick.setState(); // this updates the “Joystick” library and therefore your computer of all the current joystick and button states determined above all at once. YOU ONLY
// NEED TO USE THIS LINE IF const bool initAutoSendState = FALSE (the code for initAutoSendState is a few lines above the “setup” section of code)
} // This final “}” closes our looping code. The arduino will go back to the begining of the loop and start over. This will contatntly read the values of the game controller and keep
// updating the Joystick Library and therefore your computer with the ever changing values.