variables in arduino programming

Effective coding techniques in Arduino: More about variables

This entry is part 4 of 8 in the series Arduino Tips & Tricks

A variable is a way of naming and storing a numerical value for later use by the program. As per its meaning variable is actually assigned to a value which may constantly change. But if you write –

const int variable_name = value;

then its value remains fixed and never changes.

The following code declares a variable called inputVariable and then assigns it the value obtained on analog input pin 2:

int inputVariable = 2; // declares a variable and assigns value of 2
inputVariable = digitalRead(3); // set variable to value of digital pin 3

Once a variable has been assigned or re-assigned, you can test its value to see if it meets certain conditions or you can use its value directly.

Let us see an example. The following code tests whether the inputVariable is less than 100, if true it assigns the value 100 to inputVariable, and then sets a delay based on inputVariable which is now a minimum of 100.

if(inputVariable < 5) // tests variable if less than 5
{  
  inputVariable = 7; // if true assigns value of 7
}
  delay(inputVariable); // uses variable as delay

Note: Variables should be given descriptive names to make the code more readable. Variable names like IRSensor or pushButton help the programmer and anyone else reading the code to understand what the variable represents. You can use any name of the variable, only if, it is not a keywords in the Arduino language.

Declaration of Variable

All variables must be declared before they can be used. Declaration of variable means defining its value type, as in int, long, float, etc. Then give it a specified name. You can also assign initial value to the variable, but this is optional. You have to do it only once in a program but the value can be changed at any time using arithmetic and various assignments.

The following example declares that inputVariable is an int, or integer type, and that its initial value equals zero. This is called a simple assignment.

int inputVariable = 0;

A variable can be declared in a number of locations throughout the program and where this definition takes place determines what parts of the program can use the variable.

Scope of Variable

A variable can be declared at the beginning of the program before void setup(), locally inside of functions, and sometimes within a statement block such as for loops. The variable is declared at a particular position decides the scope of that variable.

A global variable can be seen and used by every function and statement inside the program. This variable is declared at the beginning of the program, before the void setup() function.

But a local variable is defined inside a function or as part of a for loop. So it is visible inside that function in which it was declared.

So it is possible to have two or more variables of the same name in different parts of the same program that contain different values. But if you have declared a global variable as int IRSensor; then you cannot use the same name elsewhere inside that program, because it is global variable and visible inside the complete program.

The following example shows how to declare a few different types of variables and demonstrates each variable’s visibility.

int value; // 'value' is visible to any function
void setup()
{
  // nothing here
}
void loop()
{
  for(int i=0; i<10;) // here “i” is visible inside the “for” loop only
 {
    i++;
  }
 int k; // here “k” is visible inside void loop only
}

About byte

The byte stores an 8-bit numerical value without decimal points. It has a range of 0 to 255.

byte variable_name = 180; // declares 'variable_name' // as a byte type
Share on your network!
Dattaraj Vidyasagar
Dattaraj Vidyasagar

Author on this website. He is veteran of Core Electronics since last 35+ years. ATL Mentor of Change, Niti Ayog, Govt. of India, Google Certified Educator, International Robotics Trainer and author of 17 books on electronics, robotics, programming languages and web designing... ➤➤

Leave a Reply

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