Learn to code

ARDUINO & CODES

Previous Post
Blink a LED with Arduino
Next Post
Creating a password protected lock with a keypad, relay, and display for Arduino

Understanding global and local variables for Arduino

in Arduino Basics

In the previous tutorial, Arduino Tutorial: 2.1 Blink a LED, we made our first sketch.  with an external LED. We declared one constant variable at the top of the program for the output pin. This tutorial will give you insight into how variables can be used. We can distinguish two kinds of variables, the global variables, and the local variables. When we create a variable it is called “declaring”. If we give the variable a name it is called “naming”. 

Firstly, a global variable is one that can be seen by every function in the program. Secondly, local variables are only visible to the function in which they are declared. In the Arduino environment, any variable declared outside of a function (void setup(), loop(), void(), etc. ), is a global variable. 

When programs start to get larger and more complex, local variables are a useful way to ensure that only one function has access to its own variables. This prevents programming errors when one function modifies variables used by another function.

Global and Local variables

In Arduino, if a variable is declared at the top of the program, before the void setup, all parts of the program can use that variable. Hence, it is called a Global variable. On the other hand, if the variable is declared between a set of curly brackets, the variable is only recognized within that scope. That is, it will only e recognized and can only be used between that set of curly brackets.

For example, if a variable is declared in the void setup(), it will not be recognized and can not be used in the void loop, because the void loop is within its own set of curly brackets.

The below code shows some examples of what this means for the sketch. We are using INT as the datatype to declare the variable. In the next tutorial, we will explain in detail various data types for variables and how to use them.

Example Code

int x = 5;  // any function will see this variable with the value 5.

void setup()
{
  Serial.printIn (x) // printing the variable x to the serial monitor.
}

void loop()
{
  int i = 10;    // "i" is only "visible" inside of "loop"
  
}

Naming the variable

One of the most important parts of the variable is the “naming”. Even though you can name your variable whatever you want, it is wise to follow some rules.

Firstly, your variable should be descriptive of what information the variable holds. For example, in the tutorial: 2.1 Blink a LED we declared a variable called int pinLed = 10;. We could call it “pinWater”. However, that would not make any sense. It is more logical to declare the variable with a name that makes sense. In that way, anyone who reads the variable understands what the information is that is held by the variable.

Notice that the first letter in the second word in “pinLed” is capitalized. This is not necessary, but it will be easier to distinguish the two words.

Initializing a Variable

As mentioned in Tutorial 5 we need to have a semicolon at the end of every statement. Once we have declared the variable it can hold information. To put a value inside the variable we are going to use the Assignment Operator. This is just an equal sign which gives our variable to value that is given after the equal sign.

As mentioned before, when we are declaring a variable, we need the data type followed by the name.  Then, at the end of that statement, we want a semicolon.

The first time we assign a value to the variable, it is called initializing the variable.  In the above example, we declared and initialized the variable in the same line of code: int x = 5;

Since is a global variable we can change the value at some later point in the program. The only thing we need to do is to use the variable name and assignment operator again.

In other words, type in the variable name, use the equal sign and add value. Remember to end the statement with a semicolon. In the example below this is added. If you would run this program and look at the serial monitor you will notice that the first time it will print the value 5 and if the sketch reaches the loop part it will contain the value 10.

int x = 5;  // any function will see this variable with the value 5.

void setup()
{
  Serial.printIn (x) // printing the variable x to the serial monitor.
}

void loop()
{
  int i = 10;    // "i" is only "visible" inside of "loop"
  int f = 5;  // "f" is only "visible" inside of "loop"
  // ...
  
  x = 10 // the variable x now holds the value 10.
  Serial.printIn (x)

  for (int j = 0; j <100; j++){
  // variable j can only be accessed inside the for-loop brackets 
  // we will discuss for-loops in the upcoming tutorials
  }

}

Another important aspect of Arduino is the usage of strings. In the next tutorial, we will take a look at this. Arduino Tutorial: 2.3 Understanding strings in Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu