Arduino Joystick 2.0 Library - Beginners Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys daniel here and this is a quick guide on the arduino joystick 2.0 library now if you're here you're probably looking to make your own style of games controller with some extra buttons so normally i like using an xbox one elite but particularly because the extra paddles so if you want to do something similar to that or like me you're actually looking to build a semi-accurate but budget friendly hands-on throttling stick hotus flight control system or steering simulation whatever you're after that requires multiple axis inputs as a windows hid device human interface device even that's really what we're looking to do with this kind of project so initially your board choice will affect so i have three arduino boards here only one of them is usable though so i actually have an arduino nano lovely form factory would fit beautifully into a board but it is the wrong chipset it is uh if i get the wrong way around mega three two eight p a u that will not recognize by windows as as a human interface device it's good board it's nice and small does the job not for this project though likewise we have a larger board we have a mega 2560 lots of analog inputs which is great for this kind of project but again it can't pass the information as an arduino hid what we are using instead is an arduino pro micro the pro micro uses the correct chipset which is a mega 32 u4 chipset that is what you need you'll see on the leonardo you'll also see it on the arduino duo which is one thing i will be doing i will be using on my one of my units i'll be building and it's a similar form factors megas you've got lots of digital pins and you've got lots of analog pins as well uh there's plenty of flexibility there you can also expand it with some other setups so with our body in place we've got a board choice what makes you want to connect to it well primarily we want to connect some stuff like if i show my pre-wire here a joystick now analog inputs can do anything with potentiometers so i actually have a joystick but i also have stuff like these faders here these faders are going to be used in our example and they are just potentiometers you have wiper which is that pin in this case up in there which goes between two other points and changes the resistance between the two now in order to wire these up you have to wire them up in this particular setup we have our fader itself the two sides of the main contact are connected to the five volts there's five fold going through entirely and then you have your wiper contact which is going to go to your control signal to analog read and what your arduino reads is it reads whatever the voltage value is between the wiper and wherever it is on the board so either all the way down to zero volts by the zero the ground fader all the way up to five volts on the positive where the power comes in in this case it's using five volts because we're using a reference design on the board we're also going to use some buttons as well now i have the button on the joystick but i've also got this lovely red button here and i've got this toggle switch as well and we're just going to use them as standard digital pins so i'm going to quickly wire that up on a breadboard and then once i've got it wired up on the breadboard i will be able to run you through what it's connected to and then how we're going to code it so here we are we have our joystick over here which is connected if i could just focus the camera quickly a common ground a second it's got a common ground and a common dc line which going to both of the wider edges of the wiper pins and the wide pins themselves have got their own power supply the switch is also like the white fins themselves are then going to our inputs and then we've got our switch going into the rest of it as you can see over here we're actually connecting the wiper pins to the analog inputs on the board and then the power and that is going to go on the same with these other faders power and ground going to power and ground buses in this regard and then their contact pins are going to their analog inputs on the board i apologize for being slightly out of frame our switches have a similar setup they have got themselves between the ground bus and their allocated pins so when they are connected they will short in this case when they're connected they will create a short and that will trigger the pins i've done the same my push button now we are using a latching switch here which um is literally going to behave like a momentary button that's being held down the entire time there are ways to change this but at the moment this is the configuration i'm going for in this basic input just to show you what you can and can't get away with so we're just going to quickly uh pop into software and once we've popped into software here we'll be able to show you how we're programming this and some of the nuances of the arduino joystick 2.0 library that it has when you're programming a joystick okay so here we are in software quickly um this is the library we want we want the arduino joystick library by m hieronymus um he's got some information on here already and there's some on the wiki but the wiki is a bit sparse in some places so i'm just going to go through our setup and how we're going to do it so we're going into the ide quickly i'll just get up the actual setup we've got here on camera so within the id what we're going to be doing is we're going to do some initial setup so i've already got this copy and pasted but we're going to first of all include the library so include joyce joystick.h you install this like you would any other library not sure how to do that there are plenty of tutorials on that and then what i've got here is i've declared my input pins so in this case i'm using the x and the y pins on the analog 0 and analog one i'm using my rotation which is why i'm putting that fader to rotation zed as analog three and then joystick the throttle which is that one is on on analog two the buttons are where they are so the button from the joystick is in on pin nine the switch is in on pin eight and then the red button is in on pin two so what comes next then is a monstrous of setup we actually have to set up our joystick and i've just copy and pasted this because it is a huge function so i'll split it up into two lines of people to read a bit easier so to set up the joystick we then have to close our joystick so our joystick underscore joystick is being declared so we're declaring our joystick here first of all we have to put a human interface device identifier here now there is one that's built into it by default but if you're doing something like me i am where you want multiple of these boards plugged in you need to give it a unique id in this case i'm going to go for hex value rx i'm going to have ox 50 because that's what i don't have in use joystick type so i'm going to do all caps because that's what it requires joy stick underscore type and i'm going to define it as a joystick there is the other values they've got is gamepad um gamepad or multi-axis controller but we want a joystick in this regard because we're using multiplexers we want it recognized by game controllers and the multi-access controller is more useful as other hids but it doesn't wreck windows doesn't recognize it at least from my testing as a standard human interface games controller so you'll need to go into a different way if you want to use it in that regard next we set up how many switches we've got so we've got all buttons we've got so we've got three buttons we're going to close that you can have a significantly larger number i think the current operational number is 55 you can go well above i think people could even have three multiplex up to 255 buttons if you want to go for a single board controlling all of you like your whole flight sim card kit you're building but then you have to allocate it all manually anyway so that's entirely your call we're using three in this instance we're not using any hat switches currently you can have up to two of them now what you then have is the number of actual buttons you have the number of axes you can include and what you're including so in this case we're including the x-axis i can't spell true we're including the y-axis we are not including the z-axis and we're going to the next line of us here we're not including the rotation on the x-axis we're not including the texture on the y-axis either we are however including it on the zed we are not including rudder we are not including throttle we are including throttles this is a tree where they're not including accelerometer where they're not including break and they're not including steering now if you miss any of these values out in this order so if you just wanted to go for a single identifier and then joystick type everything else will be auto assumed so that one it assumes default 32 that for buttons it assumes it's default of two hat switches it then assumes all axes are present as well which can get a bit messy in the windows viewer you can view obviously in separate bits of software if you want to view more than the standard 32 buttons one hat switch and six acts i believe it could do six seven eight axis by default so we've now initialized the joystick next we need to do is to initialize the send state for the joystick stuff so constant bull init auto send state equals true i'm gonna quickly put my comments in here this is a simple value and all this does is this makes sure that the board is constantly sending information it's not being it's not requesting polling it's being sent across automatically now i'm going to set our axes up so instead of our buttons up as well so integer value x-axis equals zero and these are just defaults and i'm going to copy the rest of them across from my other document now what you will notice as well is that i'm using underscores here this as highlighted is a reserved value so by putting whatever you're identifying your axes as for your memory whatever you're remembering further on down in us your code you need to make sure you use an underscore after it if you're declaring something like x-axis or y-axis i also need to clear our buttons here as well so i am going to copy this across quickly from the document just save time we are declaring them as last button now i'm doing its button state one button state two button so three because i only have three buttons but in a more later tutorial i will go on about using arrays in this um for more complicated button arrays and setups now we're on to our void setup loop so the first thing we're going to do is we're going to initialize all of our pins we're going to do the standard pin mode configuration uh so pin mode pulling up to my previous allocations up here so joy button one down here is allocating to our pin nine where we have our first joystick button one and we're using the pull up configuration for these pins next thing we're going to do then is joystick dot begin open close brackets because we're not choosing any modifiers in there comma that is our start setup now we're into our primary loop so i'm going to go through one axis and i'm going to copy the rest of them in so our axis we're going to do axis runtime here on each loop of this loop it's going to pull all of our buttons on our sensors for information it's going to figure out what state these which what values these guys are in what state the buttons are in and then it's going to feed that into the hid interface for windows so for our axis we're going to do our x-axis underscore that is going to equal analog read and we're going to read joy x which was our configuration for the pin on analog zero within joy x we are then going to map this underscore map we're going to map our x-axis input to a from 0 to 1.023 which is our default analog input two zero two five five we're going to actually bring it in in the resolution mostly because for joysticks we don't want them to be stupidly sensitive if you imagine there's a joystick if this you imagine the screwdriver is a joystick and that's our input if we are moving it like to the left if we're pulling it to high right because of how people move obviously and the in discrepancy of the voltage as well at this voltage it can get a very jittery and if you're controlling an aircraft the last thing you want to do is a lot of jitter at least in game so now we've pulled the axis we're then going to set the joystick axis to that so that will be joystick dot set x x axis 2x axis underscore i'm going to make sure i've got my values in place and that is it per joist that is it per axis you read the pin you map it to a lower resolution for the voltage if you want to you could have a significantly smaller resolution if you really wanted to to make it more segmented if you want to use an analog stick feel but you wanted to give a discrete output that's entirely your call joystick set as well the axis value depends on what you're doing so we're then going to quickly copy these other ones over so i've got the y-axis doing the y-axis things same basically all doing the same thing they're just doing their recording the odds that axis rotation says go in rotation z axis the throttle is an interesting one though because the joystick set throttle you want to make sure you're using your define descriptors we have up here so you can do steering you can do break so set accelerator rather than accelerate axis next are buttons now the commands for the command for this is quite interesting so the command for this is joystick dot set button that's it but we then define which button now the windows hid starts at zero so for us joy button one is actually going to be zero so in this case we would check which button we want we want buttons here on the hid and then what value we're setting it to and we're setting this to what the current button one state would be however again because the polling issues we want to make sure that a button state is coming from a constant variable not looking at what's going on on that button you can do it this way but this the you can do it by just constantly pulling the button but i find this a much more stable way to do it so we're going to create a temporary variable called int current button 1 state and we're going to get it to equal the inverse of digital read joy button one just because of the way we're doing our pull up pin pull up at the moment don't put a little if loop in here for if current button one state does not equal cut last button one state which was our initialization we put up here then we will then put in our button set command over here we'll set button zero to there from there and then we're also going to quickly go update our current button state so last button one state will equal current button one state and that's it so i'm going to duplicate them in from a again from this other document just for these uh other bits so we've then got initialization on combine two states joy button two same it's the same if loop we can do an array version as i said when i get the bigger versions coming in i will put them when i do the big version i do do an array for myself i will go through that code as well the final thing we want to do is change our poll delay or our debounce time this is quite simple this is just literally a delay i'm going to do this for 10 milliseconds and now this is important because the primary reason we are doing this delay is to ensure that we are not polling too quickly for pretty much information you have a risk of flooding the usb bus and then you're causing all sorts of problems so just quickly verify the code um before we upload it to our board and i will show you it working with the actual input controls so that's that done there and then just quickly flash this to our code i've made sure already i've got it going to arduino leonardo across com12 despite it being a pro micro it is using the leonardo frame base that's all done so we're then going to quickly test this using the windows controller usb gamepad setup and then you can see that said it's not really an audio we have a throttle control and three buttons so if i flick my buttons see it works on number two the red button works well on number three and that works well on number one but if i actually move my axes oh that one works quite nicely here but our z rotation does work nicely as well and our throttle rotation works rather nicely however if it doesn't you need to go through the calibration setup so settings calibrate and then you just follow this calibration wizard so center point handle we're just going to rotate our joystick around here now i'm just doing this loosely obviously have this point in push button down uh we're doing the center point for the axis calibration on the throttle so i'm just going to take the throttle control in here take it all the way down to zero take it all the way back up all the way down to zero again all the way back up and what we're giving this if i just show raw data on this one here we're giving it we can interestingly that's the point what we're giving it is we're giving it a maximum and minimum set of bounds and that is what it is using to declare what proportion of the throttle or the axis is being used so push next there and finish it and that's all set up so our joystick is then mapped as it should our joystick button works our z rotation now we're locked in the middle here so you can see how that goes all the way to left and then that goes all the way to the right and it locks in the middle quite nicely likewise our throttle goes all the way down and goes all the way up now you'll notice the throttle when i move it to about here we actually hit halfway despite how it being three quarters this is because this is a logarithmic potentiometer now in the future i will go into mapping logarithmic controls to a linear scale by using the remap function but that's not something this tutorial if you'll find that you're not getting exact proportional response just swap this out for swap your fader out or your potentiometer out from a logarithmic one to a linear one obviously you'll notice that it popped out to zero because we didn't have any voltage and when the cable came out so that's pretty much it but we do have one more problem one more problem the problem is is that this is declared as an arduino leonardo now if we want to go for a high different board because we are using say multiple boards in the same thing like i'm going to be doing i'm actually use this little board to control a joystick part of a hota setup so that's hands on throttle and stick um i'm gonna then build the throttle on the separate board probably a arduino duo i need to declare these boards separately so the way to do that is quite simple we're gonna come into we'll get my windows thing across quickly we're going to go into our arduino folder go to our documents yes documents we are going to go to arduino and then here you have your libraries folder but you should also have a hardware folder so we are going to create ourselves a hardware folder we're going to create call it whatever we want here i'm going to include this folder in here on the on upload as well as the other sketch which has got significantly more detail to show the commenting of the code so the avr folder we have bootloaders which we shouldn't touch because this is the katarina bootloader for the leonardo type board and our boards.txt if i drag that across here you can see we have leonardoprojoy.name equals projoy brackets arduino pro micro this is my name for this board i've renamed it all leonardo pro joy because that's one name i could name that one for alright so if i actually quickly rename this system now we're going to replace pro joy not find and replace i want to do edit find replace we're going to replace pro joy with pro micro replace all instances in that we're going to keep that as pro micro i'm going to name this uh joystick uh we're going to follow this along as well so we're not going to tweak any of these settings here what we're interested in is the vid the pid and the usb usb project name and the usb manufacturer so i'm going to name the dcfa for dc freelance audio and then i'm going to go to rename this we're going to name this as pro micro joystick i'm going to quickly amend the typo in there and i'm going to keep these guys as they are because we've already got these calibrated to that but we could tweak that um to any vendor id if you're doing mobile onboard you want to change your uh vendor id defender id can say this line but your product id needs to change every time so let's go to 256 in this case save it and now we can create multiple of these in fact i could create another fault i could create another one of these i could just copy this across and do promo so i could call this pro micro uh joystick for example and put that in of every single one where you have that that you need to replace that or pro micro throttle you just need to make sure that they do exist as a board descriptor so i'm going to save this quickly i'm now going to go to our arduino test sketch i'm going to save this here i'm now going to close all instances of the arduino ide we have made sure to save this folder here so we're going to go to arduino we're going to open up our arduino folder it should then open up one of our projects it will then go to yeah open up my example project so on uh this is my better commented version so for my better commented version we're going to go to tools boards and you'll see joystick controller avr now exists pro micro brackets joystick so we're going to verify our code again shouldn't be any issues with this and then we're going to upload it now when uploading it i'm going to quickly go to the games controller and you can see right there on the windows setup it's we're setting up pro micro brackets joystick and that's it recognized right there now you'll see that calibration settings are off but it's recognized as a joystick it recognizes all three of the buttons it recognizes the power button it recognizes the joystick button we'll just have to go through our calibration guide again because we've given it a new id so windows recognizes it as a new device and we've got plenty of work stuff to work with here uh this is the throttle calibration so all the way down all the way up all the way down all the way up all the way down all the way up and then we could do the same with the z calibration all the way down all the way up all the way down all the way up i'm going to rest in the middle and then we click next and if we go back to the main menu there you'll see that the z rotation is now doing its thing as it should this is now doing its thing as it should and our joystick's not doing its thing as it should so that was my run-through of the arduino joystick library version 2.0 this is version 2.07 specifically i was using which needs a version of arduino ide above 1.6.6 i will be going through more complicated versions later on um in other tutorials about using more complicated uh potentiometers using hat switches uh matrix and buttons and then using arrays in the code for the buttons as i said i will include a link to the sketch in a bit more detail with a high level commenting that will be in the description as well as the example avr folder with the bootloaders needed for the leonardo pro micro duo subset even and yeah i really hope this has been useful to some people if you've got anything to add that you think would be useful or if you have anything any questions at all just drop in the comments and thanks very much for your time guys
Info
Channel: DCFreelanceAudio
Views: 24,302
Rating: undefined out of 5
Keywords:
Id: hoCOq9Ngp44
Channel Id: undefined
Length: 25min 21sec (1521 seconds)
Published: Thu Oct 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.