Raspberry Pi Pico W LESSON 64: Object Oriented Programming Example in MicroPython with LEDs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy do.com and we're here today with episode number 64 in our incredible nude tutorial Series where you're unleashing the power of your Raspberry Pi Pico W what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sugar no sweeteners none need and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will be using the Kepler kit for Raspberry Pi picco W hopefully most of you guys already have your gear if you don't take a look down in the description there is a link over to Amazon you can hop on over there and pick your kid up and believe me your life and my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today now in the last few lessons we've been talking about modular programming where we start organizing our programs more logically into logical chunks or logical blocks the first way we did that was by writing functions and then we took it to the next level last week I taught you how to do objectoriented programs using classes and methods now I demonstrated that with this hypothetical concept of a rectangle so we had a rectangle class and then we could create individual rectangle objects and we could interact with those rectangular objects through our rectangle class okay now this is something that it takes a little while to get completely comfortable with hopefully you understood it last week but I thought it would would be useful if we had a second lesson and a second example and this time instead of having the arbitrary concept of a rectangle we would create classes around physical objects and maybe that would help you understand it even better I am convinced by the the end of this lesson you should really understand this concept of objectoriented programming using classes and methods okay so what I will do is I'll sort of show you the circuit that we're going to be doing I will get out your way I will have a sip of coffee and you can see we have a very simple circuit where we have a green LED and a red LED the red LED is connected to GPO pin 15 and the green LED is connected to gp14 and then the the short short leg of the LED we have connected through the 330 Ohm resistor to our ground Rail and same red LED through three 330 ohms to the ground rail so it's a pretty simple circuit that we are going to hook up here now what is the general class of objects that we're going to be dealing with it's LED now that is the class the class is going to be led what is the object that I'm going to have I'm going to create a red LED and I'm going to create a green led the red LED is an object under the Class LED and the other LED is also under the class LED so you see I have I have this this class that is called LED and then I'm going to create LED objects under it hopefully this will make a lot more sense as we go through it but let's go on over here let's fire up Bonnie and let's get started let's get started by defining our class okay what is the class of objects that I want to create well I'm dealing with LEDs so I will call it LEDs what do we need to do inside of our class we have a special method the AIT method that is run when you create an object under this class and so I need to Define that method it is uncore underscore double underscore that is init and then double underscore again what do we always need to pass itself that'll be the name of the individual objects that we create and then LED pin because each LED object that we create under this class will have its own individual unique pin number now what do we need to do okay we need to set up the LED in the init we need to set up the LED in the init well we're going to have to we're going to have to Define it as an input or an output we're going to have to do all of that sort of stuff so we're going to need to from machine import pin now maybe you could do this outside the class up at the top of the program but then if you use this class it wouldn't be Standalone working so I want it Standalone working so I'm going to import the libraries that I need inside of the class and I'm going to do that here inside of the NIT so when I create an LED object it'll come up it'll run a knit and then it's going to import the pin method from machine now I'm going to say self. pin number is equal to l is equal to LED pin so when I create an object a particular LED objects I'm going to pass it what pin number that led is connected to and then I'm going to set that to something the whole class will understand I'll put this LED pin in inside of the variable self. PIN number so what would this be well if red pin is connected to pin 15 then red pin. pin number would be LED pin so hopefully that makes sense now what else do I need to do now I need to actually say whether that pin is an input or an output so I'm going to say self and now I've got to give this some sort of name because I can't just keep calling it red LED I've got to give it under that some sort of name so I'm going to call it self. obj you could call it whatever you want okay but I'm going to call it obj and that's equal to we're going to do our pin mode right so it's going to be pin and I imported pin from machine so it'll know what that is and then what do I want I want self. pin number and then this is a what it is a pin. out like that okay I have set up that led now I have set up that led object now what do I want to do I want to put a useful method in here where I can do something with the LED what would I want to do with the LED maybe I would want to Blink it so I'm going to say Define a method blink and then in order to Blink it what am I going to need I'm going to need a self okay so you always keep track of who you are okay self might be red LEDs self might be green LED but you're going to do self and then how many times you want to Blink numb blink and then what do you need you need a delay delay time like that all right now inside of this method I am going to need time okay I'm going to need to put delays in so I better import time okay now I'm going to do my blinking inside of This Blink method so for I in range okay we're going to start at Z and we're going to go to numb blink like that now why didn't I make like a like a self. this or self. that on non blink because the number of blinks is not the number of blinks is not a parameter of the LED it's something I pass it every time I call that led but it's not a part of the LED object it is a parameter when I am working with that object so that's why don't need to do all of that self business there so for ION range from zero to num blink what do I want to do well I want to Blink it okay now I know it's going to be a DOT value of one well what dot value of one well it's going to be red LED green LED whatever the name of the object which is self Dot and I set it up calling it what obj so self. obj is the way I'm going to enter enter it self. obj has a value of one then I time do sleep of delay time and then a self. oj. value value of zero so I'm going to turn it off time. sleep of delay a time like that okay so when I create my LED object I would just say LED I would say like red LED is equal to led the class and then I would pass it the number the pin number okay when I want to interact with it I would call the object say red LED and then I would pass it num blink and delay time hopefully that makes sense but let's move on so I've created my class now I just need to come in and I need to write my program so we got to tell it what this is outside the class right I have finished the class and I've come back unindented all the way to the left and now red pin was what that was 15 green pin that was 14 and then let's say my red blinks how many times do I want to Blink red let's say I want to Blink it five times and then the red delay is equal to 0.5 okay now I'm going to have green blinks and that is going to be I want to Blink the green one 10 times and then green delay is equal to 0.1 these are just my parameters that I want to Blink now I need to create my LED objects I'm going to call this physical object this red LED D physical object I'm going to call it red L now that is an object that is going to be created in the class what led and what does it need to know it needs to know the pin number red pin so when I say red LED is equal to LED it runs up to this class it does the init it gets the LED pin number and then it sets the self. pin number to LED pin and then it does the pin mode self. obj could have called that anything is equal to pin self. pin number which we set up here and then we make that an output pin okay so now I have created the red LED object of Class LED now green LED is going to be equal to LED of what green pin now if I run this it should set it up but it's really not going to do anything because I've just set up the object machine what is that okay what is that from machine import pin what is it not like that line three ah that one scared me badly that that scared me bigly but what is the problem why did it do that why did it give me that problem you see down here I was set on local Python and I hadn't connected I hadn't connected okay now that I connected now that I connected it will be able to import wow I thought that it was some class problem that I didn't understand but that okay now it's going to run and boom it set up those two objects the green LED and the red LED it's ready to go but I didn't tell it to do anything I just set it up so now that those things are set up what what I could do is I could create my while loop while true when is true true true is always true and I could say red LED and then what do I want to do I want to dot blink and then I want red blinks that many blinks and then red D delay like that now why did I just do red blinks because I created the red LED object it came up here it ran the init so it's all ready to go now what do I do the object is red L and now what I just do is I call the method which is Blink it is not doob do blink no no no no that is all taken care of inside from the outside world all I do is call this method how do I call the method it is the object dot the method with the parameters and now what am I going to do green led. blink and then here green blinks comma green delay is this making more sense this is the second time you've seen this hopefully this is making more sense let's run this thing all eyes on the LED let's run it and one two 3 4 5 whoa that didn't work green delay Del what is that greed greed delay green will work better okay all eyes on the LEDs 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 1 1 2 3 4 5 6 7 8 9 10 okay do you see how that works boom okay so you see now down in this program we can just do a red LED blink or a green LED blink so you see we're not doing any calculations up here in the class we're not doing any calculations we're just setting up a physical object and we are interacting with this physical object through the class LED and look how clean our code is down here now red blink red led. blink obvious what that is doing green led. blink obvious that's what it's uh what it's doing and now you pass it green blinks and you PL pass it green delay all right see how much sense that makes hopefully this helps you understand a little bit better how to do objectoriented programming I've given you two examples now and the only thing that I could see that you could be a little bit confused about is is that this self is red LED right the self is red LED D but I've got to give it some name here okay this self you know what what I could call this is I could call it self doob uh my pin or something like that okay and then that case then I would set this to my pen but you've got to give this a name here obj and you know if I could do something besides blink if I could do something besides blink I could then have a different object and I could do something different with it okay so you see you could have all types of different methods inside of this overall class all right guys I hope this makes sense give me some feedback were you still confused after lesson number 63 and if you were did lesson 64 kind of help you understand how these things work a little bit better I hope it does because I really really really had a lot of trouble I mean I just got blown away when I first started doing this because they just jumped in with all this self stuff and it never really uh it never really made sense to me so hopefully seeing these two lessons it makes a little more sense to you so I've taught you two times on this and so this is your homework assignment sort of like we did with the LED I want you to create a Servo object a class for servos a class not for LEDs but a class for servos and this you're going to have to do a little bit more work but what I want is I want to create a method where you know I create a Servo object you know that going to be inside of a class I'm going to have the class of Servo and then I'm going to create a Servo object which could be like my blue Servo that could be my Servo object and then I want to interact with this just by saying something like like Blu Servo dop for position Blu Servo dop and then I want to give it a number from 0o to 180 so when I want to work with this Servo all I've got to do is I've just got to say blue Servo dop and then pass it the angle now if you remember in micropython and the Raspberry Pi Pico the way we were working with the servo before we were doing setting up the pwm and then we were calculating these values and we were doing all of that stuff and then every time that you wanted to use the servo to a position you had to go through all those calculations I want all that heavy lifting to be done up in the class and in the method and then down in the using of it it's just something like my Servo or blue Servo dop and then you pass it the angle you want now you probably don't remember how we set up this because it was a a number uh it was a number of uh lessons ago so if you want to review how you just work with the servo you can go back and look at lesson number 36 again and I show you how to set up the pwm pens I show you how to calculate an angle based on the timing and all of that sort of stuff so you can see all of that in lesson number 36 but then you take that and then what you do is you use it to create a class where you never have to think about all that nonsense again you just load in your class and then you can interact with the servo very easily does that make sense because this is really a perfect example of where you would want to do a class because you don't want to have to go back and watch lesson number 36 every single time you want to use a Servo and you're probably not going to remember 6 months from now what you learned in lesson number 36 so if you don't remember lesson number 36 go back and review that and then set up your class that allows you to easily and simply work with a Servo and then from now on anytime we're going to work with a Servo using uh the Raspberry Pi Pico W will go in and we'll load that class up I hope that makes sense guys I hope you're having as much fun taking these classes as I am making them want to give a shout out to you guys who are standing with me on Pat patreon your support and encouragement on Pat patreon is what keeps these great lessons coming you can also help me by giving me a thumbs up helps me with the old YouTube juice if you will leave a comment down below if you haven't already subscribe to the channel and when you do ring that Bell so you'll get notifications When Future lessons drop and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy dcom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,566
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: 3wyCL9QK_uY
Channel Id: undefined
Length: 21min 38sec (1298 seconds)
Published: Tue Apr 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.