[EN] Arduino: data types

This article discusses variables, data types and constants for use with Arduino, the basis of programming. This is because programming has principles, as Niklaus Wirth has said since 1976 in his book that

Algorithms + Data Structures = Programs

A program is a problem-solving algorithm that processes data, which is the main reason for the difference between computation and program.

Variable

Variables are writable and erasable memory, or RAM, reserved for storing data of a specified size and referenced by the default location of the memory used to store data. But programming languages often ​​allow programmers to name them for use in place of the address values ​​for the convenience of writing code. The value of variables can be changed while the program is running or while the variable is on runtime.

In programming with subprograms, the programmer can create external variables or variables created outside the subprogram that can be accessed from the main and subprograms with internal variables or variables that are only visible within the subprogram. Variables created in a subprogram are deactivated when that subprogram terminates to save memory.

In addition, C++ allows variables to be declared inside { and } symbols as variables within those marks and the generated variable is deactivated when the program finished in the range of { and }.

Constant

Constant is a name for any given value without modifying the value declared when the constant is created while the program is running. In order for programmers to be able to change the constants without having to modify the program, for example, programmers use 3.14 to represent pi within the program, and when the precision is changed to 3.1415926, the programmer must edit all the previous values(3.14) ​​to the new values. But if the programmer declares a constant MyPi with a value of 3.14 and uses the name MyPi every time instead of typing the number 3.14, then when changing the MyPi declaration from 3.14 to 3.1415926, the program code written as MyPi ‘s value will change to 3.1415926 automatically. Therefore, the use of constants is useful for the ease of reading the code and the convenience of changing values ​​in a range of constant declarations without having to edit the code at every location where that value is used.

Data type

Data type defines how memory is used by programming languages. For example, a 1-byte memory called char is used to store 8-bit integers with the value -,0,+ or assign one byte of memory is called a byte, which stores a byte or 8 bits of the integer with values 0 to +, etc.

Arduino uses C++ as a programming language. Thus, the basic data types of C++ are the same, but Arduino is used with different microcontrollers, such as a different size processor, different amounts of RAM, etc. When programming with the microprocessor of the Arduino Uno and ESP8266 boards are different because one is an 8-bit processor while the other is 32-bit, and the amount of memory of the Arduino Uno using ATmega328P has only 2KB of RAM while the ESP8266 has 80KB of user RAM. In program development you must consider memory usage or appropriate variables sized for the type of data. And when coded, it can be used with ESP8266, but when used with the ATmega328P, there may be problems with insufficient memory.

The basic types of information covered in this article are:

  • boolean 8 bits, true or false
  • char 8 bits in size; stores a signed integer value. Therefore, values can be stored in the range -128 to 127.
  • unsigned char มี8 bits in size; stores an integer value without a minus sign. Therefore, values can be stored in the range 0 to 255.
  • byte 8 bits in size; stores an integer value without a minus sign. Therefore, values can be stored in the range 0 to 255.
  • int 16 bits in size and stores a signed integer value. Therefore, values can be stored in the range -32,768 to 32,767.
  • unsigned int 16 bits in size and stores an integer value without a minus sign. Therefore, values can be stored in the range 0 to 65,535.
  • word 6 bits in size and stores a signed integer value. Therefore, values can be stored in the range -32,768 to 32,767.
  • unsigned word มีขนาด16 บิต เก็บค่าตัวเลขจำนวนเต็มแบบไม่มีเครื่องหมายลบ จึงสามารถเก็บค่าได้ในช่วง 0 ถึง 65,535
  • long 32 bits in size; stores a signed integer value. Therefore, values can be stored in the range -2,147,483,648 to 2,147,483,647
  • unsigned long 32 bits in size, holds integer values without minus signs. Therefore, values can be stored in the range 0 to 4,294,967,295.
  • float 32-bit size, stores single precision floating-point values in the range -3.4028235E38 to 3.4028235E38.
  • double 64-bit size, stores double precision floating-point values in the range -3.4028235E38 to 3.4028235E38.

Naming and declaring variables/constants

Naming variables and constants have the following naming rules:

  • Must not be the same with reserved word.
  • Must not be the same with the name of the location in the same scope of use. But if the name is the same but one is outside { } and another is inside can be named the same. Referring to external names is preceded by a :: , but it is not recommended to give the same name to avoid confusion.
  • Beginning with _ or letters.
  • You can add _ or numbers to names
  • Uppercase and lowercase letters have different meanings.

Defining value

The defining rules are in the following format.

L = R;

Where
L คือ variable name
R คือ expression

The operation of the value declaration is done on the right or R until the result is obtained. After that, the result is stored in a variable on the L side, which is the specified RAM.

Variable declaration

The format of the variable declaration is as follows.

type name;
type name1, name2, … , nameN;

Example of declaring a variable named voltage to store voltage values as decimal data by setting the voltage value to 3.3 can be written as follows.

float voltage;

void setup() {
  voltage = 3.3;
}

void loop() {
}

or

float voltage = 3.3;

void setup() {
}

void loop() {
}

Constant declaration

The format of the constant declaration is as follows.

const type nam = value;

An example of declaring a constant named voltage to store voltage values as decimal data by setting the voltage value to 3.3 can be written as follows.

const float voltage = 3.3;

void setup() {
}

void loop() {
}

Data structure

The data structure is a form of organizing and storing data that allows for efficient access and modification more precisely, a data structure is a collection of data values, relationships between them, and functions or operations that can be applied to data.

From the definition of a program, we need to create a variable suitable for use with our algorithm in order to solve the desired problem. If the problem to be solved is complex, the solution must be divided into several parts or requires a multi-step algorithm. This means that each step must contain a group of variables or data structures associated with the solution process.

Array

Arrays are memory reservations for storing the same type of data under the same variable name and referenced by the ordinal values of the data set, which means

  • Each element in an array variable must be of the same type.
  • An exact number of n members must be specified.
  • Each member is arranged in sequence from 0, 1, 2, …, n-1.
  • The amount of memory must be sufficient to store all data. If this is not enough, a memory reservation error will occur, preventing the program from running.

An array variable declaration has the following format:

type name[number_of_variable];

The order to be accessed must be an integer in the range 0 to (n-1). Access to members is possible with the following access patterns:

name[index]

Assigning values to the i-element where i must be in the range 0 to (n-1) can be done in the following format.

name[ i ] = value ;

If you want to assign values to an array since a variable is declared, it can be written in the following format.

type name[n] = {data0, data1, … , datan-1 };

or

type name[] = {data0, data1, … , datan-1 }

Example

Example of reading data from the temperature sensor 5 times assuming each time the temperature value is 35, 34, 35, 38 and 39. Calculate the average temperature. When written in a normal way that is not an array variable, it can be written as follows:

void setup() {
  Serial.begin(115200);
  // Input (data)
  byte temp0, temp1, temp2, temp3, temp4;
  float avgTemp, sumTemp;
  temp0 = 35;
  temp1 = 34;
  temp2 = 35;
  temp3 = 38;
  temp4 = 39;
  // Process (algorithm)
  sumTemp = temp0+temp1+temp2+temp3+temp4;
  avgTemp = sumTemp / 5.0;
  // Output
  Serial.print("Temperature:");
  Serial.print(temp0);
  Serial.print(",");
  Serial.print(temp1);
  Serial.print(",");
  Serial.print(temp2);
  Serial.print(",");
  Serial.print(temp3);
  Serial.print(",");
  Serial.println(temp4);
  Serial.print("Sum of temperature:");
  Serial.println(sumTemp);
  Serial.print("Average of temperature:");
  Serial.println(avgTemp);
}

void loop() {
}

The case of using array variables can be written as follows:

void setup() {
  Serial.begin(115200);
  // Input (data)
  byte temp[] = {35,34,35,38,39};
  float avgTemp, sumTemp;
  // Process (algorithm)
  sumTemp = temp[0]+temp[1]+temp[2]+temp[3]+temp[4];
  avgTemp = sumTemp / 5.0;
  // Output
  Serial.print("Temperature:");
  Serial.print(temp[0]);
  Serial.print(",");
  Serial.print(temp[1]);
  Serial.print(",");
  Serial.print(temp[2]);
  Serial.print(",");
  Serial.print(temp[3]);
  Serial.print(",");
  Serial.println(temp[4]);
  Serial.print("Sum of temperature:");
  Serial.println(sumTemp);
  Serial.print("Average of temperature:");
  Serial.println(avgTemp);
}

void loop() {
}

From both programs, the results of the program are as follows.

Conclusion

From this article, readers have learned about variables, constants, and data types as well as methods for declaring variables/constants including data structures for use with Arduino which is the basis for reference in C++ programming in the next article. Finally, we hope that you will still enjoy programming.

References

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-09-25