Arduino Workshop - Chapter Three - Creating Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] as we've mentioned so many times before functions are an important part of your code a function is a group of statements that run to perform a task it could be as short as reading a sensor and returning the values or something much more complex a function will run when it is told to and upon completion will return to the main body of the code from where it left off Arduino uses two functions for every sketch which are set up and loop as we've discussed previously there are four main aspects of a function to consider you have the function return type the function name function parameters and function bodies an important note about functions is that they can receive data from the rest of the program and they can also pass data back to the program the first part of their function is the return type which defines the type of data that it returns to the program will use the function void loop as an example alright so the first part of the function is the worst void this means that it returns no value to the rest of the program perhaps you've heard of the term returns void before what applies for this as well if the function returned an integer value then it would be in Luke instead of void loop the function name is fairly self-explanatory you call the function or run it by calling its name in the same way that you can refer to a variable and then you pass the required data to it the loop function runs automatically so you don't need to call it but if you want to create a different function it would only run when you called it in your loop or setup so you can see here the name we have always blue that would be the unique name of our function the function parameters are the data that is passed to your function after the function name there are two brackets that contain the data being passed through the function now again if you look at void loop you can see that there's actually nothing inside the bracket and that means it doesn't have any parameters which can be passed data to it it's something lone-wolf function it just operates by itself and doesn't sync to create parameters for a function you specify the type of data and the name of the parameter so let's take a look at how we can use these concepts create our own functions in our code to make it much more sophisticated and clean now the example we're going to be looking at today is a hypotenuse calculator and if this throws back unfortunate memories of boring maths lessons I'm sorry but it's actually quite fun we're going to be using the simple concepts that you can find the hypotenuse or the long diagonal sides of a right angle triangle using a squared where a is one side you plus B Square which is the other side is equal to C squared so therefore the square root of a squared plus B squared will give us our hypotenuse so let's take a look at the program we've got no local variables no pin definitions this is entirely software based so we're initializing the serial serial ports use the serial terminal and we've got some local variables here in our void loop we've got into a interface and float results now this is important because while a and B are perfectly fine as standard integers the result of performing different functions and these integers could return a float value so first up then we've got serial print and this is some instruction so that we can create a nice user interface enter a side value where you would go through and enter side value and we're using something a little bit different in the terminal we're going to be actually receiving data from this terminal from the serial port and then bring it back to our Arduino which you haven't looked at before and we'll take a more extensive look at the UI port and the serial functionality in the next chapter but for now it's a really cool implementation enter a side value so it's waiting for response and we're using while exclamation marks serials got available to brackets now what this means is we're running an infinite loop while everyone on infinitely very continuous loop while ever there is no serial data available pretty much it will check there's a serial buffer for any incoming data it's left empty it'll just it will just stick on that loop and what it means is it's waiting for a response it's waiting for some inputs whenever it sees some input of any kind it runs the function serial or read serial I should my thing that a is equal to the return result of red cereal so that is how we call the function now let's scroll down to the function of read zero you can see that red cereal is an integer type function we have int red cereal and this is because the values that we're going to be returning are going to be integer type values but only in coding whole numbers into the serial port so it returns an integer type value and we can run it it doesn't take any parameters that takes no arguments from the main thing and it's an important point to note that the difference between an argument and the parameter you might have heard the words been used interchangeably and you can but they're actually slightly different in red cereal if we wanted to create some parameters we could use in G or in X or in some value that would create a new parameter in some value which is like a variable a local variable we can refer to in the function and that is the parameter but then when we call read zero we need facets and doug's so if we put ten in the brackets that is an argument we're using ten as an argument and it passes ten through to the function and then are the first the first parameter there is some values so therefore in some value is going to be equal to ten so this function we remove that anyway and crack on so the first thing we do is we create a local variable as you can see within I and we're making it equal to the return of a cereal function called pass in what this does is it takes the incoming data from the theorem owner which isn't just a standard integer type by default it uses the ASCII table so in fact the letter A corresponds to an integer value and a number corresponds to an integer value but they don't correspond to the same thing so we need to turn it into integer first which is where part in comes in handy then we're going to be calculating the size of the triangle so we need to make sure it is actually a valid side we don't have a negative side or a zero length side because that is just conceptual and it doesn't actually achieve the outcome of creating a hypotenuse calculator so if I is greater sorry if I is less than one which means if it's zero or a negative number we return and we return nothing which means that simply exits the function and goes back to where goes back to what it was doing it there's a no sorry try again very good so we can now have some sort of filter for incorrect data then if it's not equals one if it's valid if it's not less than one if it's a valid integer we go through and use serial print lines so we put a space and then we use serial passed in again and what this does is it clears the buffer so we now have eyes we've captured that incoming about data we've put a new line just for some styling purposes and then we've used partings again but we haven't put anything equal to it and this essentially just flushes out the serial buffer and make sure that there's no extra remanence remaining that we haven't captured we just want that first number very good and then we return we use return I so we access the function and return I and here a is then going to be equal to I so a has captured that incoming variable so we do the same thing we print enter the other side values we now have two sides we can calculate it same thing we wait for some data then B is equal to five we now have two values a and B for a two sides and then you see we run a new function called find side and here we are passing it some data we've got two arguments here a and B and we're passing those through the function parameters we go down to find side and a create int X and int Y so a comes in to any of equal to in X and in Y is equal to that because you've got to remember that in aim in C are just local variables with in void loop so if we just use the move in find side I wouldn't know what they are because they're not in that scope so we pass them on to int X and in Y which are local and then we calculate C which is equal to a squared plus B squared we take the square root of that we find C fantastic so again a floating type number floats hypotenuse is equal to the square root of x times X or x squared plus y times y or y squared and then we print out the result really really cool to have got two different functions that were using here so let's take a look and upload it to our board it's a really fun little hypotenuse calculator so zero monarchy will say enter a side value let's enter nine packs and hit enter to send it wait for a moment yet gives us nine and to the other side values left five the other side can be equal to 5 then it works that out it says ro9 squared plus 5 squared take the square root of that and so hypotenuse length would be equal to ten point three you know it's really cool we could use perhaps we'll use three and four this is a classic example of using Pythagoras theorem 3 squared is 9 4 squared is 16 and you add those together you get 25 which is a perfect example because the square root of that goes down to 5 which is a nice whole number and there you can see we've used two different functions to create a fun little hypotenuse calculator so you can see how powerful functions are you can use them to easily enhance your Arduino project or even a C or another programming language that you're creating that project with and you could of course take the contents of red cereal and the contents of font side and copy it into your code twice three times whatever it is that's just unnecessary waste it takes up all that extra room in your memory where you could just create a nice little function or tray some modular piece of code so if you have another project you're working on similar to the debounce example that we used earlier you could put all of that inside a function then you have this nice modules code you can copy and paste into different projects as you need so if that concludes the function on using logic statements and making decisions and we've looked at logic statements and math and functions we can group all of those together to create some really powerful routines towards the project as J tuned for our next chapter where we're going to be looking at operators and writing to the EEPROM and enhancing our understanding of using serial port
Info
Channel: Core Electronics
Views: 86,801
Rating: 4.773746 out of 5
Keywords: arduino functions, how to create arduino function
Id: JaQeOcQeP3k
Channel Id: undefined
Length: 10min 35sec (635 seconds)
Published: Tue Mar 07 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.