Arduino Programming: The Practical Intermediate's Guide To Learn Arduino Programming In One Day Step-By-Step (#2020 Updated Version | Effective Computer Languages) by Tudor Steve

Arduino Programming: The Practical Intermediate's Guide To Learn Arduino Programming In One Day Step-By-Step (#2020 Updated Version | Effective Computer Languages) by Tudor Steve

Author:Tudor, Steve [Tudor, Steve]
Language: eng
Format: epub
Published: 2019-11-30T16:00:00+00:00


Chapter 8

Understanding the Arduino Framework

In this chapter, we’re going to actually start looking at the code which fuels Arduino. At this point, we are assuming that you have your Arduino unit and you’ve got it set up and linked to your computer. This is the first step.

The second step is to get an Arduino compatible IDE. For most beginners and novices, the ones supplied by Arduino themselves will be more than enough. You can navigate to their website and either download the desktop IDE or use the web-based IDE. Either one will work perfectly fine.

So, from here, we need to talk about the structure of sketches.

An Arduino sketch has two basic components that make it up: the ‘setup’ function and the ‘loop’ function. Both are essential to the overall functioning of the Arduino sketch, and so you need to make sure that every sketch you run has them. Your sketches, of course, are not limited to just these functions, and you can expand with more functions at ease as we described back in the C chapter.

The Arduino ‘language,’ so to speak, is just an extension of C and C++, which means that C and C++ coding conventions will work within them, as well as all of the things that we described previously. This makes programming your Arduino unit relatively easy.

The Arduino ‘language’ is actually a library with various different definitions and functions that are tailored to the Arduino.

This is how the most basic of Arduino programs look, in terms of structure:

void setup()

{

// code within

}

void loop()

{

// code within

}

The setup function should be just after the declaration of variables at the start of your sketch. It will always be the first function that your Arduino unit runs, so pay careful attention. You must have your setup function, even if there is nothing within it.

You use the setup function to do things like initializing Arduino relevant variables, like your pin modes. In other words, any necessary setup that you have to do to get everything up and running, those functions should be called from within your setup function.

Do note that not necessarily all of your variables must be declared here. Variables should generally be declared within their primary function or in the global scope. If you fail to define a function at these levels, you won’t be able to use it where you want it to. For example, if you defined a variable within the setup function, you wouldn’t be able to use it within your loop function since it was defined within another function and isn’t global.

The loop function does pretty much exactly what it sounds like: it loops over and over until the program is brought to an end. When the program finally is brought to an end, the loop ends. Programs are generally brought to an end by cutting off power to your Arduino unit, so there isn’t much of a way to exit the Arduino loop function. This is the primary function of your program, and everything happens from here.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.