Controlling X-Plane 11 with an Arduino Leonardo Microcontroller and Sketch Code (OCT 2021 UPDATE)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and a warm welcome to fun of flying if you're a regular visitor to this channel you'll be aware that i've spent a lot of time over recent months preparing several videos on the subject of touch portal which is an excellent desktop application that once configured allows you to control x-plane 11 with your ipad or even your mobile stroke cell phone if you wanted to now i know that many of you have enjoyed those particular videos and i thank you for all of the kind comments that i've received as a result however whilst i've enjoyed working with touch portal tremendously i have unfortunately in the process rather neglected other visitors to my channel who are very keen on building home cockpit setups with the view to controlling x-plane 11 with physical hardware such as toggle switches and push buttons for example as a consequence of this i thought that i should now dedicate some of my time into looking at once again the concept of using arduino micro controllers and ide sketch codes as an interface between the two things i.e the physical home cockpit hardware and the virtual cockpit software of x-plane 11. you may recall that i posted a youtube video on this some six months ago and whilst that video is still relevant i thought it would be best to produce another more up-to-date version of it as this to my mind will serve two purposes firstly it will act as a refresher to everyone including me as to how the hardware wiring circuits are configured between the microcontroller and the various toggle switches and also how the arduino sketch code is written to ensure that the physical switches actually do what they're supposed to inside x-plane 11. secondly it will prepare everyone for my next short series of arduino based videos which this time will demonstrate how we can use toggle switches as for the previous video but not with just one micro controller but two one of these will be an arduino leonardo acting as a master microcontroller and the other will be a mega 2560 acting as a slave microcontroller however for now as the master stroke slave arrangement is far more complicated you understand let's get back to the present as we really need to learn how to walk before we can even start thinking about being able to run so in this video then i'll be showing you how to connect seven toggle switches to an arduino leonardo microcontroller in terms of the physical wiring without using a breadboard and i'll also show you how to compile the ide sketch code required to make them all work in x-plane 11. i will then use this hardware stroke software configuration to operate the corresponding switches in a virtual cessna 172 okay so here we have our theoretical uh wiring schematic and we've got seven toggle switches here beacon lights landing lights nav lights strobe taxi lights fuel pump and pito heat and then we have our main leonardo microcontroller board here excuse me the micro controller board has a micro usb socket on board and you need the corresponding cable to connect to your desktop pc the uno and the mega have a much larger d-shaped uh usb port on here but this one's very small um so this cable here provides for a five volt supply to the board from your desktop and also allows for serial communication from the board back to the desktop and back again on this side you've got a ground circuit here in black and it picks up one side of each of the seven toggle switches and on the other side of the toggle switches you have your signal cables coming down back to the board and today we are using pins four through to ten now you may have heard me refer to the to this breadboard um which i'm not going to be using today bread balls are good for testing um testing out circuits on a small scale but you certainly wouldn't want to include a breadboard in a major real world home cockpit setup you need something a bit more permanent than that and that's what this diagram is representative of you also may have heard me refer to pull down resistors before especially when i've been working with circuits on the breadboard and that pull down resistor will be on this side of the switch between this terminal and earth and the reason those pull down resistors are there in the first place um is to ensure that the micro controller knows exactly uh whether this switch is on or off apparently when the switches are physically switched off uh if this pull down resistor wasn't there then the leonardo wouldn't know that necessarily and it might give you some spurious results and start flicking this switch on and off all on its own now as you can see on this uh diagram there are no uh pull down resistors shown and that's because i'm doing it in a slightly different way i'm availing myself of a pull up resistor that's integrally built into the micro controller so when i set up each of these pins in the ide sketch code a bit later on i'm setting each of these pins as an input because it's taking input signals from each of the switches and i'm also setting them up so that they take advantage of the uh onboard pull up resistor to overcome any issues with the control the microcontroller not knowing exactly what these switches are doing okay so i think that's about it and i'm now going to show you what this looks like in practice because i have created a little test board with all these components on and you'll see how it looks in the real world [Music] right so this is the test board that i was referring to various shots of it so you can see what it looks like um i built this specifically for this video so that you get a better idea of how all the wiring goes together and also i'll be using it for the next couple of arduino based videos where we go on to talk about um having the same sort of toggles which is working through a master and slave microcontroller setup so in terms of how this works then i'll try to label everything up so you can identify the various components quite easily but at the center of everything on this particular circuit we have the arduino leonardo micro controller and that is connected to my desktop pc by a micro usb cable which supplies five volts to the microcontroller board and also allows two-way serial communication between the board and the desktop pc to the bottom left of that i have a power indication indicator board sorry which is just a little device i made up with an led light to show me when the leonardo is getting its 5 volt power supply to the right to that we have a a ground distribution board that i i made as well because trying to take ground cables from seven toggle switches back to the leonardo that only has a couple of ground pins will be impossible so all of the black cables coming from the bottom of the toggle switches go to the distribution board first and then just one single cable ground cable goes back to the leonardo [Music] then we have our seven toggle switches and as you can see um you've got your ground black cables coming off the bottom terminal all of the switches are single pole single throw um the top terminal of each of the switches um goes to the back to the leonardo via the signal cables uh the colors of the wires are are not the same as the um theoretical sketch that i showed you before so i just i just didn't have enough colors to go around so i've had to use some colors more than once but no hardship and so each of those signal cables goes back to what do we say pins 4 through to 10 on the leonardo [Music] at the top of the board um is a mega 2560 it's not being used here in this particular circuit um but it will be used uh when we come to start looking at the master and slave microcontroller arrangements in future videos so having explained the board layout um and the physical wiring and everything else um i think we now need to go and have a look at the arduino ide sketch code that is going to make all of this work as intended [Music] okay so here we are in the arduino ide desktop application and this is the start of the sketch code that we'll be using for this particular project as you can see it there is quite a few lines of code down here all the way down hopefully this scrolling doesn't make your eyes go funny sometimes does for me um but for the uninitiated i guess uh this is going to look particularly uh complicated and confusing and to be honest with you uh when i first started doing this that's exactly how it appeared to me complicated and confusing and i wasn't sure what i was doing to start with um it's taken me literally hours and hours and hours and hours to find out how to put this code together you'd be surprised how little information there is on the general internet with regard to writing code for x-plane i don't know anybody else that's writing code like this specifically for home cockpit builds so that the code itself acts as an interface between the hardware and the software not seen anything anywhere as i said in the last video there are a few people that have their own dedicated website to this like uh xc or what was simv or some other name now um moby flight and bits and droids and people like that but they're doing it a slightly different way they're not they're not showing you the code itself they they're writing their own firmware um and i didn't want to be beholden to them uh taking advantage of their good works um i wanted to be able to do my own thing and so this is what i come up with and it actually works um i've tested it and i'm very happy with the outcome anyway so at the beginning then it says up here that this sketch code is has been tested with the arduino um leonardo mic controller only and it will only work uh with the leonardo it won't work with the uno it won't work with the mega 2560 and i'll come on to that in a minute uh this bit here obviously the sketch was written by myself and it's my intellectual property but having said that i am prepared to release this to the wider flight sim community on request um i'm not going to release it to the world uncontrolled but if anybody wants to take advantage of this code then please just send me an email [Music] and i will release the code to you there will be a couple of conditions which i'm sure you'll understand and i'll come back to that again a bit later on so moving down we have a couple of notes here um for those of you not familiar with writing code it is possible to make notes like this and completely block out um that uh block them out from the actual code itself when it's running so the program will ignore anything up here by putting a forward slash asterisk at the start of a some text and a asterix and forward slash at the end of the text if you want to do a big paragraph of text there or for any line of information you can put two forward slash characters at the beginning and all of that will be ignored by the program same here like this i've just put these dotted lines and section separators in to break up the various sections of the code so it makes it easier for me to explain what's going on and equally down here any of that will be ignored courtesy of these two two forward slash characters at the front of every line okay so moving down a little bit we have this lovely colored bit of text here which is a part of the sketch code and will be included in the program and that is something called a library it's a keyboard.h library so what's a library well a library is a collection of sub code if you like which that you can call on to make your particular program work or communicate more easily with third-party devices like um sensors or liquid crystal displays or other things of that sort and as an example here we have one called liquidcrystal.h library and that makes it easier to talk to guess what lcd displays in our case we actually need one called keyboard.h library because i'm going to ask the leonardo to communicate and send keyboard signals to my desktop computer and then and then into x-plane because that's how we're going to get the switches in the virtual world of x-plane to actually work issue keyboard commands um all of these libraries that ship with arduino by default can be found in the program files 86 go to the arduino folder libraries folder and here are the ones that ship automatically with arduino that's the liquid crystal one that i referred to earlier and this is the keyboard one and if you go double click in here and then again in the src folder there is your actual library dot h [Music] okay now when you ask for a a library to be included in your code you have to write it like this so it's hashtag include um and then these uh square brackets on either side of the word keyboard.h and it's important to note that when you type this into your code the first letter of the name of the name of the subfolder has to be in uppercase so a capital k like here like it says and the same rule applies with any uh library that you call on if you don't have it as a capital or uppercase first letter then it won't work now as i alluded to up here the keyboard library only works with certain um microcontrollers the leonardo the micro zero the dua and mkr mkr models all installed with the 18 mega 32 u4 processor sounds wonderful no idea what that is but apparently that's the case it does not work with the arduino mega 2560 the uno and probably um other ones as well [Music] as i said before the keyboard dot h library ships with arduino so you haven't got to get root around and go and find it it's already there but if you want uh to find libraries that are not that don't automatically ship with arduino you can go up here manage libraries and all of these come up these are probably written by people around the world nothing to do with arduino some of them um but they're all designed there to work with third-party devices or to help with your code to communicate with us with these devices and there are hundreds of them in here absolutely hundreds anyway let's not get too far ahead of ourselves what we need is the keyboard.h library which we've already called on and now we are free to move down through the rest of the code [Music] okay so as i've probably um alluded to before um i am a 65 year old guy a retired pensioner here in the uk and throughout my some 45 years of career i've had absolutely no experience whatsoever in writing computer code absolutely nothing and it wasn't until i started looking into arduino about a year ago that i started learning a few bits and pieces that helped me with my particular projects um namely uh getting hardware to talk to the virtual world of x-plane using a sketch code interface now i don't know much about arduino other than that there's a whole load of things that i could uh pick up later on but for the moment i know enough to get this um code to work and it does work and it's been tested so please forgive me if my descriptions aren't uh exactly professional so we shall we say um but i'll i'll try my best so in the first part of this code we're setting up some reference points in memory um and the data type for that is is like a it's called an integer and an integer is a whole number without decimal places um so we're setting up constant integer constant and ordinary hints and the only difference between these two as i understand it is that a constant int sets up number storage in memory which is read-only can't be changed unless you change the program and ordinary integers are right read write memory which means that these can be changed throughout the program i don't get too hung up on this i don't really know enough about it to be an expert but this is how it seems to work so up here we're setting up a constant integer for seven things the seven pin numbers that we have our switches connected to four through to ten um so these uh pin numbers will remain in the microcontroller's memory forever unless i rewrite the program so and they will always be referred to throughout the rest of the program um i've given them a description each which is simply switch bcn pin in this case here and that's a completely arbitrary description you can call it whatever you like but i've tried to follow a format so that i know exactly which switch is connected to which pin so switch simply for toggle switch bcn short for beacon and pin is pin four and the same down here landing uh lights nav light strobe lights taxi lights fuel pump and pito heat so down here we're setting up some more memory fit or data storage in memory uh for further integers straightforward integers and we're given a description for each of these uh switch beacon state or last switch beacon state um i'll come on to this a bit later but essentially as the program goes through the loop later on um it will be asked if the switch state this time it reads it is different from the from the switch state last time it read it won't get hung up on it now but that's what it's for um and again i've given these uh arbitrary descriptions so um switch beacon as up there state and last switch beacon state and i've done the same thing for each of the switches down here [Music] so then we come down to the void setup section of the program which is basically an uh part of the program that initiates uh initializes pin modes and libraries and things of that sort as i understand it um this part of the program will only be read once um after each power-up or reset of the arduino leonardo board um we start with something called a serial.begin um and a baud rate which is a bits per second communication rate um basically this enables you to open up the serial monitor which is up here so that you can see the results of your program being displayed on the screen if that's if that's how you've written it um then down here i'm setting up pin modes for each of the pins that i'm connected to four through ten and i'm saying uh for let's say for this one here uh pin four for the beacon lights i'm setting this pin for as an input uh on this cat in this case and i'm using the put integral pull-up resistor which you may remember me referring to earlier on in the video and i've simply done the same thing all the way through for the other pin numbers so they're all considered to be inputs that's how the programmer read them and the program will expect to receive some information on each of these pins and in this case it will be a voltage or not voltage or high voltage or low voltages i should say or a digital one or digital zero so let me come down to the void uh void loop and as the name describes it it's this part of the program is a continual loop i don't know how many times the second it goes around and around and around but each time it comes around it reads uh this the digital it digitally reads i should say the switch input for pin four and five and six and seven eight and nine and ten and it's looking for a higher low voltage or a digital one or a digital zero and it does that every time however many times a second it does it it does it every time for each of these pins so it now knows whether there's a high or low voltage at each of those pins and we come down to the first section here and it's basically saying for the beacon lights which is this bit here saying uh if the switch date as the program loops around is it different here is the switch date this time different from what it was last time and if it's not different it will ignore anything down here it won't do anything but if it is different it's probably detected that the switch has been moved from the last time it was checked this time and if it has been moved that means the intention is to switch the beacon lights on or maybe off depending on where it was where it was the last time but it says if the switch beacon state is high i.e there's a digital figure reading of one then press keyboard command c lowercase c and if not then press c again so if it's high we want the switch to be on so we press c if it's low we want the switch to be off and we press c again why are we pressing c twice because that's how x plane 11 is set up if you're familiar with x plane 11 then you'll know that you press the c keyboard command to or c in my case i have to explain that c is how i've assigned it you may have assigned it with a different letter or different sequence of keys but it's whatever keys you use to switch the beacon toggle switch on you'll use it again to switch it off because that's how it's set up in x-plane there are some switches where you use a different uh keyboard command for on and another one for off but in the case of the beacon light switch you use the same keyboard commands both cases okay so um we've got through that and we have a slight delay here then we come to this bit so it's determined let's say that the switch state this time is uh different from the last time so it's issued a command one or e either of these and if we didn't do something else now then every time it came through it would issue the command c it wouldn't stop it would always be doing it so we put this bit of text in here this bit of code and basically this is the program saying okay we've done that now let's make the current switch state the same as the last switch state so it's basically turning this bit off and when it's done that it says okay release all keyboard commands so it's basically stopping the letter c from being issued or the keyboard command c being sent to x-plane yes not easy to understand but essentially all the rest of this down here is the same thing but for all of the different toggle switches so landing lights nav lights strobe taxi fuel pump and pitot heat okay if you're confused then i completely understand the only thing you can do is to have a play with this and and see how you see how you get on um just a little note here with regard to these curly brackets um i've had no end of trouble uh when compiling code where i'll come up with an error and it's not exactly obvious what that error is but in most cases it's because i haven't got a curly brace somewhere in the program and as a rule of thumb from the start of the void loop here including that curly brace right the way through to the last curly brace there should be the same number of curly braces facing right as there are curly braces facing left the position of the curly braces is basically so you can set up commands in sections so this curly brace here will go with that one so all of sorry that one there we'll go with that one that one goes with that one and so on and so forth and ultimately all of that is included in those but it just it just splits up parts of the code between the brackets and doesn't try and do everything all at once so just be aware when you when you're messing around with this stuff uh just make sure you've got the same number of curly braces facing right as you have going left from void where i'm avoid loop position all the way down to the end good luck with that okay so that's the code um uh what we're going to do now is go i'll open up a session of x-plane and we'll load a cessna 172 and i'll overlay on that video some further video of the of the test board that i've made and the switches toggle switches so you can see them working and you can see what they and what each of them does inside the virtual world of x-plane so let's let's go over and do that [Music] okay so here we are in x-plane i've got a little cessna 172. i'll just have a look down here at the switches i can dismiss the yoke so we can see them clearly i'm going to move my cursor out the way so nobody can say that i was using the mouse to turn these switches on and off i'm sure you'd believe me um but here we are again you can see the uh inlaid video um with footage of my test board and all the switches which is powered up hopefully you can see the little red led light my keyboard is out of reach so i can't be using that either so we'll just go through the switches one by one starting with the fuel pump there we are on off on off then we go along and we have the beacon lights on off on off next one along landing lights on off on off and then we go to the taxi lights sorry nav lights uh strobe lights and lastly peter heat and off and there we have it turn them all on in quick succession turn them all off again no delays nothing all seems to work as programmed which is excellent news i'm so pleased with that okay so um that brings us more or less to the end of the video you can see it all working and we'll just have a few final comments to wrap everything up okay see you in a minute so earlier on in the video i said that uh i would make this sketch code uh available to the wider flight sim community and i was definitely being sincere when i said that and if this is something that would have be of interest to you then please by all means uh send a request to my youtube channel email address which is fun or flying at gmail.com and i will send you a copy and you can use it at will however there are a couple of caveats that i would like to just go over firstly you should be aware that my videos and any contents contained within them are subject to youtube copyright so with that in mind could you please not be tempted to re-upload this information back to youtube as being your own work morally i don't think that's the right thing to do as it has taken me a long time to put this code together and i'm only issuing it out to those flight simmers that want it for their own personal use if you could just abide by that simple rule then that would be fantastic [Music] secondly the code as written will probably not work with your particular setup the keyboard commands that i have chosen to assign to my aircraft and the pin numbers that i've decided to use on my leonardo microcontroller will all be or probably will all be different to your own particular arrangements so if you do use this sketch code for your own use you may have to go in to change some of the code to reflect your own keyboard commands that you've set up and also the pin numbers that you've chosen to use for your leonardo microcontroller having said that if you still want to give it a go then please by all means let me know if you have any issues and i will try and help of course [Music] okay so that really brings us to the end of this particular video and as always if you enjoyed it or it's um of some assistance to you then please uh hit the like button and also don't forget to click the bell and subscribe at the same time so you don't miss anything going forward as i said before if you have any questions then please let me know and i'll try to answer them and i thank you for your attention so that's it from me for now see you next time tata for now
Info
Channel: Fun of Flying
Views: 361
Rating: undefined out of 5
Keywords: #Arduino, #Fritzing, XPlane11
Id: OS_m8SY2oH0
Channel Id: undefined
Length: 36min 19sec (2179 seconds)
Published: Fri Oct 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.