Understanding an Arduino Library :: Video #2 :: Arduino Library Series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello I hope you're doing well thank you for joining me in this second tutorial about Arduino libraries now in the first tutorial we talked about downloading and setting up an Arduino library in your Arduino installation so that you could use you know so that you could use it and now in this tutorial we're going to talk about some of the syntax that you may find in one of the Arduino libraries so some of it when you open up on arduino library can be rather unusual and you might sit there staring at it and trying to figure out like what what is this even talking about so we're gonna look at a couple different libraries and we're going to talk about a couple different things but the focus for this tutorial is so that you can get familiar with some of the terminology that is that kind of makes up a library and the syntax that makes up a library okay so let's go ahead and get started so let's look at this piece of code right here this sketch alright you can see the comments that says blinking LED and this is a piece of code that I wrote and it took me I don't know a minute a minute and a half to write so I've got some data up at the top some variables that I declared I've got LED pin and set equal to five and I've got a delay time specified so I've got a variable that holds the amount of milliseconds I want to delay I come in I've got a set up I kind of set up what I want the pin to be I forgot it as an output and then I come down to void loop and I've do a digital right eye delay I do another digital right and then I do another delay so this sketch blinks an LED so notice that this sketch has two components all right it's got data you know numbers and then it's got functions so it's got functionality so data and so what I want to talk about is what a library actually is and Arduino library is actually a class okay in a class is a special kind of data structure in the C programming language that has data and it also has functionality so think of an array hopefully you're familiar with arrays if you're not you can check out the previous tutorials on arrays but an array is a data structure that can hold multiple pieces of data and a class is kind of like an array because it can hold multiple pieces of data but it also has functionality so a class is extremely awesome and again libraries are really just classes okay so what we're gonna do is we're gonna talk about a class now why would you want a class well a class simplifies things for you okay so in fact what we're gonna do is we're going to talk about what would happen if we were to take all of this and make it a class okay and we're not going to actually make it a class but we're just gonna kind of talk through some of the terminology we're gonna kind of make a mock class out of this alright so let's go ahead I want you to pretend that this line that I'm drawing right here all this information up here is a brand new sketch so pretend this doesn't even exist we're going to go back to it for reference but for our purposes we're just going to look right up here so if I wanted to make a class I would simply write the word class note that it's a keyword and then I'd give the class a name and I'm gonna call this class LED blinker all right and what this class is gonna do is it's gonna blink an LED and all I want to be able to all I want to have to do is I'm going to tell this thing a pin number and I'm gonna tell this thing a delay time and it's gonna do the rest okay so it's kind of taking care of the work for me so every class has a name so the name of this class is LED blinker and another thing that everything every class has is called a constructor okay and what a constructor does is it gathers all of the necessary information in order to make an instance of that class okay so we've got three terminal three terms here that we're talking about we've got the word class again the word class kind of defines this data structure functionality that we've been talking about we've got constructor and constructor is the name of the item that kind of collects the information that you need for your class and then we've got instance in an instance is a particular part or it's a particular instance of a class okay so let's just go ahead and write a constructor here so the constructor is the same has the same name as your class so at first that's a little confusing but it actually makes makes things very simple and easy so here's my constructor LED blinker and what it's gonna do is it's gonna kind of look like a function call but it's going to define the information that I want so what what information do I need I need a pin number and I need to delay time so a pin number is an integer so I'm just gonna call this pin and a delay time can also be in the form of an integer and I'll just call this delay time like so all right so there's my constructor so LED blinker again it's the same name as the class and I'm defining the two arguments that I need to pull in in order to make an instance of that class now there's also going to be functions associated with a class now down here we really have we really have a couple functions we've got digital write and digital write high-end digital write low so what we would do and we're not actually gonna do it here but what we would do is we would wrap all of this up into our a simple function and we would call it blink me so right here we're just kind of defining a function and again we're not explicitly writing the function here because we're just kind of mocking this up we don't want to get too into the details at this point all right and then maybe we'd have another function we'd call it fade me and that would use pulse-width modulation to fade the LED okay so here is my class again so the class is called LED blinker it has a constructor the Constructors the same name as the class and the constructor is basically going to define some arguments that I have to pass in order to create an instance of LED blinker and then I've got some functions inside here that I can use with my class okay so let's go ahead I'm gonna make another line here so here's a line okay divide this you system in your brain and pretend this is a brand new program and now let's say I want to actually use this class well how do I use a class well it's pretty easy first thing I do is I write the name of the class so LED blinker alright and then I have to name the instance of the class so I'm gonna call this my LED so this is basically the name of a think of this as the name of a variable you know when you write make an integer so here's an integer integer is a datatype and then you have to name your variable well LED blinker you can kind of think of is that okay you can kind of think of LED blinker as a datatype it's just a data type that we defined and it's also a data type that happens to have functionality to it okay so we've written the name of the class LED blinker we give a space now we name the instance of the class and then what we do is we set it equal to the constructor okay so LED blinker and now we need to pass it oops LED blinker there it is now we need to pass it the information that this program is going to need to make an instance of my LED well what was information well it needs an integer that's going to specify the pin number and then it uses an integer that's going to specify the delay time so I'm gonna pass it the integer four so that's pin four and I'm gonna pass it a delay time I'll say 1,000 so 1,000 milliseconds okay so right here I have created an instance of the LED class alright so now let's say I'm down at void loop alright so whoops void loop and I want to blink my LED that I stuck at pin 4 on my Arduino board how would I do that well here this is what it looks like you have to name the instance that you're talking about so my LED you put a period and then you follow it by the function that you want to call well what function did we want to call we wanted to call the blink me function so I'm just going to tape blink me and and whammo there it is so my LED my LED right there I'm calling that instance of the LED blinker class I put a period and then I've got the function named blink me so what's going to happen here well the LED that's attached to pin 4 on my Arduino board is going to blink and it's going to use a thousand as the delay time okay so again we didn't explicitly write this blink me function anywhere but you just have to imagine with me that it has been written okay now in the same thing we could say my LED dot fade me all right and then it would it would perform this fade me function also okay well this is all handy-dandy but what's awesome about classes is we can have another instance of the LED blinker class well how do we do that well all we do is change the name all right so let's change it to my let's call it my other LED my other LED and I want to change the parameters here cuz I'm gonna put the I'm gonna put the LED at 2 and I want to at pin 2 and I only want it to blink for 500 milliseconds all right so same exact line of code I've just changed the name and I've changed the arguments that I passed I don't have to change the arguments that I passed well actually the pin number I have to change in this case at least because I can't have two LEDs sharing the same pin at blinking at different rates I suppose I could but it'd get kind of weird but anyway so now what what do I do down here well all I gotta do I can do the same thing again I'll just paste that there but now when I want to refer to this instant I just use that name right here my other LED dot blink me does this make sense all right so what we're gonna do now is we're gonna go look at a couple examples in the arduino sketch except a couple of example arduino sketches to kind of sink this in alright but before we do this I want to just draw your attention to something notice this dot blink me does this look familiar to you this syntax I think it should let's look at a really common one have you seen this before serial dot print well wait a sec what do you suppose this says well serial is a class that Arduino uses and print is a function of that class all right now serial is built in the serial serial class is built into the arduino ide so you don't have to go through all the trouble of creating an instance of the class but you're still you're still using that class alright any time you use the print or any time you use that serial library okay so let's just do a quick recap Before we jump into some of the examples a class is like a really cool data structure okay a class has data and it has functionality a class has to have a name and a class also has to have a constructor and a construe the constructor is always the same name as the class the constructor has all of the information that you need in order to make a specific instance of that class so think of the constructor as the cookie cutter okay the constructor is the cookie cutter in the instance is like the actual cookie that you eat okay classes have functions and those functions can be applied to instances of the class and to do that you just use the name of the instance you want followed by a period and then the function and then just a note you know these functions could take you know you could also be passing arguments to these functions I mean look at serial print you pass an argument to serial print you know like print blah or whatever okay so let's go ahead and look at a couple examples of some other libraries and let's see if we can identify a couple things but before we do that one more thing I want to talk about now like everything in programming there's a hundred one ways to do the same thing so what if I told you there was an easier way to create an instance of a class I bet you jump right on it well there is alright so the easy short code to do this is you still have to name the class so here's LED blinker that's the class and you still need to name it so we'll just call it my my we'll just use the same thing here we'll call it my LED but now instead of sitting at equal to the constructor all we're gonna do is open a parenthesis and we're gonna directly send the information that those arguments and then we're gonna close the parenthesis so do you see this all we've done is skipped this right here in these two lines of code are equivalent all right so all you're doing is skipping this equals LED blinker alright so this is a little more explicit but this man doesn't this look so much cleaner when you skip having to call that constructor now you might be asking well why would you do it this way well I don't know sure plenty of people like to do it this way plenty of people like to do it this way it doesn't matter the exact same thing okay so you've seen the short code here all right so let's go ahead and now look look an example so we're gonna go to file examples we're gonna come down here notice you know these are the examples we've gone through before if you've gone through the tutorials but down here all of those libraries that we have loaded they also come with examples which is really awesome so let's take a look at the stepper library this comes pre-installed on the arduino ide installation let's look at motor knob to expand this and let's see all i want to do i don't want to talk like in depth about this example i just want to see if we can identify the class name a constructor a couple functions okay so alright so notice when we use a library we've got to use the include statement and we've got to use the name of that library we've got to refer back to the header file so that's what that line of code means that refers to the dot H file in your library file and if you recall from the first tutorial we kind of talked about that header file so that's what that does alright this defined function this defines something that follows in the sketch alright and we'll kind of see this shortly I'm not going to go to in depth about this but because I'm not too worried about it so let's come down to this next line of code in this library stepper stepper what is what is this well hey look we're creating an instance of the stepper class okay so what's the name of the class hey it's stepper what are we gonna call this instance well we're gonna be real creative and we're just gonna call it stepper ok notice the lowercase s that differentiates it from stepper but we could have called this my second kidney it doesn't matter what you call this but we just happen to call it stepper and then what's all this information in here well we're passing arguments to the stepper so that we can create an instance of it ok so what have we done here we have created an instance of the stepper class all right that's easy enough right so let's look down in setup anything interesting and here hey look at that stepper dot set speed so what's this well stepper is the name of our instance dot set speed well set speed must be a function of the stepper library does that make sense alright let's welcome let's come down to void loop anything interesting here hey look at this step or dots step and we're passing it some some values here well stepper again that's referring back to this instance of this of this stepper class and we're using this step function from this stepper class alright does that make sense I think it should again we're not trying to become pros we're just trying to kind of see some of this understand some of the syntax here let's look at another one let's go to file examples and let's look at the servo library let's look at sweep alright so again here's the include statement again and all this is doing it's pointing the code to the header file in your library folder and then we can look here's servo my servo so servo is the name of the class and then my servo is the name of the instance and notice here we're not passing any information to my servo are we and that's because we don't need to this is uh this is a class that only has the need for functions there's no data that needs to be associated with it and that's fine all right now I want you to notice - it says object okay so you may have heard of object-oriented programming and when we start talking about classes and instances we really start kind of talking about objects and really it's just a way it's kind of an abstraction and it allows you to think about how you are programming so if you see the word object you can kind of think of it as an instance alright so here's a an instance of the servo class alright let's come down here hey look at this and void setup we've got a function the attach function of the servo class all right my servo attached come down here again and here's another function a right of the servo class and another right all right so I think you're getting the kind of gist here let's look at one that's a little more complicated and this is one that I just recently downloaded it's the capacitive sensor library I'll go ahead and open up this is a contributed library extremely cool I will be doing a tutorial in the future on this library because it was so easy to do and so much fun but let's take a look at this alright so here's the include file right it's gonna point to the header file in our library folder and then look at your capacitive sensor that's the name of the class here's the name of the instance CS under slash 4 under / - so that's kind of a weird name and then it's set equal to the constructor so here they have explicitly defined it and this is the first way we did it the name of the class the name of the instance setting it equal to the constructor and then we're passing it to bits of information you know two pieces of information this happens to be the this is the input and then this is the sensor all right and then look we've got three separate instances of this class does that make sense so three different names here they just change by that last digit but three different names and we're passing it different pieces of information so then we come here to setup and notice that we've got a function that's applying only to the first instance all right so here's C s under slash four under slash two dot and then this funky function name alright and we're passing it some data and we come down here and look at here's a couple functions now this is a little confusing and it was for me and I'm not sure if this is just a convention I'm not familiar with but notice that the name of this function is the same name as the class except that the C is lowercase so that can be a little confusing but don't don't be deceived this is actually a function of the capacitive sensor class all right so here we're in the loop it looks like they are setting some variables initial and declaring and initializing some variables and they initialize it to the function capacitive sensor okay so there's three examples I think this should help you get familiar with what you're gonna see in some of these libraries that you start to use I hope this was helpful I plan on doing some more tutorials on making our own classes so basically how to build your own library those are gonna be a little more in-depth it's gonna be a little higher level but you know you just kind of slowly move up so hey thank you so much for joining me so glad you listened in I hope this was helpful see you next time you [Music]
Info
Channel: Programming Electronics Academy
Views: 60,230
Rating: 4.8551402 out of 5
Keywords: Open-source Hardware, Arduino (Computing Platform), C++ Class, Arduino library, C Syntax
Id: 171RSh0az5A
Channel Id: undefined
Length: 23min 32sec (1412 seconds)
Published: Fri Jan 24 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.