Each variable has its 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.
byte exampleVariable = 123;
int exampleVariable = 12300;
float exampleVariable = 12300.546;
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 easily processed in the cycle. The next example shows how to store the necessary values automatically from the analogue signal input to the previously defined array.
//The cycle that repeats 4 times for(int i = 0; i < 4; i = i + 1){ //Reads value from the pin 2 and stores it in the //exampleArray//. exampleArray[i] = analogRead(2); }
This cycle in the example starts with 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.
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 i is 4
int i = int(123.45); //The result will be 123
String string = "123fkm"; float f = string.toFLoat(); //The result will be 123.00
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 the following.
int result = 1 + 2; //The result of the addition operation will be 3
int result = 3 - 2; //The result of the subtraction operation will be 1
int result = 2 * 3; //The result of the multiplication operation will be 6
//The result of the division operation will be 3 //(Only the whole part of the division result) int result = 7 / 2; //The result of the division operation will be 3.5 float result2 = 7.0 / 2.0;
//The result of the modulo operation will be 1, //Because if 7 is divided by 3, the remaining is 1 int result = 7 % 3;
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 the following.
int a = 5; a++; //The operation a = a + 1; the result will be 6
int a = 5; a--; //The operation a = a – 1; the result will be 4
int a = 5; a+=2; //The operation a = a + 2; the result will be 7
int a = 5; a-+3; //The operation a = a – 3; the result will be 2
int a = 5; a*=3; //The operation a = a × 3; the result will be 15
int a = 6; a/=3; //The operation a = a / 3; the result will be 2
int a = 5; //The result will be the remaining //Part of the operation a/2; it results in 1 a%=2;
int a = 5; a|=2; //The operation a=a|2; the result will be 7
int a = 6; a&=; //The operation a=a&2; the result will be 2
Check Yourself
1. What is the data type used for a variable range 0…255?
2. What should data type be used for more precise measurements?
3. Make data type conversion char to string.
4. What is the main advantage of using compound operators?