Arduino is an electronic development and programming platform that allows different types of projects to be carried out through a correctly installed board, so it is necessary to be able to know the entire programming environment of the same. It is worth mentioning that this is the difficult part of all this, so programming knowledge will depend mainly on the programs you have.
These Arduino boards like any other programming language will have a large number 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 at the time of programming, they may vary depending on the level of programming 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 for the development of each of your projects, here we are going to show you what the Arduino variables are, what they are for and which are the most important. To do this, follow in detail everything that we will teach you next in the post.
What are Arduino programming language variables and what are they for?
Variables are the place where data can be stored . It has a name, a value, and a type. In the case of variable names, these can contain letters, numbers and symbols . They should start mostly with a letter, although they can also start with “_” but this is not highly recommended since the criteria uses the library routines.
These names can also be case-sensitive . In C they are case sensitive. In general, variables are always in lower case and constants in upper case, this being the most normal in these cases.
They use the same rules within the code for naming variables, either in lowercase but with words separated by underscores, as many as necessary so that they can improve their readability or use “CapWords” conversions (Words that start capitalized). 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 never to use the characters “L” the letter ele in lowercase and the “O” the letter o in uppercase or “I” the letter i in uppercase as simple characters for variable names, since This can lead to confusion when reading the code.
Structure of a variable in Arduino What are all its parts and what does each one contain?
As has been mentioned, variables are one of the ways 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 others.
In addition, it has a structure which we explain below:
Again it should be noted that the variables have a name, a value and a type, with the assignment it is possible to 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 first indicate 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 type of variable and a list of variables provided by such as the following:
Int i, j; Unsigned long length, counter; Float x, pj;
Variables can be initialized in declaration:
Unsigned long counter = 0; Float pi = 3.1416;
It is also possible to use the const modifier to indicate that the variable cannot be changed at runtime:
Const float e = 2.7182
It is important to note that the declaration of a variable 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 various resignations .
Variables can be declared in a series of places in the program and with respect to the place where said declaration is carried out. All this can determine in which part of the program it can be used, all this is 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 executed during compilation and not during execution . 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 .
Variables can be declared at the beginning of the program before the setup () part of the 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 setup. In the same way, it must be remembered that when declaring a global variable, it is in a permanent memory space within a static data zone and the abuse of global variables implies 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 function 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 contain different values . However, it should be mentioned that this practice is not very advisable because it makes it difficult to read the code.
The static variable modifier 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, static variables are characterized by maintaining their values between function calls.
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 kept fixed all the 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 value PI = 3.1416.
In the case of the 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 Arduino programming environment there are also predefined constants or expressions, which allow to facilitate the reading of the code.
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 compilation 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
Currently you can find different variables of the programming language for Arduino .
In the case of the data that is stored in the variables, it can be of different types, which we are going to show you below:
- Char: They are mainly used 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 has a numerical range of 8 bits.
- Int: These occupy 2 bytes (16 bits) and therefore, they store number 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 have 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: These 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 data type is chosen, the one that needs the least size and that meets the required needs should be chosen , since these will occupy space in the memory of the Arduino board and could cause the program to require more memory. than you already have available.