Xbox 360 controller support added to the USB Host Library
Update
A wireless Xbox 360 controller is now also supported via a Wireless receiver. For more information see the blog post.
Hi everyone. I just wanted to let you know, that I just added support for the Xbox 360 controller via USB to the USB Host Library. The code can as always be found at github: https://github.com/TKJElectronics/USB_Host_Shield_2.0.
Categories: Arduino, TKJ Electronics, USB


@Lauszus
Thanks Lauszus. Usb.Task(); gets run every time the loop starts. Actually, the loop starts and I run a function called serialListen (it listens for data comin in on the serial port, like acceleration info coming from the adxl from another UNO, via xbee). If an instruction comes in to rumble the controller, then scancontroller fires the delegate() function which will then fire the rumble. Then I run scancontroller(), a function that has at the top of it Usb.Task(); scancontroller checks the values of some of the sticks and buttons.
My code is very rough and is on github. I just started this project, I hope you don’t mind too much a thicket of code.
It can be seen here: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino
I was thinking I might try this with the ps3 controller later. See if it makes any difference.
P.s. That sketch will also turn on and off rumble if you push the Xbox button. I have found this will also cause the problem of controller reset.
@Blair Kelly
I think I found your problem. You are calling this: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino#L113 without checking if the Xbox controller is actually connected.
Also try to add a 1ms delay in your loop: https://github.com/blairkelly/stackedcontrol/blob/master/stackedcontrol.ino#L312.
Try to run Usb.Task() before you run serialListen(), as you are actually sending a command to the controller before you have run Usb.Task, as you execute scancontroller() after serialListen().
I found that it’s never a good idea to send multiple commands to the controller without calling Usb.Task in between. The problem is that you are not polling the input endpoint at the same rate as you are sending data.
@Lauszus
I am astonished at how much you help everyone out. Thank you very much for taking the time to analyse my code. I will try your suggestions this evening and let you know how I make out.
@Lauszus
I’m a bit embarrassed to say I was starting to run out of ideas when I just happened to plug in an external power source to the Arduino Mega 2560. It started working properly. I had ruled-out that possibility because it was working just fine with your example sketch. Perhaps the adequate power supply keeps the processes and communications from tripping up. Anyway, I thank you again for the time and effort you put into this and I’m sorry it was such a silly little thing I should not have overlooked!
@Blair Kelly
Don’t hesitate to contact me again!
No problem
Hi Lauszus,
Thanks for all of the work you’ve put in getting the game controllers working with the USB Host Shield.
I’ve been assigning the button presses to the digital I/O pins, and using the output to a relay board. Everything was working fine until I tried using the non-analog buttons. The comments in the XBOXRECV.h file starting at line 108 is a bit confusing and I’m not sure if what I’m trying to do is even possible.
Whenever I try to getButtonPress for a non-analog button (L1, L2, X, Y, A, B), I get no outputs. getButtonClick works obviously, but I’d like for a constant output while the button is pressed down. Is this possible with these types of button?
To clarify, I’ve been using an if/else function with digitalWrite to output low if the getButtonPress is true, else output high if the getButtonPress is false….using your example as a starting point. I’m new to all of this (you addind support for these controllers is really what brought me to the arduino), so if theres a better way of doing it, I’d appreciate it.
Thanks!
@Jerry
It was caused by a bug in the getButtonPress in the function, but has now been fixed: https://github.com/felis/USB_Host_Shield_2.0/commit/78cb524bcf8e78b65205b0c2d80f73748a998712.
Thanks for your feedback!
@Lauszus
Awesome! Thank you so much! Everything works perfectly now.
So it was just an addition of a bool command? I admit I tried troubleshooting it myself before I posted, but the .cpp file is well beyond my comprehension at the moment. Thanks again!
@Jerry
Yes I only added a bool cast. The problem was that it’s was trying to return a uint32_t, but the function is only a uint8_t type, so the top 24 bits where actually thrown away.