Javatpoint Logo
Javatpoint Logo

Arduino Variables

The variables are defined as the place to store the data and values. It consists of a name, value, and type.

The variables can belong to any data type such as int, float, char, etc. Consider the url - Arduino data types for detailed information.

Consider the below example:


Arduino Variables

Here, the int data type is used to create a variable named pin that stores the value 8. It also means that value 8 is initialized to the variable pin.

We can modify the name of the variable according to our choice. For example,

The above example can also be written as:

Here, the variable name is LEDpin.

We can refer the declared variable further in our program or code.

For example,

Here, the value stored (8) in the declared variable (LEDpin) will be passed to the pinMode() function.

If we have not declared the variable, the value can also be directly passed to the function.

For example:

Advantages of Variables

The advantages of the variables are listed below:

  • We can use a variable many times in a program.
  • The variables can represent integers, strings, characters, etc.
  • It increases the flexibility of the program.
  • We can easily modify the variables. For example, if we want to change the value of variable LEDpin from 8 to 13, we need to change the only point in the code.
  • We can specify any name for a variable. For example, greenpin, bluePIN, REDpin, etc.

How can we change the value of a variable in Arduino?

The value of a variable can be changed using the assignment operator ( equal = sign). But we need to declare a variable before assigning the value.

If we directly specify the value as:

We will get an error that pin has not declared.

We can easily change the variables by copying its value to another variable.

For example,

The LEDpin now contains the value 13 instead of 7. But, value of pin1 is still 7.

Let's understand with an example.

Consider the below code.

Output:

Arduino Variables

We can notice in the output that the value of LEDpin is changed from 7 to 13. Similarly, we can change the values of variables.

Variables Scope

It means that in how many ways the variables can be declared.

The variables can be declared in two ways in Arduino, which are listed below:

  • Local variables
  • Global variables

Local Variables

The local variables are declared within the function. The variables have scope only within the function. These variables can be used only by the statements that lie within that function.

For example,

Global Variables

The global variables can be accessed anywhere in the program. The global variable is declared outside the setup() and loop() function.

For example,

Consider the below code.

We can notice that the LEDpin is used both in the loop() and setup() functions.

The value is used in both functions, so, changing the value in one function will reflect in the other. For example,

Here, the value 13 will be passed to the digitalWrite() function.


Next TopicArduino Constants





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA