Variable Types

A variable is a special, small place in memory that stores information. To refer to this memory location and use it, we refer to its location by a name we make up, the variable name.

There are two types of numbers we will put in a memory location:

  • long: an integer that spans a larger range, from -2,147,483,648 to 2,147,483,647
  • floating point: a number with a decimal point, from -3.4028235E+38 to 3.4028235E+38

There are other types of variables, listed under “data types” in the reference page, but these are the two most common types of numbers we will use.

Using the long data type takes more memory than using just an int type, but if we are worrying about memory useage, or encounter an out of memory error, we should not use an Uno. We should use a microcontroller with more memory available, like a Due.

This means, as a good habit, if you need to use an integer, it should always be a long integer. This will be really important when we are counting time in micro seconds, for example. When using a long integer, always be aware of its maximum value and take precautions if you will get close to this limit.

For example, if we are measuring time in microseconds, we can count a little over 2 billion micro seconds. This is about 2,000 seconds, or about 33 minutes.

If we have to count longer than 33 minutes, we should consider counting in milli seconds. We can count up to 2 billion milliseconds, or 2 million seconds, which is about 23 days. If we need to count longer than this, we have to add an extra counter variable.

If we are not counting individual units of things, but want to describe a number like 5.9923 or get a more accurate value when we look at a fraction, like 9/4, then a float is the right data type to use.

Generally, when we decribe a number, it will be either a long or a float. If we need to have a decimal in there, it will be a float. If we are counting numbers, it will be a long.

At the very beginning of the sketch we allocate memory spaces for all the variables we want to use. This is where we give the memory spaces names and define what sort of variable they are.

We call this step, “declaring” our variables.

For example, if we want to use the two integers, named iNum1 and iNum2 and a floating point number, named V_sensor1_v, at the beginning of the sketch, before the void setup() function, we would add the following:

long iNum1;
long iNum2;
float V_sensor1_v;

void setup() {
  // put your setup code here, to run once:

This is only the beginning of using variables, initializing them with a specific data type. When we put the declaration of their data type at the beginning, we can use this variable name (which points to some data in a memory location) anywhere in the sketch, even inside functions.

This is why we call variables we declare in the beginning of the sketch as “global” variables, as they can be used globally throughout the entire sketch.

Of course, nothing exists outside a sketch, so only variables declared in the sketch can be used in the sketch.