Introduction: Arduino Leonardo/Micro(ATMega32u4) As Gamepad/Game Controller
The chip ATMega32u4 what is used in Arduino Leonardo, Micro or Pro Micro have ability to use custom USB HID Descriptors. Thanks this ability we can make our Arduino be as generic Gamepad!
This library contains a all things to "simulate" a PS2 gamepad (so two thumbsticks and 16 buttons).
Step 1: Installing the Library
The library need a Arduino IDE 1.6.6 or higher for proper running.
Next we need to download a library.
All the data and documentation are storaged in a GitHub repository:
For download click a "Download ZIP" button in right side.
Create a new directory named "Gamepad" in (typically located in Windows at) Documents > Arduino > libraries, and copy all files from "ArduinoGamepad-master" directory in downloaded ZIP into that directory.
Then start a Arduino IDE and library appear in list of libraries and in examples list.
Step 2: Comiple and Run a Test Sketch
Just open a example sketch as in image, compile it and upload it into your Arduino
Step 3: Lets Verify of Working a Library
Go to Control Panel > Devices and Printers and search your device (for me its a Arduino Leonardo) > Right click > Settings of game device > Properties > Test.
If buttons step by step going enabled/disabled, all is okay!
Step 4: Connecting Thumbaxises and Buttons to Arduino
For testing a real connection between hardware, Arduino and PC we need some buttons or thumbaxises.
I got two thumbaxises + 2 buttons (pressing a thumbaxises), connected it with Pro Micro and lets go make an Arduino Sketch.
I connected Left Thumbstick to:
- X to A2
- Y to A3
- Z to 4
I connected Right Thumbstick to
- X to A0
- Y to A1
- Z to 3
Step 5: Testing the Thumbsticks
Compile, deploy and open Serial Monitor.
Try to move with thumbstick to minimum - maximum, try to click etc.
The thumbsticks can be a different. For me its have a relative values.
Minimum value of axis is 0 and fully maximum is 1000. So logically the center is 500.
At my Thumbstick is problem, the center have (almost) still different value, so when we will be doing a integration with library, we must make calibration, stabilisation and final formatting of values.
And the Z (pressing) i have 0 when pressed and 1 when released (this is not a true, its inverted, the invert doing a INPUT_PULLUP).
Step 6: Integrating Values to Library
In this sketch I integrated my thumbsticks as I refferenced in previous step.
Now I will describe it.
- For first we need a include a library (line 7)
- Next we need to define a centers values for all axises, i setted all 500, because its a default center (line 10 - 13)
- We defining a multiplier for formatting a values. Because maximum value is 127, we get multiplier by dividing 127 with the center, so 500 (line 14 - 17)
- Initializing a Gamepad class (line 20)
- Initializing a Pin modes (line 23 - 28)
- A calibration of thumbsticks begin (line 29)
- We define a variables, and other this (line 72 - 73)
- A while... This while is for "verifying axises of thumbsticks", if is in center area (line 74)
- We read a Analog Input from axises (line 76 - 79)
- We need to check if axises is in a "center area", i selected a 100 range (line 80 - 83)
- If is all good, while going again until is 8 good confirms, so we gets a actually real center. (line 84 - 89)
- We set axises values to global variables and we recalculate a multipliers for all axises, so realCenter divided by 127
- So for first we need to get axis value, where 0 is center, so Axis Value minus Axis Center
- Then we this multiple by axis multiplier
- And for stabilise we floor it (no round)
The numbers of buttons is described in image upper.
Step 7: Testing a Final Result
Compile, deploy and lets go play some game!
I selected a Mirrors Edge, but Mirrors Edge supports only a XBOX 360 gamepad, this can be simply solved by downloading X360CE.
Step 8: More Info, Bugs Etc.
This library is a part of my project where I build custom handheld Raspberry Pi Retro Console, what will be posted here in Instructables when it will be done, for now I writing a blogs of developing at my blog, dont forget to look it!
Documentation, Bugs etc. is in GitHub:
Or tell me it into a comments!