Lesson 11 | Understanding loop() and setup() | Arduino Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I hope you're doing fantastic in this lesson we're going to be talking about two very special functions that you will use in every single Arduino sketch that you write they're called set up and loop specifically in this lesson we will talk about how setup operates and the type of code that executes how the loop function operates and the type of code that it executes how the apparently benign loop function enables amazingly responsive outputs and then how to get those little ships inside the glass bottles I want you to pretend for a moment that you own a specialty Italian sub shop store business is great and you have wonderful customers but you have one major problem you have a very high turnover of your employees now this is mostly because the majority of your employees are teenagers and by nature you know they just kind of move on to different things it feels like as soon as you get an employee trained up they move on to Harvard Business School or they get some scholarship to MIT MIT or you know they just decide that bungee jumping all day long is better than making subs in order to help train new employees you create two binders of procedure lists so one binder is called the morning setup binder and it lists all the stuff that you have to do when you open the store stuff like unlock the front and back doors turn on the open sign put the delivered sausage in the fridge and get the broccoli out of the fridge it's stuff that only has to be done once and then once you've done all that stuff a new employee can take that binder and stick it on the shelf then they can pull out the sub making binder now this is the binder that lists step by step all the stuff an employee has to do to make a sub and deliver quality customer service it might go something like this when a customer walks in welcomed them with a warm hello ask them what type of sub they want ask them what type of bread they want ask them what type of condiments they want ask them if it will be for here or to go see if they want to add one of Mama's homemade cookies to their order I think you get the gist the binder lists these steps in detail and talks about the quantities of toppings for different subs this sub making binder is really the procedure list that drives the task of the employee now this is the binder that the new employee is going to keep open all day long when they're starting out and they're going to go back to it over and over again for every sub so what does this have to do with programming well a program in Arduino has to do similar things like the sub shop there are certain tasks that need to get done just once when the program first starts things like setting up a serial communication to talk with the computer over a USB port or setting the modes of microcontroller pins as either inputs or outputs may be displaying a welcome message on an attached LCD screen or possibly turning a servo to a default location all these things might only need to happen once when the sketch first starts and never need to be bothered with again until the program's restarted or the Arduino is reset these are the type of tasks that are put into a special function called setup so let's take a look at setup notice first that the color of setup is different it's kind of like the color of baby poop on my display and what this coloring does is let you know that the Arduino IDE recognizes the function set up as a special type of function that's used for controlling the structure of the code now you'll also notice right after the word setup there is an opening and closing parentheses the set up function doesn't need any arguments to operate but you still have to include the opening and closing parentheses just like any other function you also notice the word void to the left the void means that the set up function will not return any data so this is a function that doesn't need any arguments from us in order to operate and it doesn't return any values to us but it does do something its job is to execute all the code inside its opening and closing curly braces precisely one time now let me say this again and with the dramatic pause because this is that important setup only runs once so when the program starts up setup will run just once all the code inside the curly brackets will get executed one time so if you pretend the program is like the high schooler at the sub shop the first thing she does when she comes to work is open up the setup binder and go line by line until everything is done once everything and setup is finished she closes the binder and then she goes on to the sub banking binder so for the Arduino sketch the next binder is the loop function the loop function looks just like the setup function it doesn't take any arguments it has the opening and closing parentheses it doesn't return any values so it has that void to the left and then it has the opening and closing curly brackets just like set up when loop gets executed the program will work line by line down the sketch but what's different is that when the code between the curly brackets has been executed then the program starts back at the top of the loop and does it all over again so imagine the teenager in the sub shop during the lunch hour rush he has customer after customer lined up and he's going through each line and the procedure guides step by step he asked what kind of sub they want he asks what kind of bread they want and yes if they wanted her here to go so on and so forth and he keeps working down that list until he gets to the end of the procedure list and then he starts back at the top of the list with the next customer and he does this over and over and over all through the lunch rush and all day long so this is like how the loop works it executes all the code inside the curly brackets step-by-step when it gets to the closing curly bracket starts at the top working through all of the code again line by line and it does this over and over and over now how fast can I go through a loop how fast can actually execute this code well that's a really good question and the answer is that it depends but the microcontroller that the Arduino uses can run it 20 megahertz now each line of code you write might end up being multiple instructions when it actually gets translated into machine code but needless to say even a sketch with several thousand instructions will be looping pretty darn fast now I really do like this sub shop analogy but there's a key place that it breaks down and I want to talk about it so you're not mislead in the sub shop example the teenager waits to start the instructions again when a new customer shows up but the code in the loop automatically starts over once it gets to the bottom it jumps back to the top it doesn't wait for any outside input to do so now you might be wondering how is it that this same code running over and over again produces any coherent result how does it add any variation well think about that teenage son banker do you remember the steps that she had to follow lots of the steps could have different results depending on the response of the customer so she had one procedure guide but she could make a giant variety of tasty subs and it's the same way with the programming code you're going to write instructions that will do different things based on different new inputs for example if the water levels above some threshold then beep an alarm otherwise don't do anything so is the code loops over and over again it's probably doing nothing until until it sees that the threshold is too high and then it executes what you've asked it to do or maybe something like while the light is shining keep the servo arm at 60 degrees there's all amazing types of things that you can do all types of possibilities that you can imagine on how you can control things so one last thing the functions set up and loop are required in your sketches your sketches will not compile without them being present in fact these two functions are so integral that when you open up a new sketch in the Arduino IDE you'll find them already there for you pre-populated ok so let's review set up and loop our special functions that help define the structure of the program setup is written before the loop and the setup function only executes once so all the code in the setup function only runs once the code in the setup function does things like set pin modes start serial communication with the computer displayed default messages to an LCD screen or just about anything that needs to happen only once at the beginning of a program the loop function comes after the setup function all the code in the loop function runs over and over forever really fast the loop and the setup function are required in your sketch the program will not compile without them ok oh hey that's it for this lesson if you ever forget with the loop and the setup do just say their names setup sets up the sketch loop loops over and over again ok hey I hope you have a wonderful day and I can't wait to see you in the next video lesson bye
Info
Channel: Programming Electronics Academy
Views: 29,321
Rating: 4.9284296 out of 5
Keywords: Arduino, Arduino IDE, Learning Arduino, Arduino(Brand), Arduino Tutorial, Arduino Lesson, Open Source Hardware Group, Microcontrollers, Electronics, Arduino Sketch, Computer programming, C++, void setup
Id: gAclRmEFn9s
Channel Id: undefined
Length: 10min 34sec (634 seconds)
Published: Wed Jun 08 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.