Almost every computer program manipulates the data. Data representation in the program is variable. In C/C++, the variable needs to be defined before using it, giving it some name and assigning a chosen type, dependent on the kind of data. Some common data types and how to use variables are shown below.
Data type specifies how it is encoded and represented in the computer memory. For example, integer numbers are binary-encoded, the texts are represented as a series of ASCII-encoded characters, and real numbers have a particular encoding scheme that consists of two binary numbers - mantissa and exponent. Other data types are tables that consist of elements of the same type or structures with elements of different types. There are plenty of different data types; some of them are predefined, but user-own types can be defined based on existing ones. Creating a variable requires specifying its type, which determines its place in the memory of the microcontroller and also the way it can be used. Further, the most used ones together with examples of defining variables.
byte
– a numeric type of 8 bits that stores numbers from 0 to 255.byte exampleVariable;
The example above defines a variable, reserves its memory, and assigns the memory address to the variable name. It is also possible to give the variable the initial value as below:
byte exampleVariable = 123;
int
– the integer number. Its size depends on the microcontroller class. In the case of AVR (Arduino), it consists of 16 bits that can contain values from –32 767 to 32 768. In ARM-based microcontrollers (like STM32), its size is 32 bits.int exampleVariable = 12300;
float
– a data type for real numbers that uses 32 bits and stores numbers approximately from –3.4 × 10^38 to 3.4 × 10^38.float exampleVariable = 12300.546;
array
– a set of data of the same type that can be accessed using a serial number or index. The index of the first element is always 0. The values of an array can be initialized at the definition of it or set during the program's execution. In the following example, the array of four elements with the name “first array” and data type int
has been created. The value of the array with an index of 0 will be 12, and the value with an index of 3 will be 15.int firstArray[] = {12,-3,8,15};
Square brackets of the array can be used to access some value in the array by index. In the following example, the element with index 1 (that is –3) is assigned to the secondVariable
variable.
int secondVariable = firstArray[1];
An array can be quickly processed in the loop. The following example shows how to calculate the sum of all elements from the previously defined array (for
statement will be explained in detail in the following chapters).
//The loop that repeats 4 times int sum = 0; for(int i = 0; i < 4; i = i + 1){ sum = sum + firstArray[i]; }
The loop in the example starts with index 0 (i
= 0) and increases it by 1 while smaller than 4 (not including). That means the index value will be 3 in the last cycle because when the i
equals 4, the inequality i
< 4 is not true, and the loop stops working.
bool
– the variables of this data type can take values TRUE
or FALSE
. Arduino environment allows the following values to these variables: TRUE
, FALSE
, HIGH
(logical 1 (+5 V)) and LOW
(logical 0 (0 V)).Data type conversion can be done using multiple techniques – casting or data type conversion using specific functions.
int i; float f=4.7; i = (int) f; //Now it is 4
byte(), char(), int(), long(), word(), float()
functions are used to convert any type of variable to the specified data type.int i = int(123.45); //The result will be 123
String
to float
– function toFLoat()
converts String
type of variable to the float
. The following example shows the use of this function. If the value cannot be converted because the String doesn't start with a digit, the returned value will be 0.String string = "123fkm"; float f = string.toFLoat(); //The result will be 123.00
String
to Int
– function toInt()
converts String
type of variable to the Int
. In the following example, the use of this function is shown.String string = "123fkm"; int i = string.toInt(); //The result will be 123
Typedef Specifier
A typedef
specifier can give another name for existing types or declare a new one. Renaming types is possible, but software development frameworks already have several aliases. It is helpful, however, when combined with enumerations, classes and structures to give them reasonable names and re-use them later in the code to improve their readability. We present more details on structures in the chapter Structures and Classes, but here is an example presenting a reasonable use of the typedef
specifier.
typedef struct {int x; int y;} tWaypoint; //Declare complex type named waypoint ... //Declare a variable of the type of tWaypoint tWaypoint wp1;
Enum Declaration
Enumerations are helpful to give meaning to the integer values and present some logic in a code instead of putting numbers into it. It can be, e.g., the device's state, error code, etc.
In the case a new enumeration is needed, it is possible to declare one using the enum
keyword and specifying a list:
enum errorcodes {ER_OK, ER_DOWNLOAD, ER_UPLOAD, ER_NOWIFI}; //define enumeration ... errorcodes Errorcode; //declare a variable ... Errorcode = ER_DOWNLOAD; //assign a value
The default numbering starts with 0 (ER_OK=0
) and increases by 1 with every next item on the enumeration list. However, explicitly defining values represented by the item labels is possible.
enum errorcodes {ER_OK=0, ER_DOWNLOAD=3, ER_UPLOAD=4, ER_NOWIFI=1};