![]() The void loop() is a function that executes indefinitely until you power off the Arduino. There are other statements that can be included in the void setup(),the above functions were only examples, but the important thing is to remember that the setup() is a statement that runs only once at the beginning of the sketch. Or to start the serial monitor example: serial.begin(9600) It usually contains statements that set the pin modes on the Arduino to OUTPUT and INPUT, example: The void setup() is the first function to be executed in the sketch and it is executed only once. The sketch will not compile without either one. It is important to understand this code structure because every Arduino sketch will contain a void setup() and a void loop() functions even if they are empty. put your main code here, to run repeatedly:Įssentially this is a complete code, it complies and can be uploaded to the Arduino but there is nothing in this code. put your setup code here, to run once: A function has a “ scope” – like we have seen with variables and constants.To load the most basic Arduino sketch, open the Arduino IDE click on file then examples then 01.Basics and select the BareMinimum.A function can have functions inside it …. ![]() A function is like a little program on it’s own … inside a program. ![]() But more about “libraries” in a next part. Now if we have a function that is more of a generic use, we can even put them in what is called a library and re-use the function in our other programs. We just edit the function WalkTheDog() instead of having to edit the code in 4 different spots in the previous code. For example, if we forgot to add “Unlock the door” as a step. In most scenario’s, using your own functions will also result in a smaller compiled program for your Arduino or computer, therefor being more efficient with memory needed on your Arduino (or computer).īut functions have also the advantage that if you made a mistake in your steps of walking the dog, that you only have to modify your code in one spot: the function – which makes your code more manageable. You see, that not only our code has gotten much shorter, but it also has become much more readable – the “function” is called as if it was a regular statement. We could create a function, let’s call it “WalkTheDog()”, and define it as follows. Our program is pretty long, right? And a lot of repeating code as well … Now let’s assume our program handles our day when it comes to walking the dog: Say we have a dog, which needs walking 4 times a day: At 8 AM, 12 PM, 5 PM, and at 9 PM. Let’s look at an example of what a function could be – just to grasp the concept.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |