How to Use Functions in Arduino Programs - Ultimate Guide to the Arduino #18

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the three in one smart car and iot learning kit from Sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or Follow The Line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I had lots of fun building all the projects in it so click the link in the description below to order the kit from some founder foreign [Music] we're going to learn about functions we'll see what functions are how to use them and how to write your own we've seen a few different functions already for example digital right and digital read are functions functions are the code of your program that get things done our functions have an input and an output inputs are the information you give the function the function performs an action on the input then outputs a result for example consider the function x equals y squared plus 1. if we input y equals 3 the function does the math and outputs x equals 10. some functions like this one return numeric values another function that returns a value is the digital read function which Returns the voltage state of a pin either high or low other functions don't return values but perform physical actions for example the digital write function switches the voltage state of a gpio PIN to either high or low and the tone function generates a square wave with an adjustable frequency are written with a return type followed by the function name the return type of the function is the data type of the value the function returns so if the function returns an integer the return type of the function is int with functions that don't return any values the return type is called void this type of function should look familiar because the setup and loop functions both have a void return type they don't actually return any values inside these curly brackets is where the function's code goes you can put any valid Arduino code in here once you have your function written you're going to want to use it of course using a function is called a function call or calling a function to call a function all you need to do is type the function name with open and closed parentheses and a semicolon I'm calling this function in the loop section but you can call functions in the setup section too right now this function isn't very useful we haven't given it any inputs and there's no code to Define what it does with those inputs giving a function an input is called passing it an argument when we pass arguments to a function we're sending it the inputs it needs to perform its task remember the digital write function the digital write function needs two pieces of information to work the pin number and the mode pin and mode are called parameters parameters are the types of data the function takes as an input an argument is the actual value you provided say we want a digital write pin 10 high pin and mode are the parameters but 10 and high are the arguments you'll know which arguments to pass a function if you write it yourself but if you're dealing with built-in Arduino functions and you don't know what parameters to use the Arduino reference pages will tell you if you're working with functions in third-party libraries check the documentation for the library or look at the code inside the Library files now not every function takes parameters with some functions like the milliseconds function all you need to do is call them and they'll return a value okay so how can we make this empty function do something as a simple example let's make it print a number to the serial monitor normally we would just use a Serial print inside the loop section but one of the benefits of functions is that you can reuse them you might not always want to print the same number so let's use a variable instead now we need to declare X in a way that lets the function use it to do that we enter the data type and variable name inside the parentheses here now to use the function we call it inside the loop by writing the function name followed by a semicolon then we pass the function in argument let's say we want to print the number 10. then we just have to put 10 in here okay there it is now let's try something different let's write a function for blinking an LED then use that function to Blink two LEDs the function will take an argument for the LED pin number and another argument that will set how long the LED stays on and off let's start by writing the function the function will just be blinking LEDs so it's not going to return anything so the return type can be void let's name it blink LED before we Define the parameters let's write the code to Blink the LEDs we'll need a digital right instead of passing an actual pin number to digital right let's use a variable called pin and we'll start the blinking with a high voltage then we need to delay let's use a variable inside the delay function too now we need another digital right to the same pin but now we write it low and we finish it with another delay so now we have two variables in this function the pin number and the delay duration when we call this function we'll want to be able to Define values for these variables in other words we want the ability to pass arguments to the function so we need to tell the function which parameters it should take do that in the parentheses up here the parameters are the pin variable and the duration variable so we need to declare them both and put the data type before each one they're both ants so I'll write in pin separate them with a comma and then write int duration so when we call this function somewhere else in the sketch the first parameter will be the pin number and the second parameter will be the duration now let's write the loop section where we're going to call this function to call the blink LED function all we need to do is write the function name followed by open and close parentheses and a semicolon we're going to Blink two LEDs so let's use the same function twice okay now we need to give these functions some arguments we Define this function to take two parameters the pin number and the delay duration we'll do that in a second but first let's write the setup section because we still need to set the LED pins as outputs let's connect the LEDs to digital pins 9 and 10. so we'll use pin mode with pin 9 for the first argument an output is a second argument then we'll use pin mode with pin 10 as the first argument and output is the second argument okay so now pins nine or ten are set as outputs now we can go back to the two blink LED functions and enter the arguments when we Define the blink LED function we said the first argument should be the pin number the first led will be connected to pin 9 so let's put a 9 here we said the second argument should be the blink delay so let's put 500 milliseconds here the second LED will be connected to pin 10. so let's use this blink LED function to control that PIN let's blink this led on and off every 1000 milliseconds okay so now it's going to happen in the sketch first the setup section will be executed where pins 9 and 10 are set as outputs then the loop section gets executed the first line in the loop section is This Blink LED function when the sketch Encounters this that's a function call so it comes down here to where the blink LED function is defined first it sees that we Define some parameters an in variable called pin and an INT variable called duration so the sketch takes the number nine from the first argument up here and stores it in the pin variable down here then the sketch sees the duration variable so it takes the second argument from the function call which is 500. and stores that in the duration variable now it continues on executing the code in the function the first line is the digital right to the pin variable 9 was passed to the function's pin variable so this will write pin 9 high then we have a delay with the duration variable inside the parentheses since 500 was passed to the function's duration variable the delay will be 500 milliseconds then we digital write the pin variable pin 9 again this time with a low voltage next we delay for 500 milliseconds the value stored in the function's duration variable now that all of the code in the function has been executed the sketch exits the function and returns to the next line of code back in the loop section the next line in the loop is the second blink LED function so the sketch now executes This Blink LED function passing pin 10 to the pin variable and 1000 milliseconds to the duration variable so the LED connected to pin 10 will be written High then delayed for 1000 milliseconds then it'll be written low and delayed for another thousand milliseconds let's upload this and see if it works the way we want it to I have an LED connected to pin 9. and an LED connected to pin 10. the LED attached to pin 9 bleaks first on for 500 milliseconds then off for 500 milliseconds then the LED attached to pin 10 blinks on for 1000 milliseconds then off for 1000 milliseconds so it's working okay that was a nice introduction to functions we're going to see a lot of different functions in the coming lectures but in the next video we're going a step further and learning about classes and objects [Music] Sun founder is my go-to source for sensors modules and other parts for the Arduino and Raspberry Pi have a huge selection of stem Robotics and iot kits and lots of useful sensors and modules every product has an online tutorial with wiring diagrams and example code we also offer free shipping on all orders with no minimum give them a try at www.sunfounder.com next time you need to order some parts
Info
Channel: Circuit Basics
Views: 2,806
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: RKk42mG7ATU
Channel Id: undefined
Length: 14min 22sec (862 seconds)
Published: Fri Apr 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.