Data types and variables

Data types

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 - numeric type of 8 bits that stores numbers from 0 to 255.
byte exampleVariable = 123;
  • int - the whole number of 16 bits that can contain values from -32 767 to 32 768.
int exampleVariable = 12300;
  • float - a data type for real numbers that uses 32 bits and store numbers approximately from -3.4E38 to 3.4E38.
float exampleVariable = 12300.546;
  • massive - 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 “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.

  • bool - the variables of this data type can take values 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

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

  • Casting - 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.
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
  • Converting String to float - function 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
  • Converting String to Int - function 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 operators

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

  • Addition (+) - 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.
int result = 1 + 2; //the result of the addition operation will be 3
  • Subtraction (-) - the operation that subtracts one number from another where result is the difference between these numbers.
int result = 3 - 2; //the result of the subtraction operation will be 1
  • Multiplication (*) - the operation that multiplies numbers and gives the result.
int result = 2 * 3; //the result of the multiplication operation will be 6
  • Division (/) - the operation that divides one number by another. If the result variable has the 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
  • Modulo (%) - the operation that finds the remainder of the division of two numbers.
int result = 7 % 3; //the result of the modulo operation will be 1, because if 7 is divided by 3, the remaining is 1
  • Assignment operator (=) - 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.

Compound operators

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:

  • Increment (++) - increases the value of integer variable by one.
int a = 5;
a++; //the operation a=a+1; the result will be 6
  • Decrement (- -) - decreases the value of integer variable by one.
int a = 5;
a--; //the operation a=a-1; the result will be 4
  • Compound addition (+=) - adds the right operand to the left operand and assigns the result to the left operand.
int a = 5;
a+=2; //the operation a=a+2; the result will be 7
  • Compound subtraction (-=) - subtracts the right operand from the left operand and assigns the result to the left operand.
int a = 5;
a-+3; //the operation a=a-3; the result will be 2
  • Compound multiplication (*=) - multiplies the left operand by the right operand and assigns the result to the left operand.
int a = 5;
a*=3; //the operation a=a*3; the result will be 15
  • Compound division (/=) - divides the left operand with the right operand and assigns the result to the left operand.
int a = 6;
a/=3; //the operation a=a/3; the result will be 2
  • Compound modulo (%=) - takes modulus using two operands and assigns the result to the left operand.
int a = 5;
a%=2; //the result will be the remaining part of the operation a/2; it results in 1
  • Compound bitwise OR (|=) - bitwise OR operator that assigns the value to the operand on the left.
int a = 5;
a|=2; //the operation a=a|2; the result will be 7
  • Compound bitwise AND (&=) - bitwise AND operator that assigns the value to the operand on the left.
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?

en/iot-open/programming_fundamentals_rtu/data_types_and_variable.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0