loading

IMPORTANT NOTE: This article is for Arduino IDE version 1.6.6 (or above). To add a USB Game Controller to an Arduino Leonardo or Micro using Arduino IDE version 1.6.5 (or below) see the following Instructable: Add USB Game Controller to Arduino Leonardo/Micro.

Out of the box the Arduino Leonardo and the Arduino Micro appear to the host computer as a generic keyboard and mouse. This article discusses how the Arduino Leonardo and the Arduino Micro can also appear as a generic Game Controller or Joystick. This project will only work with Arduino products based on the ATmega32u4 microcontroller (i.e. the Arduino Leonardo and the Arduino Micro). It will not work with the Arduino UNO, because it is based on the ATmega328 microcontroller.

The Arduino Joystick Library (a.k.a. Game Controller library) used in the Instructable provides the following:

  • X, Y, and Z axis
  • 32 buttons
  • X, Y, and Z axis rotation
  • Rudder
  • Throttle
  • 2 Point of View Hat Switches

Step 1: Installing the Library

The latest version of the Arduino library that allows the Arduino Leonardo and Micro to appear as a Game Controller can be downloaded from the following GitHub repository:

https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/version-1.0

This GitHub repository actually contains three different versions of the Arduino Joystick Library:

  • Joystick - Causes the Arduino to appear as single Game Controller
  • Joystick2 - Causes the Arduino to appear as two, simple Game Controllers
  • Joystick3 - Causes the Arduino to appear as three, simple Game Controllers

Copy the Joystick (and/or Joystick2 and/or Joystick3) folder from GitHub to the Arduino Libraries folder (typically located at %userprofile%\Documents\Arduino\libraries). The library (or libraries) should now appear in the Arduino IDE list of libraries.

Step 2: Running the Test Sketch

Included in the library is a test sketch, called UsbJoystickTest.ino (or UsbJoystick2Test.ino or UsbJoystick3Test.ino). This sketch should be loaded, compiled, and uploaded to the Arduino Leonardo or Micro using the Arduino IDE (version 1.6.6 or above).

The following steps are for Windows 7. If you have a different version of Windows or a different operating system, these steps may differ.

Step 3: Windows Control Panel - Hardware and Sound

Go to the Windows Control Panel and select “Hardware and Sound”.

Step 4: Devices and Printers

Then select “Devices and Printers”.

Step 5: Arduino Settings

  1. The Arduino Micro or Arduino Leonardo should appear in the list of devices.
  2. Right mouse click on the Arduino Leonardo or Arduino Micro to display the settings menu.
  3. Select “Game controller settings” to get to the “Game Controllers” dialog.

Step 6: Game Controller Settings

The Arduino Leonardo or Micro should appear in the list of installed game controllers. Select the Arduino Leonardo or Micro and click the Properties button to display the game controller test dialog.

Step 7: Test Using Game Controller Settings

While this dialog has focus, ground pin A0 on the Arduino to activate the test script. The test script will test the game controller functionality in the following order:

  • 32 buttons
  • throttle and rudder
  • X and Y Axis
  • Z Axis
  • 2 Hat Switches
  • X and Y Axis Rotation

Step 8: Another Simple Test

Once you have verified the Arduino Leonardo or Micro is working as a Game Controller, you can start creating your own Game Controller projects. The attached Arduino sketch file is a simple example that reads digital pins 9, 10, 11, and 12 and maps them to buttons 1, 2, 3, and 4 on the Game Controller.

Step 9: Joystick Library API

Now that the Joystick library is available to the Arduino IDE, an Arduino Leonardo or Arduino Micro can be used for custom game controller projects. The following describes the complete Arduino Joystick Library API.

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).

Step 10: Additional Information

Additional information about this Arduino Joystick Library can be found on the following websites:

Arduino Joystick Library - http://mheironimus.blogspot.com/2015/11/arduino-joystick-library.html

GitHub [MHeironimus/ArduinoJoystickLibrary] - https://github.com/MHeironimus/ArduinoJoystickLibrary

