Paul McWhorter arduino lesson 34

What I need you to do is pour yourself a nice, strong cup of coffee and get ready to learn a little bit more about functions. Okay, what we’re gon na learn today is how to return a variable value from a function. Okay, this series of lessons really started in Lesson number 32, where I showed you how to start using functions in Arduino and the idea behind a function is to take a complicated task and break it down into logical chunks and then to define code or functions to Do those logical blocks and then your main program is much easier. You just call these blocks and execute them, and you can start doing things in a more modular fashion, but once you start doing that, you have to start being very mindful of the difference between global variables and local variables. So one way you can do, it is just make everything a global variable. All the functions know what all the variables are and do it that way. But what we showed in lesson number 33 is is that, as we start doing more sophisticated programming, we really need to start using local variables where the variables are local to the individual functions and we pass values back and forth. And so what we’re going to just look at today is we’re gon na start, looking at our first look at using local variables and how we can pass a value back from a function up to the main void loop.

So you see all the sudden starts, looking really cool, really modular, but the way we did this is we did this with global variables. That means we define the variables up here and all of the functions knew what all of the variables were, but we’re going to start moving into local variables, all right and so let’s think about that let’s think about what we’re really going to do is we’re gon Na make average a local variable and so we’re not going to define it up there. But what I’m going to do is I’m going to define it down here, so I’m gon na say, float and I’m going to define average and now who’s. The only one that’s gon na know what a V is, the only one that’s going to know what a V is is between this curly bracket in that curly bracket, because AV is local only to this and I’m just going to take that one out and then What I’m gon na do here is just say after I calculate average I’m going to do a serial dot, print, L in and then I’m gon na say make it a print say your average is and then I’m gon na do a serial dot print L in I’M gon na say AV okay, so this print statement will be happy because AV is inside its defined inside this clause inside this void, loop and so it’s a local variable. But this print statement is part of the same chunk of code where I define it.

So this should work very well, but now what we’re gon na do is instead of just calling F grades like this. What we’re going to do is we’re going to say that ad is equal to the function. Well, if we make it equal to the function, the function needs to have a command in it where it returns something back up to us so, rather than just have global variables up here, we’re gon na start passing things back and forth, and what are we going To pass up well AB great so when we come down to AB grades before we just calculated F what’s the problem here, it’s not going to know what AB is because AB was not defined up here and it is not defined in AB grade. So it’s not going to know what it is and we’re going to use a different variable. What I’m going to do is I’m, going to define one it’s gon na be float and we’re gon na call it average right and then we’re gon na calculate average. Now what’s the problem this is gon na know what average is, but up here, it’s not going to know because I’ve got local variables here, local variables here. So when I have chunks of code using local variables, this is the first way that I’m going to teach you how to pass a parameter back up, and so what I’m going to do down here after I calculate average, I’m gon na, say return average.

Okay. So what happens? This function have grades is going to declare a variable average and then it’s going to calculate average and what’s it going to do it’s going to pass it back up and then who’s going to catch it it’s passed up to here. So AB is going to catch. It AB is equal to this. Well, what comes here whatever you return? What am i returning I’m returning average now you know how, in the earlier lesson, 32, I just put a void in front of your a void in front of everything so like. If you define a function, you just define it as void, that’s what you do, if you’re, if you’re using global variables, but now I’m returning something. So what do I have to do when I define the function? I’Ve got to define what type of variable I am returning. What am i returning I’m returning a float so now. This is still the command that defines this function, but it’s not void it’s, not empty it’s, returning something what’s it returning it’s returning average when average pops back up here who catches average the variable a V okay. So this AB grades will go down to AB grades. It sees it’s going to create a float which float does it create it creates average and returns that you see sort of like passing the football I pass it and you catch it. Okay, that’s how this thing works so let’s see now.

If this thing will work, let’s hit it here, oh it’s, not happy what happened. Oh, oh, oh, oh, okay. I see this print grades which using average as well, and so what we are gon na do is. We are just going to get rid of this we’re. Just going to get rid of that whole function, does it make sense that when I did not declare AB as a global variable, the other functions that wanted AB were not happen, so we just got rid of that. Okay, I think everybody is happy how many grades I have three grades input, the first grade 97 98 and you guys are most exceptional students. I think you would probably so we’re gon na say a hundred. Your average is ninety eight point: three three boom. Okay, now let’s just go back over this and let’s make sure that we understand what happened. Ave is now a local variable known only to the void. Loot. Ave is equal to whatever the function have grades returns. When we go down to AB grades now it’s returning something so it’s not void its returning something it’s returning a float, so I define the function with float AB grades and then I have to declare the variable that I’m going to calculate, which is average. I calculate average and then what do I do? I return average. What does that do that comes up here and puts that value here, which then is caught by a V, the local variable? Okay? Does that make sense? Let’S just start with another one just to understand how to do this return, just just from scratch, I’m going to try to do just a really quick one.

So let’s come up here and say file new and what I want to do is let’s just create a function, that’ll square, another square, a number okay and let’s use local variables, and so what I’m going to do here is we always need our trusty serial monitor. So serial dot begin 9600 now we’re going to come down here and we’re gon na define X, so we’re gon na say let’s make it a float, not a cattle float, float X and then I’m gon na get X from the user. How do I get something from a user? I ask I wait I read and so I’m going to say serial dot print L in what number do you want to square like that? Let me make this big, so what number do you want to square being diligent? To put oh man, I messed up for you guys. You only get me up here. I didn’t do the shift that’s most of them, where I hit the caps lock. Okay, what number do you want to square? I asked: what do I do? I wait so I’ll say Wow serial dot, they all equal, equal zero. What do I do? Absolutely nothing, so an open and a closed Clause open and close curly. I forget that it puts those clothes in there for me, so I wait and then what do I do read so I’m going to say X, which is what I’m waiting for is equal to serial dot.

Parse float. So now I read X now. What do I want to do, I want to say X, 2, which would be like x. Squared is equal to what we’re going to call it a function x. Squared now let’s think I have a new variable here. I better define it I’m using local variables. So I’m going to define it here, a fellow 80 and then I’m going to say x, squared so now, I’ve just I’ve declared my variables and X, 2 is equal to x, squared and then serial dot print I’m going to put a series of things in here. Okay, let’s put in X just to print out X and then say serial dot print and then put in the word squared, is equal to and then serial dot println and then put X 2. So what should this do? What should this little thing of code? Do it should ask you for a number and then it should square it, and then it should say that that number squared is equal to and then give you the answer now. What is the situation that we have here? Well, we haven’t taught it what x. Squared is so we’ve got to define the function. Where do we define the function less than 32? We define it after after the void loop. That means after the whole void. Loop has ended so now we’re going to define it remember what we learned eat earlier. This lesson is it avoid no it’s not avoid because I’m gon na pass x squared back up to it.

So what we’re gon na do is x, squared is afloat so we’re gon na call the function, a float function and we’re going to call it the x. Squared function and then x, squared is gon na, be a void and then we’re gon na open curly, and then it should close the curly for me there it is now. What does this do? It’S gon na have to know what I it’s gon na have to know what X is. So how is it gon na know? What X is X is a local variable, so I couldn’t just say like float answer, I couldn’t just say float answer because if I said float answer and then I say X is equal to, if I say X, if I say answer is equal to x times X, what is the problem? Well, the problem is it doesn’t know what X is because I made it a local variable, so I’ve got to get this X down here somewhere. How can I do that? Well, I got ta pass it inside of here. So what am I going to pass I’m going to pass X now if I pass X, I’ve got to catch it here and when I have to define it again so I’m going to say, float X, so this X here this local variable X, is passed within These parentheses down to this X, which is going to catch, but this X is different than attics, but it passes it now.

Answer is equal to x times X, this x times X. Now, what do I need to do? What I need to do now is: I need to return what I’m going to return X then that’s going to come back into x2. You think this is gon na work. How many mistakes did I make that I don’t know about let’s, see that wants to save it. Okay looks like it might be happy. What number do I want to square? Well, I want to square 2. 2. Squared 2 squared is equal to 2. What went wrong? Well, it’s good time sometimes to have a problem for the space in there X, 2 is equal to x, squared and then I print X, 2 and then x. Squared float answer. Oh, what did I return? Did you catch that I returned to X back? What did I want to return answer? Did you guys catch that? I hope you caught that okay. What number do I want to square to 2? Squared is 4 boom. I do want a space in there. So I’m gon na put a space right. There let’s try that again. Do you see what we just did we’re starting to do modular programming we’re starting to do modular programming with local variables? What numbers do you want to square 2.5? Okay. 2.5. Squared is equal to 6 point 2. 5. Hey. That looks good. All right, so what you can see is is that once we start using local variables, we have to be mindful of passing values down to the function and then returning values from the function, and so really.

What I wanted you to see today is the object of today’s lesson was to see that if you’re going to return something when you define the function, you don’t define it as boy, you define it as a float or an INT or a string or whatever you’re. Returning you define it it’s, whatever you are returning. If you are just using global variables, you can make all your functions called void, void void. But if you’re returning something you need to define the function using the type of variable that you are going to to return. And then you can see that we return answer and answer was a float. So therefore, this has to be a float. So really, the purpose of this was to understand how to return something and then to have a variable up in the call that is just equal to what you call and then doesn’t. This kind of start getting excellent x squared is equal to x. Squared of what number of X and then that’s passed down I’m gon na do another video really. The purpose of this video is to learn how to just return something to use the return function, but in the next video we’re going to learn more about passing parameters to and from functions inside the parentheses. Does this make sense? I would love to hear from you guys. I would love it if you would leave a comment think about giving us a thumbs up subscribing to the channel okay, because I think this is really important.

