Controlling X-Plane 11 with Arduino Leonardo & Sketch Code (PLEASE ALSO SEE LATEST VIDEO OCT 2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone and a very warm welcome to fun of flying in this video we will be looking at another alternative way to control x-plane 11 namely by using an arduino leonardo microcontroller and some specifically written arduino ide sketch code to be honest with you and something i have to get out there right at the beginning is that i am in no way a computer language specialist and nor am i a programmer and frankly i didn't have the first clue about how to write code that would actually allow a bunch of physical switches to communicate with a microcontroller for that microcontroller to to communicate with a pc and for that then to go on to communicate with x-plane 11 itself initially when i began looking at the many examples of arduino sketch code out there on youtube it scared the living daylights out of me and my first thought was to leave well alone after all did i really want to invest a great deal of time learning about a computer language that i've never heard of before and then learn how to write the actual code that would do what i wanted it to do and the answer to that perhaps not surprisingly at that stage was a definite no [Music] basically i thought better of it and decided to stick with third-party interfaces that would do the job or one in particular anyway namely hixi hc sci or what was originally called simvim truthfully the hixi online configurator is a very good tool and is an excellent method of linking up your physical cockpit hardware to that of the virtual cockpit in x-plane 11. so why on earth would i want to then go off and reinvent the wheel in any event as i was and still am in the process of building a home cockpit setup that includes numerous uh push button switches toggle switches potentiometers rotary encoders and lcd displays all of which was going to be difficult enough to manage the last thing i needed was having to worry about writing complex computer code on top of all of that so over the next few months i just carried on carrying on as they say but there was always this nagging doubt in the back of my head and that was could someone like me and everyday joe with absolutely no computer programming experience whatsoever actually write an arduino sketch code that could be used as an interface with x-plane 11. well after pondering on this for some weeks i said well yes to hell with it what have i got to lose i'm retired i've got nothing better to do why not learn something new surely if other people can do these things then why can't i so off i went and the first thing i did was to purchase the elegu complete starter kit which included the arduino uno r3 microcontroller the kit also included numerous jumper cables and other small devices that would in turn enabled me to get started on my steep learning curve initially i thought that i could start learning how to prototype which basically means learning how to design small physical but temporary wiring circuits on what they call a breadboard all of which would then be connected to the uno microcontroller following that i thought that i may be able to take the step further and start writing small arduino ide sketch codes which would bring all the physical stuff to life and that's exactly what i did and over a period of months i learned a lot about physical wiring circuitry various electronic components and a lot also about arduino sketch coding and most of the information that i pulled together came from the arduino website as well as from video blogs that others had posted on youtube now whilst all this was very interesting and something that i had never done before it was at the same time getting a tad frustrating because none of it was really helping me to find out how to write sketch code that would connect all of my physical switches and hardware to x-plane 11 and this is where things started to get a bit difficult no matter how hard i searched on youtube and i spent some considerable time doing so i just couldn't find any specific information linking arduino code 2x plane 11. it just didn't seem to be there there are a couple of developer guys out there doing something similar such as bits and droids and another one called mobyflight but as i understand it they've gone a step further by writing both microcontroller firmware and user interfaces for other people to download and install similar to hixi or what was called simvim i guess for me though this isn't what i wanted to do basically i didn't want to be beholden to someone else's hard work i wanted the freedom to do my own thing and design things the way i wanted them which may of course prove to be a bridge too far i guess time will tell initially then i took account of the hardware components that formed a part of my home cockpit setup such as momentary push buttons toggle switches potentiometers rotary encoders and so on and then started looking into what arduino sketch code was available for these particular items obviously any code found at that stage wasn't going to work directly with x-plane 11 as it would need adapting but it would be an important starting point into my understanding of how each sketch code was put together and how each worked with the component that it was written for once i'd done that i then had to think of a way that the sketch code could actually communicate with x-plane 11 itself and as it turned out this part in theory anyway proved to be the most straightforward part of the process all i needed to do was to put theory into practice but how was i going to do it then i thought about how home desktop flight simulators such as x-plane 11 or microsoft 2020 function today yes you would of course need a joystick or a yoke to control basic flight as well as a throttle of some sort and possibly even a set of rudder pedals but many of the switches and instruments in a virtual cockpit are actually controlled by keyboard assignment so taking this a stage further the question for me was could i find a way of writing an arduino sketch code that would allow a microcontroller to recognize an input from a physical push button or toggle switch and then send a sequence of keyboard commands to my pc and then on to x-plane and the answer to this question quite remarkably was yes it could be done [Music] after months of research and throughout the initial development stages i eventually learned how to write a suitable sketch code that would send keyboard commands to my pc from a switch input but no matter how hard i tried i just couldn't get it to work in practice certainly with the micro controllers that i had available at the time namely the arduino mega 2560 and the uno r3 following even more research on this i eventually found out that the type of sketch code that i was writing which included something called a keyboard.h library would only work on microcontrollers that had native usb keyboard and mouse support such as the arduino micro the leonardo and the mkr model as the arduino mega 2560 the uno r3 and the nano as well as many others simply do not have this native usb keyboard support due to the type of onboard microchip being used they simply weren't going to work for me so in the end i had no choice but to purchase yet another microcontroller and in this case i went for the leonardo so now that i have the correct microcontroller the correct prototype wiring setup required and the correct arduino sketch code to go with it how does it actually work well let's have a look at the prototype wiring setup first and for this i'll be using the well known and excellent fritzing desktop application [Music] okay so here we are in fritzing and we've got our basic elements already on the screen now start with the leonardo micro controller um and just a word to the wise the usb uh socket on the leonardo is much smaller than the one on the mega and the uh uno r3 um this one's actually called a macro usb cable so if you've not used one of these before you will need to buy one of these cables as well excuse me then we have the breadboard and on that we have three toggle switches which we're going to use for beacon lights landing lights and nav lights respectively first thing we need to do is to power up the breadboard so we'll take a 5 volt supply across here [Music] just tidy these up a bit [Music] and we'll give that a red color [Music] and then we also need to put a ground cable in as well i'm going to take this over to this side of the board to feed this row here because i'm going to take the ground side of the switches over to here and not on that side so we just tidy this up as well like so move that over and voila and we give that a black color okay so we've got our five five volt supply to the board and we've got a ground circuit on this side so we simply take a five volt supply from there to this side of the toggle [Music] switch uh give that red and we'll do the same here square that up and we'll do the same on this one so that's your actual three um five volt supplies to each of the switches and then we need to take an uh ground ground connection to here from each of these switches as well but you need to put in a pull down resistor on on the ground side as well [Music] so we'll try and turn that there we are [Music] and i'll drag that now over to ground i'm going to copy that a couple of times i don't have to fiddle around with another one [Music] yeah that's those done and we take a ground to there and i'll give that a black color [Music] and you'll notice i've left a little pinhole there which i'll come back to in a minute so one more earth or ground side and a final ground side there now what's going to happen is that when you've got your five volt supply coming into the switches this side and when you when you toggle the switch you get a ground circuit made up uh through this pull down resistor back to the board the pull down resistor is there um really to tell the leonardo microcontroller what's actually going on when the switch is off um if you just if you didn't have that pull down resistor there the leonardo basically wouldn't know what was going on with these uh these at these pins um when the switch was off uh it would be left hanging and wouldn't know quite how to deal with it i don't fully understand the science of it but apparently that's the case so when you're using push buttons momentary push buttons or toggle switches like this i believe you need to put a pull-down resistor in you can actually have a pull-up resistor as well which is on the 5-volt supply side i believe anyway take my word for it that's what you need if you didn't use resistors on the breadboard then you could avail yourself of a pull-up resistor which is inbuilt into the leonardo here which requires a slight alteration to the ide sketch code that we're going to write later on but it's probably good practice to put a pull down or pull up pull up resistors on your in your physical circuit good housekeeping to do that okay so now we've done that bit then we have to put the signal cables in from the ground side of each switch so we'll take a cable from a wire from there to there um we will tidy that up as usual it's a bit fiddly doing this but at least you you'll get a better idea from looking at this and you would do the real physical wiring setup because that looks a lot messier there we are i'll give that uh say what green and we do the same for here and we go to pin number three remember these pins because uh these pin numbers because that's what we're going to be using in our sketch code [Music] sorry big pregnant pauses move that over a bit [Music] there we go and then we'll give that what a yellow shall we yes and the last one goes to pin number four and just tidy that up as well [Music] fish right so there we are and we'll give that what let's have a purple shall we okay so that's that i'm going to save that quickly before i lose it so in essence that's our prototype wiring circuit that we are going to work with in terms of writing a sketch code so we'll show i'll show you in a minute what the physical wiring uh version of this looks like and as i said before it's a lot messier than than what this looks like so bear with me on that but um let's see how we get on [Music] so having looked at the theoretical prototype wiring and the switch layout we can now see the actual physical wiring layout which as promised does look a bit of a mess compared to the version in fritzing but anyway leaving that aside for the moment here we have the leonardo microcontroller which is connected to my pc via a micro usb cable which gives the board a 5 volt supply and enables the serial bus communication from this board to my pc the five volt supply comes from the leonardo then to the breadboard over here and the ground comes from here all the way over to this side to here then we have our three switches here and each of those are taking a five bolt supply from this side so there's one there one there and one there and on this side of the switch it goes to ground and it goes through a pull down resistor through to here and back to the board so ground side of the switch through the pull down resistor back to the board ground side of the switch through the pull down resistor back to the board and in between the tail of the switch and the pull-down resistor we have our uh signal cables so each one of these this green one this orange one and this is a green one as well not the same as the fritzing drawing but never mind each one of these goes to pin terminals two three and four so it's from this one to two this one to pin three this one to pin four so that circuit is exactly the same as you saw on the fritzing theoretical layout so now we'll get on to the crux of the matter and start looking at the ide sketch code so here we are then in the arduino ide desktop application and we can now at long last have a look at the specific sketch code that i have developed and written over the last few months that would allow my three physical toggle switches to activate three virtual switches in the cockpit of an aircraft in x-plane 11. please remember though as we go through this that i am not a professional programmer and in fact i have never written a computer code before in my entire 65 years of life what i have done though is to pull numerous bits of code together from wherever i could find it and through a great deal of trial and error i eventually created a sketch that works and does what i wanted it to do i will of course attempt to explain various parts of this code where i can but because i am in no way an expert on this stuff i may not be able to explain things entirely correctly so please bear with me i accept that this code looks complicated and somewhat overwhelming but like anything else in life once you start understanding how things work they become a little less complicated and a little less overwhelming so the first part of our code sketch or sketch code is up here where it states that we have to include in this particular sketch a keyboard.h library whenever you write this on in a sketch code or for any library you use you have to have a capital letter at the beginning of the word otherwise it's not recognized and when it has got a capital the cur the color of this word will change to this nice orange the keyboard.h library ships with arduino ide software package so you don't have to go rooting around to find it but it only works with uh microcontroller boards that have an atmega 32u4 processor okay that's the boring stuff but basically it means a leonardo a micro zero a dua and an and or an mkr model it does not work this library does not work with a mega 2560 an uno r3 or a nano and many others that do not have this processor so then we have a separator line uh and by the way i should point out that if you get a copy of this code um anything with a forward slash and a star here that ends in a star and a forward slash again that entire bit of text there is ignored by the micro controller it doesn't it's just for information in the sketch code by the developer in this case me so it won't do anything with this it's just there for information the same here any forward slash forward slash and then text beyond that is also ignored uh by the microcontroller it's just for information again same here on this dotted line so we've got our keyboard in h library in place then we come down and we have to create some number fields it's just a data type and they're called const int which is constant integer or a number that remains constant in which case we are setting one up here for pin number two of the leonardo which is where our beacon light switch is connected to this wording here is simply the wording that i have created you can call it anything but it will always be referred to as whatever you make it here i've called it a switch because it's a toggle switch bcn short for beacon and pin because we're talking about a pin on the leonardo board and it's pin two the same down here another constant integer for the landing lights uh switch switch again landing short for uh pin number three and then lastly the same thing again switch for the navigation lights pin number four constant integer means that the integer just a number but it will never change it will always be two or three or four or whatever you set it up as moving further down we are creating uh some more um integers or number fields one is called switch beacon state so that is what is the condition of the switch for our beacon lights is the switch on or is it off digitally that will be a one or a zero being red at pin number two and another integer for the same switch is the last switch beacon state in other words what was the last state of the switch was it on or off was it one or zero the difference between these two will tell the leonardo whether the switch has been moved or not same thing again down here for our landing lights same thing again here for our navigation lights then lastly in this section we have a crea a character field um for a special key on the keyboard which is left alt um the left alt key on the keyboard and we are calling that for the purposes of this program alt key okay so that's the first section if this is all getting a bit uh too much then don't worry about it it it is complicated uh initially so just try and follow follow along as best you can [Music] so now we're going down to the next stage which is something called the void setup now this part of the program is only looked at once by the microcontroller all of these up here are referred to in the rest of the program constantly um but these are only looked at once and what we're essentially saying at the beginning here is that we're initializing the usb serial communication with the pc at what they call a board rate of 9 600 bits per second this number can vary but that's good enough for what we want to do now and this part here the next bit is pin mode which basically what what we're doing here is basically setting up pin two uh of the leonardo as an input it could be an output or it could be an input but for what we're looking for because we have a switch connected to it it's an input so the leonardo is expecting to receive some data on input for pin two input for pin three input for pin four so then we go in down to the next section which is called void loop and it's called loop for a reason and that's because once the program has got through this first setup stage it reaches the void loop stage and it just continually goes around through and through these instructions around and round and round until you command it to stop and the first thing is going to do is to read the digital state of the p of switch input pins two three and four and it's going to determine from that whether there is a digital one or a zero whether there is a voltage or not a voltage at those pin numbers if we've thrown the toggle switches on these then there will be a one and that means uh that it's switched on but anyway it's going to come down to let's go to the beacon light section start with it's going to say is the current state of this of pin 2 for our beacon lights the same as the last time when i checked and because we initially set it up as off [Music] when it gets to this it's going to say well it is different because it was off just now and now it's on and it's going to say well if the switch is on or reading high i there is is a voltage then press these keyboard commands alt key which is your left alt key on the keyboard and the number one and in x plane 11 i have assigned beacon light the beacon lights toggle switch to left alt key and one yours may be different in which case you have to put some different instructions here so it will press the alt key first delay by a hundred milliseconds and then press the one if the switch uh is low then it will actually do those same uh case k commands again and why is that well if you think about x-plane and how the switches work the if you consider the the beacon toggle switch it issues the same command alt one if you switch it on and it issues the same command again if you switch it off because it's a toggle switch so we have to replicate that so we do that here again so having done all of that if we left things as they are um by saying well if the switch is high then we do this if this switch is low then we do the same thing again i toggle switch then it would just do that continuously and if you switched if you went into explain now you would see the beacon light switch flicking on and off very quickly so you have to now introduce a part of the program that actually tells it tells the x-plane to stop flicking the switch and that's where this bit comes in and basically it gets to here is having gone through that it gets to here and says well is the switch now in the same state as it was the last time i checked after we switched it on and if it is then it says keyboard release all which basically says uh it's the program saying release these keys now press them no longer and even if the switch is low it says the same thing so either way the alt key and the one are no longer going to be sent to the pc it's turned them off so in other words it'll go through this once it will either set the switch high or not the switch it will issue the keyboard commands just when you switch the switch on or it will issue them when you switch the switch off and then it will say okay we've done that now i'm going to release all key all key commands and that's the end of that then we come down and essentially for the landing lights we do exactly the same thing although these words here have ldg in not bcn for beacon you'll see them here here and so on uh only in this case we're saying we want to issue a keyboard command alt key and two because that's what i have set up the landing light toggle switch to be all key and two same thing here and then right at the bottom we have the navigation lights so we've got nav in all of these instead of ldg or beacon and in this case i've set the toggle switch in x-plane to be left alt key and three so it's all key and three all key and three and that is it simple not really i know now before we leave this uh sketch um i just wanted to point out a couple of things you'll notice right the way through this that there are lots of parentheses or what they call brackets curly braces or curly brackets i'm not sure what they're called semicolons there's equal signs double equal signs exclamation mark and equal signs they all mean something and they all have to be in the positions that they're in now i have come across numerous problems trying to get these codes to work in the past just because there's one curly bracket missing somewhere i keep getting errors in when when i try to compile the code and we'll just try and compile it now so we click on this tick if you watch the progress of this green bar yes thankfully it compiles but how many times have i done that and it comes up with the big red warning sign saying something is missing um and it's usually more more often than not it's something to do with these curly braces so if you're playing around with this code or any code like it i suspect you're gonna have more problems with having the curly curly braces in the right place and that than anything else it does give you a message down here which tries to indicate where the problem is but it's not always easy to try and work it out but i suppose practice makes perfect and the more you go through these things the more you'll understand it okay so having done all that we know it compiles we know it works so i guess the next thing to do is to um open the x-plane 11 application and see if our switches actually work [Music] okay so here here we are in a little cessna 172 inside x-plane 11 and what we're hoping to see courtesy of the arduino sketch code that i've developed and written is that this beacon light switch will switch on and off same for the landing lights and the same for the nav lights so as you will see from the inset on the screen you can see the three toggle switches concerned the one on the right is the beacon lights the one in the middle is the landing lights and the one on the right uh sorry left is uh for the nav lights so let's actually see if these actually if these work so i'm going to switch on the beacon light first yes it works look at that and off and on and off no anomalies it just does what it it's supposed to do every time i switch the physical switch on the one in the virtual world of explain switches on and vice versa the next one we're going on to the landing light so that switch should now function oh yes look at that on sorry off on off and then the last one is the nav lights yes look at that it all works fantastic you will not know how relieved i am that that's actually working after all of the months of trial and error trying to write to a sketch code for this so i'm very pleased with all that and i hope that you've got something out of it as well and i can assure you that i am definitely not using any third-party um interface with this this is all being done by arduino sketch code only um let's do it one more time me never lights [Applause] beacon no landing lights sorry and beacon lights yes they're all working excellent so let's move on okay then so now that i've managed to develop a sketch code that works as an interface between three toggle switches and x-plane 11 i now need to start thinking about how on earth i'm going to expand the code to accommodate all of my other input and output devices clearly there is a limit to the amount of input pins there are on the leonardo microcontroller and very soon i'm going to simply run out of pin availability there may also be an issue with the amount of memory on board this is something that i recognize pretty early on in the development of my project so i've already been looking at using the leonardo as a master board and the mega 2560 or the uno r3 i haven't decided yet as a slave board to increase significantly the number of inputs and output pins available [Music] in this respect i have successfully written another sketch code whereby switch input signals are being recorded by the slave board and then being communicated to the master board and then on to x-plane 11. development work is still ongoing as far as this is concerned but my initial results are at least very encouraging and i will post further videos on this in due course i'm also looking at writing sketch codes to incorporate multiplexers and lcd displays but all of this is at an early stage of development as far as things stand today [Music] so in summing up then i'm personally very pleased that i was able to get to this stage without any computer programming experience whatsoever and very much look forward to developing things further over the coming months i'm almost certain that there will be arduino experts out there cringing at my work and saying that i should have done this or done that but hey we all have to start somewhere right hopefully you have been able to follow along and have been able to take something positive that will help you with your own projects but if you have any questions please post me a message and i'll try and answer if i can in the meantime i would just like to thank you for watching and say that if you found this video of any assistance please smash the like button also please don't forget to hit the subscribe button and the bell so that you don't miss any future videos when posted tata for now
Info
Channel: Fun of Flying
Views: 2,692
Rating: undefined out of 5
Keywords: Arduino, Fritzing, XPlane11
Id: A5s_9UNFDck
Channel Id: undefined
Length: 37min 54sec (2274 seconds)
Published: Thu May 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.