[MUSIC] [SOUND] In this lecture, we'll talk about the structure of a sketch, the standard structure. There are certain functions that are defined, that have to be defined, inside every sketch, and so we'll talk about those cuz every sketch has to sort of follow this template. So, the setup function. Every sketch has to have a setup function. So, a sketch is different than an actual, it looks different than an actual C++ program, in that it doesn't have a main. A C or C++ program always has to have a main, which is the main function where execution starts. It's a little different for this, because what happens in a sketch. We actually said this a few lectures ago. Is that the main is created but it's created behind the scenes. So it happens during the compile. But you don't have to write the main, instead you have to write the components of the main. First, starting with this setup. So setup is a program that's executed once when the arduino is powered up. So, when you plug it into power, the first thing it does, first thing it does is run the boot loader, but as soon as that's done, it executes the setup function. And the setup function, it happens one time. So it's used for initialization purposes. And there are many, sort of a typical thing in embedded systems, and in things, devices, that you have some operations that you need to do to initialize your devices is. It depends, completely depends on the application and what you're doing. Maybe you need to initialize a memory. Maybe you need to set up your communication link with Siri or something like this. If you were to look at our Arduino libraries for all the shields and things like this, they all often, not all but pretty much all have these, have a begin function which sets up the interface and is executed one time. So, Ethernet.begin, right, it sets up the Ethernet card the first time. You execute that only once. Serial.begin, it sets up the serial communication interface only once. You execute it one time, typically. So there are these setup functions, these initialization functions, and you put them into the setup function. So, it always starts off void setup(). Setup takes no arguments, it returns no values so it's a void, and between the curly brackets you just list whatever the setup functions are, or setup operations are that you wanna perform. So every Arduino program has to have a setup function. In addition to that, every Arduino program has to have a loop. Now this is a property of any sort of embedded systems. IoT devices, any sort of devices that you want to write code for. They're always, the code is always written in an infinite loop. Now that seems strange at a glance. If you've ever done any programming, infinite loops are generally considered bad, right? If you take a programming class and you want to compute the average between some numbers, you write the code to do it. At the end it finishes it, it returns, and it's done. It prints out its result. That's how normal desktop and laptop code works. That is never how an embedded system works. Because and embedded system, or an IoT device which is an embedded system those things have to operate. They have to be always operating as long as they are powered on. Imagine if I made a digital camera, and that digital camera, I press a button and it executes some code to take a picture. I press the button, it executes the code, and then it completes. The end. Then my camera's off and I can't do anything else. That's not what you want. You want this thing to constantly be waiting for input, right? So it takes the picture then it waits for the next button press. And maybe press a different button it does some different operation, right? But it's always running as long as it's powered on it's got to be attending to buttons and inputs and waiting for some kind of an input to trigger it to do something else. So always this code for any kind of embedded system, including Arduino programs, it's always written in an infinite loop. Now if it were an arduino you would actually write an explicit infinite loop. You'd say while one or something like that. While true, and that would be an infinite loop. The arduino, it does that automatically for you so when it makes it's main, it takes whatever you write into this loop function and just executes it infinitely. It sticks it into an infinite loop, so while one loop is probably what it looks like. It just executes that over and over. So whatever code you write in this loop, that's the main body of your code. Whatever code you write in this loop, it happens over and over forever, as long as the Arduino is powered on. So you have to understand that about its execution. That's how it's supposed to act, and that's how it's gonna act, so you put whatever main control flow you want. You write that into the loop function, which again takes no arguments, returns no value. And just understand that will get executed over and over, thank you. [MUSIC]