Each variable has its own data type that determines its place in the memory of the microcontroller and also the way how it can be used. There are plenty of different data types. Further will be viewed the most used ones:

- numeric type of 8 bits that stores numbers from 0 to 255.*byte*

byte exampleVariable = 123;

- the whole number of 16 bits that can contain values from -32 767 to 32 768.*int*

int exampleVariable = 12300;

- a data type for real numbers that uses 32 bits and store numbers approximately from -3.4E38 to 3.4E38.*float*

float exampleVariable = 12300.546;

- a set of same data type that can be accessed using serial number or index. The index of the first element is always 0. The values of massive can be initialized at the definition of to do it during the execution of the program. In the following example the massive with the name “*massive**firstMassive*” and data type*int*has been created. The value of the massive with the index 0 will be*12*and the value with the index 3 is*15*.

int firstMassive[] = {12,-3,8,15};

Square brackets of the massive can be used to access some value in the massive by index. In the following example the element with the index 1 (that is *-3*) is assigned to the *secondVariable* variable.

int secondVariable = firstMassive[1];

Massive can be easily processed in the cycle. The next example shows how to store automatically the necessary values from the analogue signal input to the previously defined massive.

for(int i = 0; i < 4; i = i + 1){ //the cycle that repeats 4 times exampleMassive[i] = analogRead(2); //reads value from the pin 2 and stores it in the //exampleMassive//. }

This cycle in the example starts with the index 0 (i=0) and it increases by 1 while it is smaller than 4 (not including). That means in the last cycle the index value will be 3, because when the i equals 4, the inequality i<4 is not true and the cycle stops working.

- the variables of this data type can take values*bool**true*or*false*. Arduino environment allows following values to these variables:**TRUE**,**FALSE**,**HIGH**(logical 1 (+5V)) and**LOW**(logical 0 (0V)).

Data type conversion can be done using multiple techniques - casting or data type conversion using specific functions.

- cast operator translates one data type into another straightforward. The desired type of variable should be written in the brackets before the variable data type of which needs to be changed. In the following example where the variable type is changed from float to int, the value is not rounded but truncated. Casting can be done to any variable type.**Casting**

int i; float f=4.7; i = (int) f; // now i is 4

-**Converting***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

- function**Converting String to float***toFLoat()*converts*String*type of variable to the*float*. In the following example is shown the use of this function. In case 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

- function**Converting String to Int***toInt()*converts*String*type of variable to the*Int*. In the following example is shown the use of this function.

String string = "123fkm"; int i = string.toInt(); //the result will be 123

Arithmetic operations on Arduino are used to do mathematical calculations with the numbers or numerical variables. The arithmetic operators are following:

- one of the four primary arithmetic operations that are used to add numbers. Addition operator can be used to add only numbers, only numeric variables or the mix of both. The following example shows the use of addition operator.**Addition (+)**

int result = 1 + 2; //the result of the addition operation will be 3

- the operation that subtracts one number from another where result is the difference between these numbers.**Subtraction (-)**

int result = 3 - 2; //the result of the subtraction operation will be 1

- the operation that multiplies numbers and gives the result.**Multiplication (*)**

int result = 2 * 3; //the result of the multiplication operation will be 6

- the operation that divides one number by another. If the result variable has the**Division (/)***integer*type, the result will always be the whole part of the division result without the fraction behind it. If the precise division is necessary, it is important to use*float*type of variable for this purpose.

int result = 7 / 2; //the result of the division operation will be 3 (only the whole part of the division result) float result2 = 7.0 / 2.0; //the result of the division operation will be 3.5

- the operation that finds the remainder of the division of two numbers.**Modulo (%)**

int result = 7 % 3; //the result of the modulo operation will be 1, because if 7 is divided by 3, the remaining is 1

- the operator that assigns the value on the right to the variable that is on the left of the assignment operator. The work of an assignment operator can be seen in any of the previously viewed operation examples.**Assignment operator (**

Compound operators in Arduino are a short way of writing down the arithmetic operations with variables. All of these operations are done on integer variables. These operands are often used in the loops, when it is necessary to manipulate with the same variable in each iteration of the cycle. The compound operators are following:

- increases the value of integer variable by one.*Increment (++)*

int a = 5; a++; //the operation a=a+1; the result will be 6

- decreases the value of integer variable by one.*Decrement (- -)*

int a = 5; a--; //the operation a=a-1; the result will be 4

- adds the right operand to the left operand and assigns the result to the left operand.*Compound addition (+*

int a = 5; a+=2; //the operation a=a+2; the result will be 7

- subtracts the right operand from the left operand and assigns the result to the left operand.*Compound subtraction (-*

int a = 5; a-+3; //the operation a=a-3; the result will be 2

- multiplies the left operand by the right operand and assigns the result to the left operand.*Compound multiplication (**

int a = 5; a*=3; //the operation a=a*3; the result will be 15

- divides the left operand with the right operand and assigns the result to the left operand.*Compound division (/*

int a = 6; a/=3; //the operation a=a/3; the result will be 2

- takes modulus using two operands and assigns the result to the left operand.*Compound modulo (%*

int a = 5; a%=2; //the result will be the remaining part of the operation a/2; it results in 1

- bitwise OR operator that assigns the value to the operand on the left.*Compound bitwise OR (|*

int a = 5; a|=2; //the operation a=a|2; the result will be 7

- bitwise AND operator that assigns the value to the operand on the left.*Compound bitwise AND (&*

int a = 6; a&=; //the operation a=a&2; the result will be 2

**Check yourself**

1. What data tyoe is used for a variable range 0…255?

2. What data type should be used for more precise measurements?

3. Make data type conversion char to string.

4. What is the main advantage of using compound operators?