LESSON 04 – VARIABLES

 

STORAGE

 

In programming, we often need to calculate a value, then hold on to it until we need it later on.  Variables allow us to store values inside programs. 

 

TYPES OF VARIABLES

 

Some programming languages, including Arduino’s, require that we specify the type of data that has to be stored.  We do this by creating a variable of the corresponding type.  Here are some of the basic types:

 

int – stores an integer (whole number)

float – stores a number that contains decimals

string – stores a sequence of characters such as a word

 

DECLARING A VARIABLE

 

Declaring a variable simply means announcing to the program that we will be using said variable.  Each variable always has to be declared before being used.

 

The following declares an integer variable called count:

 

            int count;

 

The following declares a float variable called soda:

 

            float soda;

 

VARIABLE NAMES

 

There are a few simple rules for naming variables:

  • No spaces
  • Should start with a lowerCase letter
  • Inner words should start a capital letter

 

Here are a few good examples:

 

  • loopCount
  • maxVoltage
  • word

 

Here are a few bad examples:

 

  • loop count
  • MaxVoltage
  • 2words

 

ASSIGNING VALUES

 

We can assign a value to variable.

 

Here is an example that first declares a varable and then assigns it a value:

 

     int maxVoltage;

     maxVoltage = 5;

 

We can also do the above in a single line:

 

     int maxVoltage = 5;

 

We must assign a value of the same type to a variable.  So we cannot assign 5.5 to an integer variable. 

 

OUTPUTTING VARIABLES TO SERIAL MONITOR

 

We’ve already seen how to output a message to the Serial Monitor.  We can also output the value of a variable.

 

Here is an example that creates a variable, gives it a value and outputs that value to the Serial Monitor:

           

     int value = 10;

     Serial.println(value);

 

Note that the bottom line only works if you’ve established a connection between the Arduino and the computer by using the Serial.begin(9600); statement.

 

ARITHMETIC EXPRESSIONS

 

We can assign arithmetic expressions to variables.

 

The following example creates two variables and gives them values.  Then, a third variable is given the sum of the first two variables.

 

     float a = 5.2;

     float b = 4.3;

     float c = a + b;

 

Here’s another example with expressions:

 

     int x = 1;

     int y = 2;

     int z = 3;

     int product = x * y * z;

 

RIGHT SIDE FIRST

 

When the Arduino encounters a line of code that has an arithmetic expression, it calculates the value of the expression and then continues with the work of that line.

 

This means that one can have a variable that appears on both sides of the = sign.

 

An example of this is when you want to increment a value of a variable by 1.  The statement below increments variable x by 1.

 

     x = x + 1;

 

GLOBAL VARIABLES

 

Variables that are declared inside of a function only exist within that function.  So you cannot declare a variable inside the setup function and then use it inside the loop function.

 

If you do wish to use a variable inside different functions, then you can declare the variable at the very top of the program above the functions.  Then, that variable will exist inside all functions.

 

Here’s a general example:

 

     int value;

 

     void setup()

     {

         //you can access value inside here

     }

 

     void loop()

     {

         //you can access value inside here as well

     }

 

DELAYS

 

We can make the Arduino wait for an amount of time by using the delay() command.  We specify the amount of time in milliseconds.

 

Here is the delay command halting the Arduino for 2 seconds:

 

     delay(2000);

 

 

TRY THIS…

 

PRACTICE PROGRAM 03

 

Write a program that creates 5 int variables a, b, c, d and e.  It then assigns them a value.  You pick the values.  It then calculates and outputs the sum and the product of those variables.  Note that you may wish to create variables that represent the sum and products.

 

PRACTICE PROGRAM 04

 

Write a program will declare a variable called x inside the setup function.  On the next line, initialize x to 10.  Add the lines needed to display the value of x on the serial monitor.

 

Alterations

 

a)     Inside the loop() function, try incrementing the value of x and then outputting it to screen.  Note that to do this, x needs to be a global variable.

b)    Add a delay of 1 second (1000 milliseconds) between the increments.

c)    The Serial.print() and Serial.println() commands have the option of converting a provided integer number to either binary or hex.  If you want to output the number 72 in binary, you use:

 

     Serial.print(72,BIN);

Or if you have a variable called value that you want to output in hex, you use:

 

     Serial.print(value, HEX);

 

Change your code to output the value of x, its equivalent in binary and its equivalent in hex.  The output should look like this:

 

            0 -> 0 -> 0

            1 -> 1 -> 1

            2 -> 10 -> 2

            3 -> 11 -> 3

            4 -> 100 -> 4

           

            9 -> 1001 -> 9

            10 -> 1010 -> A

            11 -> 1011 -> B

            … and so on…