In the previous topic (Analog Output), you must have noticed that we have used number 3 (pin number) and 500 (delay value) a lot. But what if we have to change the pin number or the delay in improving the code?
You have to change the value in each of them. This is very tedious and time-consuming. But there is an easy way, using variables.
A variable is used to store a value or an information so that we can refer or/and manipulate it at a later stage during the life of the Arduino sketch. Memory is set aside for storing the variable and the variable is given a name which allows us to access it in the sketch.
So, we have to make two variables for pin number and delay. This is how it looks once you modify the code:
Let us learn more about variables.
Below are some variable types that are frequently used in Arduino sketches:
Datatype | Size in Bytes | Description |
---|---|---|
char | 1 Byte | It stores 8 bit numerical ASCII value of characters like alphabets, symbols etc. It can also store a signed number that is in range of -128 to 127. Character literals are written in single quotes like 'a', '#' etc and their ASCII numerical is stored at corresponding variable location. |
unsigned char | 1 Byte | It can store 8 bit numerical ASCII values of characters, symbols etc and can also store any unsigned number in range of 0 to 255. Character literals are written in single quotes like 'a', '#' etc and their ASCII numerical is stored at corresponding variable location. |
int | 2 Bytes | Stores a 2 byte(16 bits) signed integer value that is in range of -32,768 to 32,767. |
unsigned int | 2 Bytes | Stores an unsigned integer value of 2 bytes(16 bits) that is in range of 0 to 65,536. |
long | 4 Bytes | Stores a 4 byte (32 bit) signed integer value that is in range of -2,147,483,648 to 2,147,483,647. |
unsigned long | 4 Bytes | Stores an unsigned 4 byte(32 bit) integer that is in range of 0 to 4,294,967,295 (2^32 - 1). |
float | 4 Bytes | Stores a signed 4 byte(32-bit) value that is integer or a value with decimal point (say 12.15) that is in range of -3.4028235E+38 to 3.4028235E+38. |
double | 4 Bytes | Same as float for evive. |
The standard form of variable definition is:
Variable_Datatype Varible_Name;
Variable_Datatype can be int or float depending on the type of variable you want. Variable_Name is the name of variable. Variable is referenced or used by its name later in the program.
By giving the variable a type and name, space is made available in memory for this variable.
E.g. if you want to count the number of times the main loop is executed, you must first define a variable count as shown below:
int count;
You can give a variable any name as long as it sticks to the rules set out below. It is best to give variables meaningful names that help you and others understand the sketch better.
You can assign a value to a variable with the following statement:
Variable_Name = Expression;
Expression will give a valid number that will be assigned to the variable. Let’s count how many times the loop is executed. Below is the Arduino sketch:
Let’s make a new sketch, where we’ll add two float variable to a new variable. Below is the code:
When the value of the variable is sent to the serial monitor window, println() automatically rounds the number off to two decimal places, i.e. number of digits after the decimal.
The second time that println() is used to send the value of the variable to the serial monitor window, the number of decimal places is specified as 5. This is done by passing a second parameter value of 5 to the println() function.