Stuff and we’ve done a whole lot of lessons and we’re a little late to get to this, but this modular programming is really important and starting in Lesson. Number 32 is where we’re really getting into this modular programming lesson. Thirty five I’ll talk to you more about passing parameters back and forth: palma quarter top tech boy comm.

Paul McWhorter arduino lesson 34 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.
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.

(Visited 56 times, 1 visits today)

Comment (21)

1. Kenny Van Der Sypt says:

Each video brings solutions to my projects problems! Loving this 🙂 Didn’t know anything about coding last week. Not saying i’m a guru now, but now i’m able to somewhat read existing code and use it for my own.

2. maryam salama says:

what if i want two return two variables?

3. Nathan Enos says:

Great video! I’ve been trying for several years I’ve been trying for several years to learn the Arduino, watching this video and that video, reading articles, and nothing ever fell together. When I started watching this series a few months ago, something finally clicked. I’m no expert yet, but I’m actually making progress! Thank you!
P.S. I did catch the mistake (returning x instead of answer). If only I could catch my own mistakes like that! 😀

4. Papa Alpha Oscar says:

My averages keeps on compounding the result from previous calculations and I can not figure it out. Code is the same as far as I can tell.

5. Louis Spinelli says:

Great tutorial!

6. Jeff Hambleton says:

Paul, I’m getting a problem in the code I wrote and code I copied from your lesson.
How many grades. I press 4
I input 66 and get the “Please Input a Grade” message twice again.
It keep repeating.
I don’t understand the Serial.available and the Serial.parseFloat well enough.
I tried sticking in small timers but no change.
Thank Jeff

1. Paul McWhorter says:

make sure your serial monitor window is set to no line ending.

7. Jeff Hambleton says:

I’m using Arduino IDE 1.8.8 and Windows 10

8. Harman Chawla says:

lifesaver! thank you!

9. Paul McWhorter says:

You know, my coffee machine just broke, so I am very sad.

10. Gediminas Gresevicius says:

Can’t wait for your next video. Keep on moving with function. You are so good in explanations.

11. misterdroid.ma says:

Thank a lot for your video lessons they are great I have a question for you . I have a lot of difficulties to parse values from this function to an other one . I need those int root in an other function Alarme to compare hour and mn received with esp8266 time clock system

void firebasedata(){

Firebase.stream(“/Alarme/”, [](FirebaseStream stream) {

//String eventType = stream.getEvent();
//Serial.println(eventType);
//String data = stream.getDataString();
//Serial.println(data);

const size_t capacity = JSON_ARRAY_SIZE(6) + 5*JSON_OBJECT_SIZE(3) + 130;
DynamicJsonBuffer jsonBuffer(capacity);
////StaticJsonBuffer<500> jsonBuffer;
////char json[] =stream.getDataString();
JsonArray& root = jsonBuffer.parseArray(stream.getDataString());
////if(!JsonArray& root.success()) {
////ESP.restart();
////}
JsonObject& root_1 = root[1];

int root_1_H1 = root_1[“H1”]; // 9
int root_1_Mn1 = root_1[“Mn1”]; // 6
int root_1_scID1 = root_1[“scID1”]; // 1

//
JsonObject& root_2 = root[2];
int root_2_H2 = root_2[“H2”]; // 14
int root_2_Mn2 = root_2[“Mn2”]; // 11
int root_2_scID2 = root_2[“scID2”]; // 2

JsonObject& root_3 = root[3];
int root_3_H3 = root_3[“H3”]; // 14
int root_3_Mn3 = root_3[“Mn3”]; // 11
int root_3_scID3 = root_3[“scID3”]; // 2

12. ONYONYMOUS ONYON says:

16:38 BOOM!! O.O brain cosmic all conected!!.jpg Thankyou 🙂 !!

13. David Stockford says:

will you be updating the Toptechboy site lesson list? I only found this by accident. I thought you had stopped creating lessons.

14. Clair Smyers says:

Excellent video. Thank you Paul! It explains a lot!

15. Reverie Christine says:

THANK YOU VERY MUCH <3

16. Frangod3123 says:

Thanks for being so animated and enthusiastic!!! It really helps me to better capture the knowledge.

17. Hamed T says:

Mr.McWhorter please make another lesson about “switch, case” and state machines. thanks.

18. #3d PK says:

Wonderful video

19. TGP Gaming says:

Watched many videos on functions.
This guy explain it THE BEST way you can imagine.
Great owrk Paul, love this video, finally understand it !

20. Erygion says:

Great lesson Mr. Mcwhorter, I’ve learned a lot and you explain it well. I look forward to future lessons. Thank you. ♾