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:
byte exampleVariable = 123;
int exampleVariable = 12300;
float exampleVariable = 12300.546;
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.
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 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
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
int result = 7 % 3; //the result of the modulo operation will be 1, because if 7 is divided by 3, the remaining is 1
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:
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; a%=2; //the result will be the remaining part of the operation a/2; it results in 1
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 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?