Sunday, December 14, 2014

Making Decisions with if/else

We have seen a number of ways in which blocks of code can be executed.  What if however, we only wanted to execute a block of code if a certain condition within the program existed?  We can do that using the "if" statement.

USING "IF"

In an earlier lesson, New Concepts, The if statement, and Aliasing, we wrote a program that turned on a LED if our button was pressed.  This was accomplished using the following:

if(button == 1)
     led = 1;
led = 0;

That code says that if the button is equal to 1 (on), then turn on the LED, otherwise turn the LED off.  This is rudimentary code, but it sets us up for more complex examples of making decisions within our programs.  What if we had two buttons, and we needed different code to be executed depending on which button was pressed?  What if we had a variable that depending on the value, different code would be executed?  "if" allows us to do these kinds of things.

2 Buttons:

if (button1 == 1)
    {
        do these instructions;
    }
if (button2 == 1)
    {
        do these instructions;
    }

Variable:

if(variable <= 10)
    {
        do these instructions;
    }
if(variable > 10)
    {
        do these instructions;
    }

These examples show how we can use the if statement to execute or not execute some instructions.  As a reminder from the previous lesson, we only need to include the braces around our instructions if there is more than one instruction, otherwise we can simply write our one instruction without the braces.  Example:

if(this condition is true)
    do this one instruction;

The if statement tests the operator for a true/false state and only continues with execution if the result is true. If the result is false, the instruction(s) is/are skipped.

IF/ELSE

Now let's ask ourselves this question.  If I have two buttons, and I want some instruction to execute if neither button is pressed, how can I do this?  C gives us the "else" statement, which is basically a default.  Button 1 is not pressed, and button 2 is not pressed, so this default code block will execute.  Here is how that looks:

if(button1 == 1)
    {
        do these instructions;
    }
if(button2 == 1)
    {
        do these instructions;
    }
else
    {
        do these instructions;
    }

You can have pretty much as many of these options as you need to:

if(condition)
    instruction
else
    instruction

or

if(condition1)
    instruction
if(condition2)
    instruction
if(condition3, 4, 5...)
    instruction
else
    instruction

SIMPLE DECISION MAKING IN AN EXAMPLE

I have written a simple program to show decision making based on the condition of a variable.  There are much better examples, but we have not yet covered some concepts that are required in order to fully implement those examples.  In order to keep things simple and maximize understanding, I chose to use an example that operates without user input (i.e. runs on its own without any input from outside sources like buttons).

Figure 1.
 The full program is listed in Figure 1.  Our variable "count" is declared on line 16. Line 17 declares our shadow register variable for the GPIO register.
We have defined the initialization state of GPIO and TRISIO on 20 and 21 respectively.
GPIO ports 0, 1, and 2 have been defined as our LED masks on lines 24 through 26, and for our delay routines we indicate the clock frequency on line 29.
We then declare our functions, which in this case will only be to turn on each LED. This program doesn't require a function to turn them off, because we will simply clear the port register within the program.
Line 51 begins the main program, and we begin here by setting the TRISIO bits to outputs, and clearing the GPIO shadow register, then copying the shadow register to the GPIO register.  We also set the value of our count variable to zero.
Line 58 begins the main while loop.  Within this loop, the program makes a decision about which "if" block to execute based on the value of "count".  Since we set count to zero at the beginning of the program, the first if statement, which asks "if count is greater than 2, make count equal 0 is skipped.  Now, the if count equals 0 block is executed because "count" is equal to zero.
This if block, beginning on line 63 will clear the shadow register and the GPIO register, which would turn off any LEDs that might be currently on.  Then the red LED is turned on using a function call.  The program then delays 500ms before incrementing the count variable.
Count now equals 1.  The program flow at this point returns to the top of the while loop.  Count is now equal to 1, so the if (count == 0) block is skipped, and instead, the count == 1 block is executed.
This block, beginning on line 72, turns off any LEDs that are on, then turns on the blue LED.  The sequence completes the same as the red sequence.
Count is now equal to 2, thus, the two "if" blocks are skipped.  Since both if blocks are false, the default sequence "else" is executed, which again clears the shadow and port registers.  The default state is to turn on the white LED, which this if block does. Count is again incremented and is thus equal to 3.
Now when the program returns to the top of the while loop, count is greater than 2, so the if (count > 2) instruction gets executed, which makes count equal to zero again.  Note that there are no braces around this instruction since there is only one instruction.

We have had some hardware changes for this example.  To follow along, you will need:
1 PIC12F629 microcontroller
1 LM7805 linear voltage regulator
1 0.1uF ceramic capacitor
3 220 Ohm 1/4W resistors
1 9V battery or other power source
1 each red, blue, and white (or whatever colors you may have) LEDs with a forward current of about 20mA.
Various jumper wires
1 breadboard

A schematic and breadboard layout for your reference are included below in Figures 2 and 3.

Figure 2.
Figure 3.

No comments:

Post a Comment