Paul McWhorter arduino lesson 33

 


 
But once we start working with functions, we’ve really got to start being mindful and deliberate about what type of variables we’re using. Are we using local variables? Are we using global variables? So I need you to pour yourself a nice cup of strong coffee, wow that’s good. I need you to fire up an Arduino IDE, a nice fresh one. I will move out of your way and we’re gon na start talking about variables now in defining variables in the past. We know that you can define different types of variables. What types of variables have we used? What we know for round numbers? We declare something is an int for the in between numbers. We define something as a float for a single character. We would call it a char or if we want a string of characters, we we define it as a string. Okay, besides those types of variables, we also in a very recent lesson learned how to do a raise where a raise would be something like a variable grade which might be float, but that variable grade has slots in it, so that we can. We can store a list of numbers, so we know how to do different types of variables and we know how to do a race, okay. Well, this is a related concept, but it gets it becomes very important when we start working with functions now. In the last lesson, lesson number 32: we learned about functions and using a function, you sort of create little blocks of code that do specific things, and then you call those blocks and that allows us to start doing modular program.

Then what the issue becomes is the issue becomes what part of the program knows which of the variables okay, so let me kind of define it this way. Everything that we have done so far has been global variables, and what do I mean by global variables? I mean that we define the variables before the void setup. We declare the variables at the very top and we because we declare the variables at the very top. The variable declarations are not surrounded by curly brackets. That means, if we define the variables at the very top everything in the program knows about that variable in all the different parts of the program and all the different clauses, all the different things within the curly brackets throughout the program all know what that variable is They can all know what it is and they can all change it. Okay and that’s, where the confusion comes in, if you start doing modular programming – and you have this function that does this thing, this function, that does this thing, this function. That does this thing. If they’re all sharing the same variables, you can sometimes get in trouble because one of those functions you might be using from like an old program, it might change a variable in a way that you weren’t expecting and then it can mess you up so sometimes it’s. Better once you start using functions, is to not use global variables, but to use local variables.

Now this is the way a local variable works. If you define a variable inside of any type of clause and type side of any set of curly brackets, that variable is only known within those curly brackets. Okay, that would be a local variable when you define it at the top its global. It can be used anywhere. So if I have a set of curly brackets and I define and use a variable in there once I leave those curly brackets it’s as if that variable, never even existed. Okay, so let’s take a look at this let’s use a global variable, just as an example. Here and then we’ll change and kind of play around with it. So you can start seeing what I’m talking about so let’s say it and then I’m, just going to say I’m gon na I’m gon na declare a variable, int it’s gon na be the variable X and I’m gon na say X is equal to 7. Okay, then, if I come down here, I’m going to do in the void setup, serial dot, begin, okay, we’re gon na use the old, trusty, 9600, okay and then I’m going to do a delay what’s. A hundred just give it time for the serial monitor to actually initialize and then I’m gon na say serial dot, println and then I’m gon na say you are in the void, set up okay and then I’m going to say serial dot print L in print print.

L in and then I’m gon na print X, okay, I’m gon na make this a little fancier, I’m gon na, say you’re in the void, setup, comma and x equals and then I’m going to take off the println. So it’ll put it all on one line: okay, so you are in the void. Setup and X is equal to whatever it is, and then I’m gon na delay I’m gon na delay about four seconds. So we can see that nice and easy and then I’m going to come down here and I’m going to kind of do the same thing I’m gon na do like this and just copy it and then paste it and then I’m gon na say you are in The void loop, okay and X is equal to X and then here let’s see okay. So that will be good. Just like that, and so it’s gon na say that you are in the void, setup and it’s going to wait and then I should probably put a delay here. Let’S just say two seconds: okay, so what’s it going to do it’s going to say you are in the void, setup and x, equals seven and then it’s going to say you are in the void. Loop and x, equals eight or x, equals seven again that’s what it should do so let’s run this and see what happens hoping for happy. Oh look, everyone’s happy, and then I want to open this up and then looks you were in the void.

