ZachLabs Arduino

Deep Dive: Variables

Similar to Scratch and Python, C allows you to create variables to store temporary values. In the example for Lab 2 we used this code to blink the LED at a rate dependent on the state of a switch:

void loop()
{
    if (digitalRead(3) == LOW)
    {
        // The switch is on, so blink the LED on pin 2 quickly:
        digitalWrite(2, HIGH);
        delay(100);
        digitalWrite(2, LOW);
        delay(100);
    }
    else
    {
        // The switch is off, so blink the LED on pin 2 slowly:
        digitalWrite(2, HIGH);
        delay(200);
        digitalWrite(2, LOW);
        delay(200);
    }
}

We can rewrite this code to read the switch state, store the corresponding delay time in a variable (delayTime), and then blink the light using that delay time instead of hardcoding the delay() calls:

void loop()
{
    // Determine the delay time by reading the switch on pin 3:
    int delayTime;
    if (digitalRead(3) == LOW)
    {
        delayTime = 100;
    }
    else
    {
        delayTime = 200;
    }

    // Blink the LED on pin 2 using the delay we determined above:
    digitalWrite(2, HIGH);
    delay(delayTime);
    digitalWrite(2, LOW);
    delay(delayTime);
}

Much like function parameters, variables in C must be declared before they can be used and must specify a data type. The variable used above, delayTime, is an int that can store an integer between -32,768 and 32,767, but there are many other data types available. Here are some common ones, taken from the Arduino website:

Type Description
bool A boolean value that can be either true or false.
byte An 8-bit unsigned integer, from 0 to 255.
int A 16-bit signed integer, from -32,768 to 32,767.
unsigned int A 16-bit unsigned integer, from 0 to 65,535.
long A 32-bit signed integer, from -2,147,483,648 to 2,147,483,647.
unsigned long A 32-bit unsigned integer, from 0 to 4,294,967,295.
float A 32-bit floating point number (also known as a decimal number).
double A 64-bit floating point number (also known as a decimal number).

Variable Scope

In C, matters are further complicated by a concept called variable scope. If a variable is declared within a set of curly braces it can only be used within those curly braces, and then only after it is declared. Consider the following example, in which a variable is declared within a function and can be accessed from some places but not from others:

void loop()
{
    // We *CANNOT* use delayTime here; it has not yet been declared.

    int delayTime = 0;

    // We *CAN* use delayTime here.

    if (digitalRead(7) == LOW)
    {
        // We *CAN* also use delayTime here because we are still inside
        // the outer set of curly braces that it was declared within.
    }
}

// We *CANNOT* use delayTime here because it is no longer in scope.

If you want to be able to use a variable from anywhere in your program you should put it at the top of your program, before setup() and loop(). This is sometimes called a global variable.