Data Types and Variables

Data Types

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 – 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.4 × 10^38 to 3.4 × 10^38.
float exampleVariable = 12300.546;
  • array – a set of the same data type that can be accessed using serial number or index. The index of the first element is always 0. The values of an array can be initialised at the definition of to do it during the execution of the program. In the following example the array with the name “first array” and data type int has been created. The value of the array with the index 0 will be 12, and the value with the index 3 is 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 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.

  • 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 (+5 V)) and LOW (logical 0 (0 V)).
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
  • Convertingbyte(), 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 the 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 the 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.
//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; 
  • Modulo (%) – the operation that finds the remainder of the division of two numbers.
//The result of the modulo operation will be 1, 
//Because if 7 is divided by 3, the remaining is 1
int result = 7 % 3; 
  • 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 the 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;
//The result will be the remaining 
//Part of the operation a/2; it results in 1
a%=2; 
  • 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 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?

en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/arduino_and_arduino_101_intel_curie/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