Good Arduino Coding Practices & Variable Scope

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's Brian with uino Academy and creator of circuit crush.com and I'm excited to be here with you on your journey to becoming the maker you were born to be now that we've started getting into some coding we have to have a little talk before we go any further we need to talk about something called variable scope or just scope for short no I'm not talking about the stuff you rinse your mouth out with or you used to look at germs with SC open a world of programming refers to where in a program a variable can be used it refers to the parts of a program or code block that can use a certain variable now this may sound a little weird and confusing but don't worry we're going to clear that up and it's actually pretty easy to understand we also need to say a few words on using variables instead of using what are called literals or hardcoded numbers which is good programming practice and to see what I mean for both topics I've pulled up the blank example that comes packaged with the arduino's IDE I'm using this simple example because it's very basic and since we don't know a ton about programming yet this basic sketch should be easy to understand for us at this point and though it's short it'll serve as an example to demonstrate the concept of variable scope and good programming practice when it comes to using literals or hardcoded numbers in your sketches so let's take a look first as is good practice we see the sketch starts with a group of comments that described the program and give us some vital info on it now I'm going to be honest and tell you that in the interest of not being boring you may not see me do this in every sketch from every lesson in the future but that doesn't mean you shouldn't do it when you're doing your own sketches and projects for example we can see from the comments that this sketch uses the arduino's builtin LED and tells us the LED's pin number is 13 and we'll need to know this information to get this to work it's also good to note that this example requires no external components because it uses the LED and series resistor that are built into the board but for our purpose we're going to make a few simple modifications to the program in a minute the next thing to note is a setup function we see we're using the pin mode function here but we also see LED buil in which is kind of weird this along with other words like high low input output and more are called constants constants are predefined expressions in the Arduino language which are used to make programs easier to read they appear in blue and are often though not always typed in all capitals and you know this is all fine legit for this sketch I mean using LED buil in totally works and there's nothing technically wrong with it but for our purposes we're going to make a small change after the comment block at the top but before the setup function we're going to declare a bite called LED pin and assign the value 13 to it now there's a few things to note about what I just said and what I'm doing first I'm using the bite data type because we're declaring a PIN number and since 13 is a lot less than 255 and the pin number isn't going to change during program execution we can use a bite instead of an integer and save some space next notice that I said I'm assigning the value 13 to the variable LED pin I didn't say I was setting LED pin equal to 13 and there's an important distinction that we talked about in our lesson on arithmetic and logic so if you skip that lesson or need a review please go back and watch it again finally notice that I gave the variable descriptive name that's says something about what it's for it would be clear even to a complete beginner that this variable has something to do with a pin the LED will be connected to so now that we have our variable declared let's replace the LED built-in constant with the variable name so here we could see the name ell deep in refers to the variable we declared which refers to the number 13 so so pin mode and digital right we'll see the number 13 and doing this brings us to our next important point I could have just put the number 13 there and some of you may be wondering why I went through the trouble of declaring a variable for a PIN number instead of just hardcoding number 13 and this is because hardcoding numbers or using literals is usually a bad idea why is this a bad idea you ask let's say you decide to change the pin number in the future because you don't want to use a built-in LED for some some reason and you've wired up your own LED circuit you would have to sift through the code and find every line that refers to the pin number and change it now you may be thinking something like well so what we only have a few lines of code here that's going to be easy and you know what for short programs like this you're right but now imagine you have a sketch with like I don't know 530 lines of code or something it's easy to see how doing this could be unwieldy and error prone rather it would be much easier to change the pin number in one place place near the beginning of the program so that's a reason why it's usually a bad idea to hard code numbers and we want to develop good programming habits early in the game but wait a minute we still have a literal or hardcoded number a th in the delay function well let's apply the same thinking and create a variable for that in case we ever want to change a delay time which controls the speed the LED will blink at we'll just call it delay time MS and assign the value 1,000 to it only this time we'll need an integer because bytes can only go up to 255 and notice that we added the letters Ms on the end again this gives it a descriped name because the delay function takes milliseconds now let's talk about scope the two variables we just declared are called Global variables
Info
Channel: Circuit Crush
Views: 820
Rating: undefined out of 5
Keywords:
Id: HfHOqN1ZgqQ
Channel Id: undefined
Length: 6min 38sec (398 seconds)
Published: Fri Mar 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.