Variables in Arduino  What are they? + Structure and Functions ▷ 2020

Arduino is a electronic development and programming platform that allows to carry out different types of projects through a plate correctly installed, so it is necessary to know everything the programming environment of it. It is worth mentioning that this is the difficult part of all this, so programming knowledge will depend mainly on the programs that they have.

These arduino boards like any other programming language will have a lot of operators, functions, variables, among others, this entire field can become very extensive, that is why here we will refer mainly to the most important variables when programming, they may vary depending on the programming level that you want to obtain.

Taking into account that knowing each of these factors will help you in the use and management of this platform to the development of each of your projects, here we are going to show you that are the Arduino variables, what are they for and which are the most importantTo do this, follow in detail everything that we will teach you next in the post.

What are variables in the Arduino programming language and what are they for?

What are variables in the Arduino programming language and what are they for?

Variables are where you can store a data. Have name, a value, and a type. In the case of variable names, these can contain letters, numbers and symbols. They should start mostly with one letter, although they can also begin with “_” but this is not highly recommended as the criterion uses the library routines.

These names can also carry upper case and lower case. In C they are case-sensitive. As usual, variables always they go in lowercase and constants in uppercase, this being the most normal in these cases.

They use the same rules within the code for naming of the variables, either in lowercase but with words separated by underscores, as many as necessary so that they can improve their readability or use conversions “CapWords” (Words that start with a capital letter). However, the most common in these cases is to place the first word in lowercase.

The use of a single underscore as a prefix for non-public methods and instance variables. As for the names to avoid, it is important to never use the characters “L” the letter ele in lowercase and “O” the letter o in uppercase or “I” the letter i in uppercase as simple characters for variable names, as this can lead to cause confusion when reading the code.

Structure of a variable in Arduino What are all its parts and what does each one contain?

Structure of a variable in Arduino What are all its parts and what does each one contain?

As has been mentioned, the variables are one of the ways that one has to save a value, they have a name and are of only one type. These variables can mostly be used for many things such as checks, mathematical operations, among other.

In addition, it has a structure which we explain below:

Statement

Again it should be noted that the variables have a name, a value, and a type, with the assignment it is possible change the value of the variable if desired. All variables must be declared before use. Therefore, declarations must appear at the beginning of each function or statement block. When declaring a variable it will be necessary to indicate first the type of variable and then its name. Optionally, it is also possible to give it a value, which is called initializing the variable.

In the case of the declaration, it consists of a variable type and a list of variables provided by such as the following:

Int i,j;

Unsigned long longitud, contador;

Float x,pj;

Variables can be initialized in the declaration:

Unsigned long contador=0;

Float pi=3.1416;

It is also possible to use the const modifier to indicate that the variable cannot be changed at run time:

Const float e=2.7182

It’s important to put attention on the declaration of a variable it can only be done once in the program. However, the value of the variable can be changed at any time simply by using arithmetic and miscellaneous resignations.

Variables can be declared in a number of program locations and with respect to the place where said declaration is made. All this can determine in which part of the program it can be used, all this will be called variable scope or scope, which we will explain later.

In relation to C and C ++ they are said to be static typing languages, which means that the typing check is runs during compilation and not during execution. In this way you cannot change the type of a variable at run time. In the case of other generally interpreted languages, they are dynamically typed and the same variable can take on values ​​of different types at different times, such as python or PHP.

Ambit

Variables can become declared at the beginning of the program before the part of the setup () configuration, locally within functions, and sometimes, within a block, as for loops of type iF..for .. etc. According to the place of the declaration of the variable, the scope of application or the capacity of certain parts of a program to make use of it will be determined.

Global variables are those that can be seen and used by any function and testament of a program. As already mentioned, this type of variable must be declared at the beginning of the program just before the setup. In the same way, it must be remembered that when declaring a global variable, is in a permanent memory space within a static zone data and the abuse of global variables is an inefficient use of memory.

Local variables are those that are defined within a function or as part of a loop. They are only visible and can only be used within the role in which it was declared. In this way, it is possible to have two or more variables of the same name in different parts of the same program where they can reach contain distinct values. However, it should be mentioned that this practice is not very advisable because it makes reading the code difficult.

The modifier of the static variable It is used to design variables that are only visible within a function. However, unlike local variables that are created and destroyed each time the function is called, the static variables it is characterized by maintaining its values ​​between function calls.

Constants

In the programming area, a constant is a value that cannot be modified or altered during the execution of a program, so it must be maintained throughout the fixed time, in this way it can only be read. In the same way a constant corresponds to a fixed length of a reserved area in the main memory of the computer, where the program generally stores fixed values. As for example it can be the PI value = 3.1416.

In the case of const modifier, the behavior of a variable is modified by making it “Read-only”, this means that it can be used like any other variable, but in this case its value cannot be changed. In the environment of the arduino programming There are also predefined constants or expressions, which allow to facilitate code reads.

In C ++ constants can also be defined at the module level before compiling, so that they do not occupy memory and their name can be replaced by the value defined in the build process. Mostly these constants are written by names with capital letters and underscores separating words.

List of the main variables of the programming language for Arduino that you should know

List of the main variables of the programming language for Arduino that you should know

Currently you can find different programming language variables for Arduino.

In the case of the data stored in the variables, it can be of different types, which we are going to show you below:

  • Char: They are used mainly to store characters, these can be letters and have to go “” or ´´, char letter = “a”; , Letters Char = “2”.
  • Byte: In the case of bytes, numbers between 0 and 255 can be stored and it has a numerical range of 8 bits.
  • Int: These occupy 2 bytes (16 bits) and therefore, they store numbers between 2x -15 and 2x 15 – 1, that is, between -32,768 and 32,767.
  • Long: It occupies 32 bits (to bytes) and has a range that goes from -2,147,483,683 to 2,147,483,647.
  • Unsigned int: This also occupies 2 bytes, but since it does not have a sign, it can values ​​between 0 and 2×16 -1, that is, between 0 and 65,535.
  • Double: It is also characterized by storing decimal numbers, but they have 8 bytes (64 bit).
  • Float: They are decimal numbers that occupy 32 bits, that is, 4 bytes, these can take values ​​between -3.4028235E + 38 and + 3.4028235E + 38.

You must bear in mind that whenever a type of data is chosen, the one that needs the least size and that covers the required needs, since these will occupy Arduino board memory space and could cause the program requires more memory than you already have available.

If you have any questions, leave them in the comments, we will answer you as soon as possible, and it will surely be of great help to more members of the community. Thank you! 😉

You may be interested:

Rate this post

Leave a Comment