Setup X is equal to 7 it’s waiting and it says you’re in the void loop X is 7 you’re in the void. Loop X is 7 okay. So what does that mean? If I define declare the variable up at the top? It is not within any curly bracket, so everything in the program knows what it is. Whether you are in the void set up the void, loop or you’re down in a function that you made so let’s. Try that let’s just say I’m gon na declare a function, void and I’m gon na declare it hello, okay and then open closed and then open, curly and then I’m gon na do a close, close curly and then I’m gon na say serial dot, println and I’m Gon na say, the hello function has X, equal and then I’ll do it like. I did before make it a little nicer here and then I will say serial dot print L in and then I will put X. Okay like that, and that looks good. I have an extra one of those clean it up a little bit. Okay, now what I’m going to do up here is, after I say you are in the void. Loop and X is equal to whatever then I’m, just gon na call I’m gon na call the function. Hello. Okay, like that now let’s, see if that’s gon na run. What is it not like? Serial.Print, I forgot, were you yelling at me when I didn’t put my colon and I hope so, okay, so I think that’s going to be happy and let’s look at what happens.

All right you are in the void. Setup X is seven. Okay, you are in the void. Loop X is 7. The hello function has X is equal to seven CC. You can see that when I come here and say hello, I pop down here and down inside of hello. It knows what X is because X is declared globally inside the void set up inside the void loop inside of any function. You know everybody is working with the same X, okay, but what? If? What? If I came down here and this function did something like X is equal to X, plus one and let’s say I really wanted this to be different. This was like a different, a different thing that I’m thinking about than the one up here. What happens? Okay, void set up X is seven all right void. Loop X is seven. The hello function has seven alright, but now let’s look at this. What has happened that hello function changed it to eight and it not only changed it to eight for the for the hello function, it changed it to eight for the for the void loop. Now. Can you see that that’s a problem that if one part of the program can start messing with the variables that another part of the program is using, you can get cut? You know you can cause a lot of problems and for simple programs like this it’s, not a big deal, but, as you start getting really complicated programs, you can see that it’s really a lot better to start using local variables so that my function it’s just gon Na have my own variables and I’m not going to mess with the other guy’s variables.

Just my function has my variables but let’s kind of take a look at that. So let’s say that right here, instead of declaring the variable up at the top, is a global variable. Let’S declare it as a local variable and let’s declare it inside of the void setup. So here, I’m, going to say yet and I’m gon na say X, is equal to seven. Okay, now, let’s see what happens. If I try to run it whoop, I get an error. Okay, I get an error down here where I’m trying to serial dot print line X because it doesn’t know what X is. Why does it not not know? What X is because X is declared as a local variable, and this is the way that you should understand it. If you declare a variable in any clause that is between any sets of curly brackets, it only knows that variable when it’s inside those curly brackets. So you see, I did not get an error here within the void setup, because when it said serial print line X, it knew what it was. Okay, so let’s come in here and let’s. Just get rid of this. Okay, let’s get rid of this, and if we get rid of that, then we’re not going to want to call it. Okay and now let’s see what happens. Okay, we still get an error, because this one doesn’t know what it is all right. So let’s say that I am going to declare it here again: okay, so I’m, going to say yet X, just make it an X all right, okay, let’s see what happens now.

You’Re in the void setup X is equal to seven okay and then look at this. Now X is equal to zero. How come because this X is not the same as this X. Okay in the void setup X was equal to seven, but this X is only good between these curly brackets. Here I’ve got a different set of curly brackets, and so this is a completely different X all right, so a local variable is a variable that is declared within a set of curly brackets in another set of curly brackets. You don’t get to see that and you can’t change it and it’s, not even about something as big as a void setup or a void loop or a function. Let’S try this okay, so let’s say for INT. I is equal to 1 to I is less than or equal to 5. Okay, I is equal to I plus 1 okay, open curly and close curly. It automatically makes that closed curly. When I do the open, when that that messes me up because I’m used to having to close my own things, that’s, okay, all right so I’m going to say here, you note X – is equal to 5, okay and then I’m gon na, say, cereal dot, print line And I’m gon na say you are in a for loop and x, equals okay and then I’m going to say I’m gon na make that cereal print I’m. Sorry, you know what we’re doing here and then I’m gon na do cereal dot print, L in and then I’m gon na print, X, okay and then I’m gon na fall out of that.

So here, I’m gon na have a variable X and the void set up at 7 and then here, I’m gon na have a variable in this for loop and inside that for loop, it is gon na be 5 and then, when I come down here, I’m gon Na print it out outside the for loop now, what do you think is X, gon na be 5 or is X, gon na be 7 well, let’s see go like that and we get an error right. I hope you saw that. Why do we get an error? Because this definition, this declaration of X is only good within the curly brackets and even a for loop? You created a clause and inside that Clause is the only place that this X is known inside this Clause. This set of curly brackets is the only place this X is known, and so, when we get down here, this little X he’s an orphan because he hasn’t been defined yet okay because he can’t see inside that cause. He can’t see inside that other Clause, and so here he is not defined. We would have to define him like let’s say that we put him in front of the four for loop and say X is equal to two okay and now let’s download that or I’m sorry, I need to say: int X is equal to so I declare the Variable on give it a value all right, so now let’s go! Look okay, you’re in the void setup X is seven yes, okay, then you are in the for loop X is five and then what it’s? Two okay.

So in the for loop it’s. Five in that clause and then when we drop out of that clause, it goes back to two because that’s, where it was set here so changing X inside the for loop, does not change this one because it’s a local variable. Does this make sense? I hope it does it’s just kind of a an important concept. Now, you might just say: hey I’m, just gon na do global variables because it’s a lot easier, but once you start using functions and you start writing more complicated programs, you really need to start using local variables and then what you can do is you can pass Variables down to a function and then get numbers back, but things are controlled by how you pass the parameters and so that’s. Probably what the next lesson is going to be we’re, going to go back to functions now and we’re gon na learn how to use good programming skills by defining our variables within the different functions that we’re using and that way we don’t end up with code that Behaves in complicated or confusing ways: okay, hope this has made sense man. I would love to hear your guys comments down below. Let me know if this makes sense. It’S really really important. I probably should have dealt with it before now, but now yeah better late than never. Let me know your comments. Give me a thumbs up on this video think about sharing it think about subscribing to the channel.

This is palma quarter from top tech boy calm.

 
 

Paul McWhorter arduino lesson 33 Video

 

 

 

 

 

 

Paul McWhorter arduino lesson 33 news

 

 

 

 

 

 

 

Paul McWhorter arduino lesson 33 Social

 

 

 

 

official.arduino
2019-10-28T18:46:20+0000

We’re opening the Arduino IoT Cloud to other platforms, starting with the ESP8266 by Espressif Systems — NodeMCU, SparkFun’s ESP Thing, ESPDuino, and Wemos (to name a few) — along with other inexpensive, commercially available plugs and switches based on this module.
safe image.php?d=AQB6YIkvOaOwGAQg&w=720&h=720&url=https%3A%2F%2Fblog.arduino.cc%2Fwp content%2Fuploads%2F2019%2F10%2FDSC00029 - Paul McWhorter arduino lesson 33
official.arduino
2019-10-28T17:47:41+0000

How fast can you run the 40-yard dash? Find out with your own wireless timing gate system.
safe image.php?d=AQDAnlSUYJivJY5S&w=720&h=720&url=https%3A%2F%2Fhackster.imgix.net%2Fuploads%2Fattachments%2F1008638%2Fblob mOWHBn2WBw - Paul McWhorter arduino lesson 33

 

 

(Visited 14 times, 1 visits today)

About The Author

You might be interested in

Comment (21)

  1. Hey Paul!

    I have really learned a lot from your Arduino lessons, but I’m curios to ask if you could dive in to the subject of how to create libraries with the Arduino IDE?

  2. When you define the variable i in the for loop since it is not part of the curly brackets is it only local to the for command or is it local to the void Loop?

  3. Paul did you ever do a video on how you make your videos? I am teacher and am very impressed with your production quality. Thank you!

  4. How does this series have so less views? 😮 This is great! Thank you so much for the time you put into these videos. Almoust trough the entire course!

  5. These lessons have been invaluable. I wonder though, will you consider doing a series of lessons on Iot? At the very least I would like to encourage you to do a lesson, or series a of lessons demonstrating sending email and text via Arduino when any particular condition is met.

  6. Best base level instructor on the Tube.. Spent days on an issue that you just solved in the first 10 minutes of this video.

  7. Hey Paul, is there any way you could do a glossary page that kindly lists definitions for each of these functions? I think it be quick reference for those of us older folks mixing the different terms up.

    Also, is there any way you do some wifi stuff? Specifically how to communicate board to board via the wifi or LoRa.

LEAVE YOUR COMMENT

Your email address will not be published. Required fields are marked *