#BB9 Organising your codešŸ§¹for easier debugging and maintenanceāœ…

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome to my series of short videos in which we discuss how the arduino interacts with various electronic components yes in less than 15 minutes we'll go over the basics how they can be used hints tips tricks and traps yes welcome to the second part really of the videos that deal with how to make our code better within not just the arduino environment of course any c plus environment at a sort of a hobbyist level i guess we're not talking about commercial work here if you're working as a paid developer you know this and much more besides so we're looking at hobbyists who have a grasp of you know code and they go yeah i get all this but maybe the because they've never been taught it they don't understand the structure of the code is probably equally important as to actually what the code does so what i've done is knocked up a quick project here and believe me it only took 10 minutes to do this because it's very very simple i've connected one of these two line displays it's a little cd display 16 columns wide two lines deep via i squared c there's an i squared c backpack as they call it on on the back of this so we only need four wires so two for i squared c and two for power yeah that's four wires here this bit of coiled wire ends in this black thing here that is about a ds-18b20 temperature sensor and we're displaying that temperature over here i know this screen is somewhat difficult to read because well it never does record very well i don't know why i want to give a big shout out to pcb way pcb prototype the easy way now we're all familiar with their special pcbs five dollars for 10 pieces but you can also have flex pcbs advanced pcbs and of course you can order custom parts let's have a look at the cnc and 3d printing options they have so it's 3d printing first you upload your cad files as you would do normally for a pcb but then you select your materials and submit a quote request cnc machining is pretty similar in the way you submit your files upload your cad files select one of these many many different types of material you can use and of course there are also 27 different options for the surface finishing just look at a few of those there from anodized brush bead blast spray painting and there's more under that list as well and finally there's sheet metal laser cutting and bending so let's have a look at that once again you upload your cad files select the products you want to make it from and specify a few of the parameters here about whether you want threads for example and you can always submit your request for a quote and it's about seven to nine business days to get it done okay that's pcb way excellent service good quality why don't you check them out now to get this going i just followed the same path that i normally would do is to get the core thing working right you just throw the code together you sort of put a few comments in maybe while you're doing it but um basically it's a bit of a mess you get the thing working you go woohoo done it yeah but you don't walk away at that point you go this this looks horrendous it's all unstructured and cluttered in bits and pieces all everywhere let's talk about how we can really clean up that code and make it well pleasurable to look at but more to the point pleasurable to maintain in three six months nine months a year you know you might want to add another sensor or something you don't want to get a bit of code there's spaghetti you want to get a code that's well structured and laid out you go this is a pleasure to work with let's start okay so this is the what i'd call the prototype code we just throw everything in and get it working basically without worrying about too much about the structure or anything as you can see there's lots of includes here at the top um so that takes up a number of lines and say okay this bit here sorts out the sensor right so it's a reduced library this one here ds18b20 these three lines sort out the ds18b20 initialization and so forth great here's my button that i've got connected up the liquid crystal display i've used that particular library for years and years and years i'm not going to upgrade it because it works just fine the way it is the problem with the arduino system is that it will tell you all those new libraries to be upgraded for you and boards and whatever and you go okay upgrade my libraries then you find the programs you wrote two years ago no longer work because they're not backward compatible so um i won't be doing that any time soon right then we have a setup yeah which is pretty normal we set up the serial monitor set up the pin for the push button then we set up the lcd as you can see i've already started to sort of you know group logical things together and then we have a bit of sensor initialization there then we go okay we've done the what that's that's enough and then the loop which is just this little bit here is nothing is it really we go when you've pushed the button when you've pushed it i connected the button low we request a new temperature reading from this converter ds18b20 right that takes a little while to do possibly uh well off the top of my hands head maybe it's um 100 milliseconds maybe so we just do nothing in that time there look this is a demo better code all right then we go and get the temperature in centigrade and we output it to the lcd screen great i mean that is it and then it goes around the loop again and goes have you pressed the button if not just keep whizzing around until you do press the button brilliant i mean it's a very very small bit of code to do well a fairly useful thing isn't it to read the sensor yeah but it's it's what the way we've now got it is what i've considered to be like a prototype bearcat yeah just to prove that we can link the sensor readings and the lcd together even though they're two entirely separate things yeah because where we have lcd in this project we could just as easily have a cloud yeah we're going to push the sensors up to somewhere like thingspeak for example so you can see what the temperatures are over a period of time okay so we've got all this and you might think well that's that's all right now i'll leave it like that but the trouble is this is a very very very small project you start getting a bit bigger and you're gonna have reams of stuff well a up the front here to sort of you know declare all these objects and add all these libraries in the setup gets more cluttered as you have to initialize various bits and perhaps get responses back we've got no wi-fi on here of course that would be another big thing to do wouldn't it so what do we do well the very first thing we do is to say right let's logically look at our projects and go how many bits of this project have we actually got that are entirely separate okay well you can see from the workbench there the first thing we have is an lcd screen right now that is entirely separate from anything else you might say well surely that's connected to the sensor well no it's not it can contain any information on this screen can't it so it's an entirely separate bit of the project and you can get this bit running all by itself uh ditto the 18 b20 ds18 b20 there's another bit you can get running all by itself so that's another bit so anyway we're going to split it up into at least two bits right and uh the button as i say as well and then of course you've got the overall overarching preparation for the entire project yeah for example all the initialization routines where do they belong right let's have a look at the code the final code that i'd expect to see and see how i did it now we're just back in the arduino ide for a bit because there's something i need to tell you we're going to put some of this code into a separate file but how do you create these new files and where do they go well on the arduino ide if you look at the top right hand corner they see this down arrow there click that and you'll see it says new tab what new tab actually means yes you do get a new tab here in the ide in a minute but it also means new file so if we clicked click that it goes okay what's the name for the new file when we've decided what we're going to call these files just click on that arrow type in a name here so for example you might have lcd dot h you must have the dot h afterwards we'll never find them okay and then you just say okay now at the top of this ide you now see that it's got this lcd.h as well as the original one that's the original one if we click the other one it's empty at the moment where's it put that file it's put the file in exactly the same place as your sketch so you have a folder sketch it's put another file in there now called lcd.h okay so it finds it dead easy it knows where it is and it will open it up automatically the next time you open up the main code so that's how you do it in the arduino ide and always have done right now we spoke about creating new tabs files and effects yet so what i've done here is created three files as you can see at the top here we have a file called lcd helper.h sensorhelper.h and then something called globals oh no not globals don't worry about it now what that means is by moving some of the code into separate files look at what's happened now to the main program it's turned from that sort of mixing bowl of various ingredients all muddled together to something now that is much easier to see what's going on and to debug so we've got these three includes which in fact you could also you could manage those as well to a single include if you wanted to but i put left them in there for now the setup now looks like something that we can really use and understand okay we have the serial begin as standards yes but now we say initialize lcd and we just call this function here we'll come into how we've done that and then we say initialize the sensor we just do that there so none of this individual api function calling you know all these funny esoteric magic number things let me get rid of all that and say look just just go and do the lcd just go and do the sensor and then we go we're done it makes it very easy to understand and we know it works because we've done the prototyping beforehand and should something go wrong we can have a look at any one of these instances here of what these functions are and go what do i need to change or what didn't i do that i should have done or can do in the future okay so that's the setup nice and neat let's collapse that now look at the the loop what we're saying here is if the sensor button is low go and get the temperature and print the temperature that's it and that's logically what we're doing isn't it people will understand exactly what this code is doing by reading that okay let's have a look then at the first file remember i said in the arduino ide we could have that lcd.h well i tend to call my files lcd helper or whatever it is sensor helper yeah for example in my esp32 web radio which contains many many files and they're huge and long and uh you can see there we are there's a list um of all the files that i got in there and i know that if if something were to go wrong in that esp32 where brady i'll go what is it that's not working is it the is it the buffering is it the screen is it the the touch thing whatever it is i can go straight to that file and have a look yeah so i'm not delving about amongst the myths of wi-fi settings when in fact i'm trying to be looking at the lcd settings okay let's have a look at the lcd helper then see what i've got in there right the very first line says hash pragma once now this is um strictly speaking it's not totally portable you might find a compiler that doesn't understand that there's a different way to do it but i'm not going into it now it just means compiler if you've read this file once don't do it again i don't want you to duplicate what's in here you read this once and then mark the fact you've read it once and that is it yes and you need that at the top of all your included files up here so then we say okay this is all to do with the lcd then so what do we need in here well we certainly need the liquid crystal library that i mentioned a while back uh what's this global's h that's over here isn't it well when you think about it in any program there are some globals that you do actually need yes because you're talking in various functions to the same thing so if you're going to have some globals include them first at the top however once again that globals file will have a pragma once at the top we'll have a look in the sec there's hardly anything in it just to prevent it being reduplicated well we'll skip the next bit and say look here's the liquid crystal that's exactly the same line of code we haven't changed any of this code we've just moved it about here's the setup lcd with all the bits we had last time great that's nice and easy we can understand that and here's the print print temperature in centigrade it accepts one single parameter called as a float called current temperature yes yes yes i don't like floats but come on this is a demo right set the cursor print out there's some boilerplate text print the actual thing printer use printf as well because we know printf is lovely to debug stuff with and that's it yeah nice and easy and that is now separate from our main you don't see all those those details in here just what's going on now the sensor helper is very simple and similar it's got the pragma points at the top so you don't duplicate any of this code various includes a few more includes this time this is getting the sensor working and i'm not going to go through all this really you can see what this is doing and we know it works because we did the prototype with all the code all mashed in together and it worked fine yeah right let's just have a look at the globals but there's not a lot in it really is there one it's a compiler only include this once if you see it twice forget it the second time i need to include the arduino.h and i think under the new arduino 2.0 ide i think we need to do that as well i don't think it does it in the background not as they're going to include that somehow for you behind the scenes and i don't think they should put all this stuff behind the scenes really but anyway and there's our lib printf that's it that's all we got in the globals almost not worth having but i had to do it just to show you okay so that's our three helper header files which we've included here now the purists amongst us will of course say you can't include code and header files we should just put the declarations the functions and then put all the code into cpp files but that just feels like a lot of work yes at work we have to do it for hobbyist use i think this is plenty good enough that's my stance anyway yeah globals sensor helper lcd helper and then just this very simple clean easily maintainable code but more than that it's you can see the woods for the trees now now there is one little thing on this entire project that we haven't really mentioned yet that's the button isn't it now i've sort of put the button in where it was originally that is as part of the sensor code in fact if we look at the sensor helper we see that uh i've put it in here um just to ignore these little comments we haven't got to that b yet but the the pin itself the button pin three i've put into sensor but i'm thinking that's that could really be a global connect so maybe that's something we should have put into the globals in here and uh referenced it that way but i don't think it's it's critical but it's certainly something to think about yeah because when you come back to this in six months time you go i've got to change that button or do something with it you're going to be looking somewhere are you going to be looking in the sensor file um probably not now we were gonna talk about what those double colons meant in the uh the main for example where we have things like um well talking about the button digital read and we have sensor colon colon pin button yeah time's too short we'll talk about those next time and certainly addresses to some extent anyway the my my loathing of the use of globals that um yeah at least this way it's it's more manageable and under control now just before we wrap up for today does the code work any better like this does your microcontroller give two figs in the hood whether or not you've made it all nice and neat with separate files like this doesn't it care one job i mean the answer is of course it doesn't care the code compiles to exactly the same code as what we had before yeah so why are we doing it and the answer is because we are trying to keep things organized rather than jumbled as you can imagine i'll get quite a few requests for help and some of the code i see well it's like that prototype code that we started off with in this little project but a hundred times worse where people have added bits in and jumbled and got conditions and it's like no wonder you can't see what's going on so this helps you split things out and get your code working or the bits that aren't working at least you can identify where that code is that's all i'm saying okay we're going to call a halt there if you've got any queries comments whatever please do put them down into the comments section of the video and um don't forget to give this a like if you found it even moderately entertaining or informative or whatever yeah i do hope that you really do use this sort of mechanism though for splitting out your code because it will make your code easier to maintain and understand so great stuff i hope i hope i'll see you again in the next video and we'll talk about namespaces i hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching you
Info
Channel: Ralph S Bacon
Views: 15,679
Rating: undefined out of 5
Keywords: Arduino, Beginners, electronics, C++, microcontrollers, programming, gadgets, ardiuno
Id: PWMOb85OGY0
Channel Id: undefined
Length: 18min 45sec (1125 seconds)
Published: Fri May 27 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.