<p>Great job with this mate. Days and days of fun you basically taught me coding, i wouldn't have learned it if i didnt want to modd my joysticks lol it kept me going.</p><p>i have a question if you can answer it. i was wondering if i could use this libraby on a teensy board? to easly manage 32 buttons. Thanks again</p>
<p>Any Arduino compatible board based on the ATmega32u4 should work. That being said, I personally have only tested using the Arduino Micro and Arduino Leonardo. I try to keep the list of &quot;supported&quot; board up to date on the GitHub repository (https://github.com/MHeironimus/ArduinoJoystickLibrary). Based on the Teensy website (https://www.pjrc.com/teensy/), it seems like the Teensy 2.0 should work, but I am not sure about the others (Teensy++ 2.0, Teensy 3.0, Teensy 3.1, etc.) since they have different microprocessors. If you do try the library with a Teensy, let me know if it works or not. Thanks.</p>
<p>Yes Sir! i might try both.</p><p>for science. ;)</p>
<p>i have already ordered a 3.6 :/ i know that wont work with the same code. but thanks for the 2.0 advice. i will let you know what i learn.</p>
<p>Thank you so much for this Instructable. How ever. Is there a <br>tutorial that you know of that finishes it off with the coding and <br>button schematics to full fill the 32 buttons and axes?. </p><p>When I <br>saw this I was looking for a DIY button controller. When I saw this it <br>had enough buttons and axes for exactly what I wanted. I went and bought<br> the Leo and a kit and a bunch of buttons. Got the Leo to work on <br>Windows but now I am stuck and trying to find a way to wire 32 button <br>and all the axes. </p><p>Thanks for any replay you can provide. Thanks</p>
<p>JasonG255 - The easiest way to get more that 16 buttons on a Leonardo or Micro is to use a series of Shift Registers. The Arduino website has a tutorial on how to use these at <a href="https://www.arduino.cc/en/Tutorial/ShiftIn" rel="nofollow">https://www.arduino.cc/en/Tutorial/ShiftIn</a>. Hope this helps. -Matt</p>
<p>is buying a teensy board easier? lol i would have to lean more on shift registers. i only know a little.</p>
<p>Any Arduino compatible board based on the ATmega32u4 should work. That being said, I personally have only tested using the Arduino Micro and Arduino Leonardo. I try to keep the list of &quot;supported&quot; board up to date on the GitHub repository (<a href="https://www.pjrc.com/teensy/" rel="nofollow">https://github.com/MHeironimus/ArduinoJoystickLibrary</a>). Based on the Teensy website (<a href="https://www.pjrc.com/teensy/" rel="nofollow">https://www.pjrc.com/teensy/</a>), it seems like the Teensy 2.0 should work, but I am not sure about the others (Teensy++ 2.0, Teensy 3.0, Teensy 3.1, etc.) since they have different microprocessors. If you do try the library with a Teensy, let me know if it works or not. Thanks.</p>
<p>cracked it? its not beyond you trust me. if you haven't just take an hour out your day to look over the code that Matthew wrote and you'll be able to work out where to squeeze your buttons. ill be doing arduino tutorials on my channel soon sub</p>
<p>would making a switch matrix and using the keypad register work?</p>
<p>hello Matthew! thanks for everything!</p><p>i am modding an old ps2 racing wheel to work on my pc, and you helped me so much! i have already made my X and Y axis, and they are working great! now i want to write the code for the 8 buttons. i am guessing that i have to use switch or if, right?</p><p>thanks once again! have a great day!</p>
<p>i have wrote the code, and everything works like a charm! thanks once again Matthew!</p><p>here is my code if anyone is interested in using it, or imporving it(bevause i know its not going to be good :P)</p><p>#include &quot;Joystick.h&quot;</p><p>Joystick_ Joystick;</p><p>void setup() {</p><p>Joystick.begin();</p><p>Joystick.setXAxis(0);</p><p>Joystick.setYAxis(0);</p><p> pinMode(6, INPUT_PULLUP);</p><p> pinMode(7, INPUT_PULLUP);</p><p> pinMode(8, INPUT_PULLUP);</p><p> pinMode(9, INPUT_PULLUP);</p><p> pinMode(10, INPUT_PULLUP);</p><p> pinMode(11, INPUT_PULLUP);</p><p> pinMode(12, INPUT_PULLUP);</p><p> pinMode(13, INPUT_PULLUP);</p><p>int lastButtonState = 0;</p><p>}</p><p>void loop() {</p><p>int button0Val =digitalRead(6);</p><p>int button1Val =digitalRead(7);</p><p>int button2Val =digitalRead(8);</p><p>int button3Val =digitalRead(9);</p><p>int button4Val =digitalRead(10);</p><p>int button5Val =digitalRead(11);</p><p>int button6Val =digitalRead(12);</p><p>int button7Val =digitalRead(13);</p><p>int xAxis = analogRead(A0);</p><p>int yAxis = analogRead(A1);</p><p>Joystick.setXAxis(xAxis);</p><p>Joystick.setYAxis(yAxis);</p><p>Joystick.setButton(0, !button0Val);</p><p>Joystick.setButton(1, !button1Val);</p><p>Joystick.setButton(2, !button2Val);</p><p>Joystick.setButton(3, !button3Val);</p><p>Joystick.setButton(4, !button4Val);</p><p>Joystick.setButton(5, !button5Val);</p><p>Joystick.setButton(6, !button6Val);</p><p>Joystick.setButton(7, !button7Val);</p><p>}</p>
<p>cool stuff right! i just added 360 pedals to my flight setup no going back now. checkout my youtube if your interested it modding ;) ive got some idea coming for the future. im learning alot.</p><p>https://www.youtube.com/watch?v=OzvGmhhtsEs</p>
<p>Hi Matthew,</p><p>I'm trying this code to use a joystick but I have problems</p><p>Z axis works while X-axis and Y no.</p><p>#include &quot;Joystick.h&quot;</p><p>Joystick_ Joystick;</p><p>void setup() {</p><p>Joystick.begin();</p><p>}</p><p>void loop() {</p><p>int xAxis = analogRead(A0);</p><p>xAxis = map(xAxis,0,1023,-127,127);</p><p>Joystick.setXAxis(xAxis);</p><p>int yAxis = analogRead(A1);</p><p>yAxis = map(yAxis,0,1023,-127,127);</p><p>Joystick.setYAxis(yAxis);</p><p>int zAxis = analogRead(A2);</p><p>Joystick.setZAxis(zAxis);</p><p>}</p><p>Thanks in advance,</p>
<p>How are the X and Y axis behaving (e.g. not moving at all, they are erratic, they do not move the full range, etc.)? Have you tried debugging the values coming in from the analogRead function or the output of the map function?</p><p>You could also try the latest version of the library (<a href="https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/master" rel="nofollow">https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/master</a>) and see if that works better for you. You will not need the map function call with the latest version of the library.</p>
<p>Hi Matthew,</p><p>X and Y axises as well as Rx and Ry are moving irregularry and when I try to move the sticks, they go crazy!If I use Serial as axises reader the value goes from 0 in the middle 127 to left -127 to right. Axis Z and Rz are perfect. I'm using the Library last version.</p><p>this is the code:</p><p>#include &quot;Joystick.h&quot;</p><p>Joystick_ Joystick; </p><p>void setup()</p><p>{</p><p> Joystick.begin(); </p><p> }</p><p>void loop() </p><p>{</p><p> int xAxis = analogRead(A0); </p><p> Joystick.setXAxis(xAxis); </p><p> int yAxis = analogRead(A1); </p><p> Joystick.setYAxis(yAxis);</p><p> int zAxis = analogRead(A2); </p><p> Joystick.setZAxis(zAxis); </p><p> int rzAxis = analogRead(A3); </p><p> Joystick.setRzAxis(rzAxis);</p><p> }</p>
<p>Hi Matthew,</p><p>I'm using your library to realize an joystick.</p><p>It is possible to change the name of the gamepad?</p><p>I mean, Can I to change the name that appear in &quot;device and printers&quot; list from &quot;arduino Leonardo&quot; to &quot;topolino&quot;?</p><p>Thanks in advance,</p><p>tiopepe</p>
I have never done this, but there is a discussion about this on GitHub at <a href="https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/14" rel="nofollow">https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/14</a>.
<p>Hi Matthew,</p><p>I'm using your library to realize an analog handbrake.</p><p>Does it make sense to reduce the amount of traffic sent to the PC by reducing the frequency of calling the send command (directly or indirectly with autosend)? Currently I call the send operation only if the input value has changed. I'm wandering if the data is anyway sent with 100Hz to the PC so that this does not make sense at all!?</p><p>Thanks for any help and your great library,<br>Peter</p>
Peter,<br><br>I think you are OK either way. You can get by with only sending data when the values change, but sending at a rate of 100Hz should be OK as well. I do not think you would want to send any more frequently than that, however.<br><br>Matt
<p>Hi Matthew,</p><p>Thanks for your help! Finally I implemented it so, that data is only set if the change of the value is larger than a specific change tolerance which can be set by a constant: <a href="https://github.com/phaase/analog-controller-input" rel="nofollow">https://github.com/phaase/analog-controller-input </a> </p><p>Peter</p>
<p>Glad to hear you got it working the way you wanted. Thanks for sharing your link.</p>
<p>If I understand correctly, it does not makes sense to reduce the frequency of send operations on the device side...</p><p>From [https://en.wikipedia.org/wiki/USB_human_interface_device_class]:</p><p>&quot;The host periodically polls the device's interrupt IN endpoint during operation. When the device has data to send it forms a report and sends it as a reply to the poll token.&quot;</p>
It does take some of the host CPU time to process the HID Reports coming in, so you do not want to overwhelm the host computer with non-stop messages, but 100Hz should be fine.
<p>Funnt really, I asked in the Arduino community for some help with making my own HID type interface for such a gizmo and I got nothing but demands for code that I didn't have and no help whatsoever.</p><p>*thumbs up* for a nice instructable.</p><p>*thumbs up* for a very nice instructable.</p>
<p>Hi Matthew,</p><p>Great job on the Joystick library.</p><p>I have a few questions. The game controller shows up as an Arduino Leonardo, is it possible to change its name?<br>Connection shows as a com port, dis you ever think about porting to LUFA and be able to assign a VID and PID?</p><p>I want to implement reading buttons from CD4021 shift registers. There is code for it that I could use, but was just wondering if you have ideas about this?</p><p>regards,</p><p>Arend</p>
Arend,<br><br>There is a current thread out on GitHub that is discussing renaming the Arduino at https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/14. I have been wanting to investigate this for some time, but have not had the time. :(<br><br>Using a shift register (like the CD4021) should be fine. I have used 74HC595 shift register on other Arduino projects without issue.<br><br>Matt
<p>Greatjob!!! you have taught me soo much. thankyou ;)</p>
<p>Hi Matthew,</p><p>thanks for this library!<br></p><p>I just found <br>out that MULTI_AXIS does not work on the Micro, GAMEPAD does. In my case<br> the Micro was recognized as a com port without the slightest chance to <br>enter the game controller properties... Took me only 5 hours to find out<br> :)<br></p><p>(tested on Win 7/64(10.6.13) and MacOS Sierra (10.6.8))</p><p>It<br> is tempting just to read several analog inputs one after another, but <br>that is a trap: The analog inputs affect each other if you do not place <br>&quot;delay&quot; or &quot;(void)analogRead&quot; between them. </p><p>First part of my project was reanimating some gameport rudder pedals for DCS.<br></p><p>In the next part I will use an old MS Strategic Commander for absolute view control with programmable presets...<br></p><p>I<br> used to work with Digisparks but they are limited to two axes if you <br>connect them via USB. Maybe I will multiplex around that problem, but I <br>learned a lot tonight and I really like the power of 32 buttons and 8 <br>axes. Thank you very much!</p><p>Regards,</p><p>Andy</p>
<p>just i test this and found arduino micro controller and buttons, but how add and use all axis 12 pcs analog input pin to potentiometers ? i try add pot but no response no found. pot only do button 3, on/of game controller.</p>
<p>I recommend going through the Analog Read Serial example on the Arduino website (https://www.arduino.cc/en/Tutorial/AnalogReadSerial). If you can get that working, you can use that value as an input into the Arduino Joystick Library.</p>
how add joystick code this, i no understand good ? i need only read all analog input and then send data to game (FSX) simulator need read data what joystick do, <br>i need add arduino micro to usb-hid read all 12 analog input and flight simulator X use this, yuoke,trim wheels,rudder,trottle,propel,mixture,brake toe, test and micro show my device manager ok, and game controller show too good. <br>have you example one potentiometer how add to micro and working ?<br>then i maybe can understand and add all more axis and sliders to micro.
<p>I am currently finalizing Version 2.0 of this library. Once that is finalized, I will try to do a Instructable on using a potentiometer with the new version of the library (it may take me a while to get around to it, though).</p>
<p>i no understand thats do 3 arduino micro joystick my device manager, and run something tes prcedure move joystic but i no have connect any potentiometer and button this ? how made own program and add pot this ? i know only pot left to 5volt, right to gnd and center to A0-5 or 12,11,10,9,8,6,,4 pin but how code read analog pins ?</p>
<p>Help please...this is exactly what I need to set up a 6 x 6 button matrix to interact with a flight sim. I am running 32 bit Win 7 and using IDE 1.6.6 (after failing to get the other instructable to work with 1.6.4) and using a genuine Arduino Micro. However, the code will not compile. The Micro is selected in the IDE, the correct port is selected, and the library is in the correct file location. Any ideas? ee the error message quoted below.</p><p>&quot;Arduino: 1.6.6 (Windows 7), Board: &quot;Arduino/Genuino Micro&quot;</p><p>In file included from C:\Users\User\Documents\Arduino\libraries\Joystick\examples\UsbJoystickTest\UsbJoystickTest.ino:9:0:</p><p>C:\Users\User\Documents\Arduino\libraries\Joystick/Joystick.h:31:2: error: #error. The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).</p><p>#error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.).</p><p>exit status 1</p><p>Error compiling. &quot;</p>
Try using the latest version of the Arduino IDE (Version 1.6.12 at the time of me writing this reply - https://www.arduino.cc/en/Main/Software) and see if that fixes your issue. If it does not, please let me know.<br><br>Thanks,<br>Matt
<p>pins A0-5 analog and 12,10,9,8,6,3, must be have analog input how ?</p>
<p>i use buttons/switch mobiflight software and wery easy use, just made FSX radio com 7-segment displauy radio /nav/com 1 all and working ifne, but proplem have mobiflight NOT support analog input, buttons and switch can add all maximum what arduino mega have easy to use.and support many mega board sametime, </p><p>now need only analog input and hat switch to arduino micro board, how make ?</p>
<p>Hi, im totally newbie whit arduino but i need help, how make my arduino micro(or mage 2560 whit ch340 chip) or nano, or uno, i have.</p><p>but i buy micro this use. i need only all maximum (micro 12 pin) analog input and hat switch only, no need other buttons/switch.</p><p>how make code ? how burn code ? what program i need ?</p><p>if someone can made to me code help lot me my FSX simulator i need make own joystic,bedal,trottle,trim wheel, hat switch,etc. proplem have my project how made all analog input uses ?</p><p>i looking many month now google but not found help, all have only 3 or 6 or little analog input :(</p><p>hardware i have win 8, flight simulator X gold, arduino micro (or mega 2560 if can) and 12 analog input need +HAT switch. no more . </p><p>how made ? im titally not understand code lot :(</p><p>thanks regard matti.virta </p><p>send mail my gmail name have this. you know. if you can help me project. </p>
<p>If anyone experiences the issue I just listed then upgrade to 1.6.12 and hopefully the issue will be solved as mine was. </p>
<p>Hi @<a href="https://www.instructables.com/member/MatthewH" rel="nofollow">MatthewH</a> , PLEASE Please Help!!!</p><p>Thanks for awesome code and tutorial. I'm very new to programming and Arduino and I'm slowly getting hooked. All I want to do is have all 32 buttons and both Hat Switches working as keys. So it will give me 40 push buttons.</p><p>I'm planning to make a simple (mfd: multifunction color display) key pad for a flight sim, something like: http://www.thrustmaster.com/products/mfd-cougar-pack</p><p>I can get a basic button matrix to work, no problem but:</p><p>I can not for the life of me get the bottom matrix to work with your setup... I think I'm way too new to this! </p>
<p>@Schalkdk You will need to send me more details. You can send me a private message with the details (specifics about your hardware, what pins on the Arduino you are using, your sketch file, etc.).</p>
<p>Hi Matthew,</p><p>I just created a <a href="http://rc-aviation.ru/mtech/34-komptehnology/1847-usb-pp-arduino-pro-micro" rel="nofollow">publication about PPM to USB joystick converter</a> (in Russian) about application where I used your library. Thank you very much - it works fine. :)</p><p>I want also to share very important knowledge - if someone going to use the library with popular board <a href="https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide" rel="nofollow">Arduino Pro Micro</a> he has to <strong>solder a capacitor 0.1 mkF between the board RST and GND pins</strong>. </p><p>The problem is Arduino Pro Micro works 'almost' as genuine Arduino Leonardo or Arduino Micro but it is too fast in initialization. It start to communicate with USB before the USB driver on the computer finished its initialization which <strong>can result in communication error</strong> (communication LED on the board blinks slowly, communication speed slow down, etc.). <strong> </strong>The attached capacitor slows down the board initialization and after the soldering such problem disappears. </p><p>Andrey</p>
<p>What is a 0.1 mkF capacitor in uF? Thank you,</p>
<p>0.1 mkF is the same as 0.1uF. Andrey is Russian. mk is micro, F isFarad </p>
<p>Thanks for sharing your findings about the Pro Micro.</p>
<p>Hello, this library is perfectly and exactly what I'm searching for. At the moment I try to program a gamepad for the recalbox on a raspberry pi. My only problem is to handle the bouncing of the button and the joystick. When I push the joystick one time, the Leonardo runs the loop more than one time and the recalbox register 5 to 6 movements so that the controlling in the recalbox is not possible. I try to handle this with a delay from 150 but with this delay it is not possible to use the joystick for movement and the buttons for example shooting &quot;parallel&quot;. How I can handle this problem with this library.</p>
<p>You are correct, you do not want to use the delay method to<br>debounce your buttons for a game controller project. The Arduino website has a<br>tutorial on how to debounce a button without using the delay() method. I use a<br>slightly different technique in some of my joystick projects. If you take a<br>look at the sketch files in the following two instructables, you can see how I<br>handled the debounce issue: <a href="https://www.instructables.com/id/Ultimate-Classic-Game-Console-Joystick-to-USB-Adap/" rel="nofollow">https://www.instructables.com/id/Ultimate-Classic-Game-Console-Joystick-to-USB-Adap/</a><br>and <a href="https://www.instructables.com/id/Classic-Joystick-to-USB-Adaptor/" rel="nofollow">https://www.instructables.com/id/Classic-Joystick-to-USB-Adaptor/</a>.<br>Basically what you want to do is read the value of each button line every time<br>through the loop and add them up. Then every 10 milliseconds (which can be<br>determined by a call to millis()), I check the sum and if it is over some threshold<br>value (e.g. 4 or so) I consider the button pressed for that 10 millisecond<br>frame. I then reset my counters and start adding again.</p>
What the code I must write?<br>I write this code but is not work normally<br>#include <br>int asu = A0;<br>int sensorValue = 0;<br>void setup() {<br> // put your setup code here, to run once:<br>Joystick.begin();<br>}<br><br>void loop() {<br> // put your main code here, to run repeatedly:<br> sensorValue = analogRead(asu);<br> Joystick.setXAxis(sensorValue);<br>}
<p>Have you tried debugging your code to see what value sensorValue is after the analogRead call? The current version of setXAxis expects this value to be between -127 and 127.</p>

About This Instructable

96,754views

99favorites

License:

More by MatthewH:Create a Joystick Using the Arduino Joystick Library 2.0 Remote Control Your Raspberry Pi Ultimate Classic Game Console Joystick to USB Adapter 
Add instructable to: