Arduino – Data types and variables

Data types are used to declare different variables or functions. The type of variable determines how much space it takes up in memory and how to handle that group of bits.

The variables are introduced according to the following syntax.

type variableName = value;

int ledPin = 3;

The constants defined in the arduino program do not occupy any program memory. The compiler replaces references to these constants with the value specified at the time of translation. These constants are introduced with the #define keyword.

#define constantName value

#define ledPin 3

Const is a variable qualifying keyword, that modifies the behavior of the variable so that the variable becomes “read-only”. This means that the variable can be used in the same way as any other type of variable, but its value cannot be changed. Constants defined with the const keyword follow the rules of the scope of the variable. Therefore, it is better to use it than #define.

const char ledPin = 3;

Variables declared in a function or code block are local variables. It can only be used by statements that are in the function or code block and cannot be accessed from the outside.

In the following example, we declare a variable called “myLocalVariable” inside the “sum” function. This variable can only be used inside the “sum” function. These are called local variables.

int sum(int num_1, int nun_2)
{
  int myLocalVariable = 0;

  myLocalVariable = num_1 + nun_2;
  return myLocalVariable;
}

Global variables are defined outside of all functions, usually at the top of the program. The global variable can be accessed with any function. That is, a global variable can be used throughout the program after it has been declared.

In the following example, a variable named “myGlobalVariable” was declared at the beginning of the program, outside the functions. The variable created in this way can be accessed from anywhere, which is why they are called global variables.

int myGlobalVariable = 1;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.Print(myGlobalVariable);
}

So far, so much for creating variables and their scope. Let’s see the data types.

void

The void keyword is only used to declare functions. The void indicates that the function is not expected to return information (no return value) for the function from which it was called. We can also write our own function with it, which we just call elsewhere in the code. One of the most important functions of our sketch is void:

void setup()
{

}

bool

It can have two values: true or false. Each bool variable occupies one byte in memory.

bool value = false;
bool buttonValue = true; 

boolean

Boolean is not a standard type. Use the bool data type instead.

char

This data type occupies one byte of memory and is capable of storing a character value, so it stores a value of 1 character. Letters are enclosed in apostrophe ‘A’, character strings in double quotation marks ‘abc’. It is a signed data type, which means that you can store food between -128 and 127.

char value = 'A';
char value = "abc";

The characters are stored as numbers, the decimal value of the letters can be retrieved in the ASCII character standard. The value of “A” + 1 is 66, because the value of the uppercase ASCII of A is 65. You can use the Serial.println () command to output it to the serial monitor in other number systems.

char value = 'A';

Serial.println(value);
Serial.println(value, DEC);
Serial.println(value, HEX);
Serial.println(value, OCT);
Serial.println(value, BIN);

unsigned char

Same as byte data type. It takes up one byte of memory. Numbers from 0 to 255 can be stored in it. If possible, use the byte instead.

unsigned char myChar = 160;

byte

Stores unsigned numbers in 1 byte, 0 to 255. 

byte myByte = 25;

int

Primary data type for storing numbers. On ATmega-based tables (eg Arduino Uno), the integer stores 16 bits (2 bytes) between -32,768 and 32,767. They work with arithmetic operators in the traditional way, but you can also use bitwise operators with them.

int myInt = 26;

int a = 5;        // binary: 0000000000000101
int b = a << 14;  // binary: 0100000000000000

unsigned int

Suitable for storing unsigned integers. Because it works in the same 2 bytes as the integer, it can store a larger positive value: between 0 and 65,535 for ATMega-based arduinos.

unsigned int var = 23;

word

Same as unsigned int. Stores unsigned numbers in 16 bits, from 0 to 65535.

word myWord = 25000;

long

Stores a signed numeric value between -2,147,483,648 and 2,147,483,647 in 32 bits (4 bytes). If you count with integers, then at least one of the values ​​must be long, or an integer constant followed by an L, or a long type of variable that forces it to be long.

long speedOfLight_km_s = 300000L;

unsigned long

Unsigned long variables are extended-sized variables for storing numbers and store 32 bits (4 bytes). Unlike regular long, unsigned long will not store negative numbers, so they fall in the range of 0 to 4,294,967,295 (2 ^ 32 – 1).

unsigned long time = millis();

short

16-bit data type for ATMega and ARM based Arduinos. Stored value between -32,768 and 32,767.

short myShort = 10;

float

Float tracks numbers with a decimal point. Floating point numbers can be up to 3.4028235E + 38 and up to -3.408235E + 38. They are stored as 32 bits (4 bytes) of information. The float data type has only 6-7 decimal digits. This is for all digits and not for decimal places.

Floating point numbers are not accurate and can produce strange results when compared. For example, 6.0 / 3.0 is not equal to 2.0. Instead, you need to check that the absolute value of the difference between the numbers is less than some small number. Converting from a floating point to an integer results in truncation:

float x = 2.9;
int y = x;

The result of the variable y will be two. To round, use the round () function:

float x = 2.9;
int y = round(x);

So the result will be 3.

double

Double-precision floating-point number. On Uno and other ATMEGA-based tables, this takes up 4 bytes.

double myDouble = 68.665;

string – char array

You can create a string with a string array. These can be declared according to the following examples, all of which are valid. The last element of the character arrays is 0 (zero), hence the compiler recognizes the end of the string. More about the char arrays here .

char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
'}; char Str4[ ] = "arduino"; char Str5[8] = "arduino"; char Str6[15] = "arduino";

String – object

You can manipulate text-based strings with it. Read more about this here .

array

An array is a memory location that stores the same type of elements. As a reference to a specific location or element in the array, enter the name of the array and the position number of that element in the array. More about the arrays here .

The next chapter will talk about operators.

advertising – ESP boards from amazon.com