Beginner Series Lesson 7: If, else if, and else statements
Here we w ill focus on if, else if, else-statements and using conditional operators

Summary

In this lesson, we review some tips and tricks for formatting conditional statements and explore the different variations of if-statements.  We will also learn how to program our own flight controller using only IR sensors.

## Conditional Statements

From our last lesson we learned that an if-statement follows the format

As long as the condition is true, then the statement will execute.  We also learned how to create basic triggers using conditional operators like <  and => . Sometimes, we might want more complex conditions for our programs.  Luckily, Arduino gives us a lot of options for how we want to write our conditional statement.

We can create intricate conditions using a few special operators

&&  And
||  Or
!    Not

Here are a some examples:

Example 1:

The condition (x > 5 && x < 9) is false, since x = 10 and 10 > 9
So the statement A = 1 does not execute.   The final value of A is 0.

Example 2:

The condition (x > 5 || x < 9) is true, since x = 10 and 10 > 5
So the statement A = 1 executes.   The final value of A is 1.

Example 3:

The condition (x && !y && z) is true, since x = true, y = not true, and z = true.
So the statement A = 1 executes.   The final value of A is 1.

If,  Else if, Else-Statements

if (Condition) {
Statement
}

else if(Condition){
Statement
}

else if(Condition){
Statement
}

else{
Statement
}

• Each condition is checked one at a time, from top to bottom.
• There can be multiple else if statements, but only one if statement and one else statement.  An else statement is not required.
• Condition – If the first condition enclosed in the parenthesis is true, then the statement will be executed.  If not, the next condition is checked.
• Statement – Anything enclosed within the brackets is the statement and will only execute if the condition is true, except for else.
• if all conditions  in the entire chain are false, the else statement will then execute

The benefit of using an if-else if chain statement is that only one statement can execute at a time.  Some examples:

Example 1:

The condition (x < 8) is true, since x = 5 and 5 < 8.
So the statement A = 1 executes.   The value of A is 1.
The condition x < 10  is also true, since x = 5 and 5 < 10.
So the statement A = 2  also executes. The final value of A is 2.

Example 2:

The condition (x < 8) is true, since x = 5 and 5 < 8.
So the statement A = 1 executes.   The value of A is 1.
The next condition is not read, since the first condition already executed.
The final value of A is 1.

Example 3:

The condition (x && y && z) is false, since y = false.
So the statement A = 1 does not execute.   The value of A is 0.
The condition (!x && !y && z) is also false, since x = true.
So the statement A = 2  does not execute.  The value of A is 0
Since all conditions are false, the else statement automatically executes.
The final value of A is 3.

Remember, an IR sensor that is triggered is true, and one that is not triggered is false.

bt8 = false

bt8 = true

We can now combine these lessons to create triggers in our program that only execute when we cover our IR sensors.

## Using Advanced Triggers

Using what we’ve learned in our previous lessons, let’s write code to read IR sensors 11, 14 and 18

When we combine this with an if-else if-else-statement, we can create complex combinations triggers

We know that our sensors are equal to true  when the IR sensors are covered.  Statement 1 will execute if sensor 11 is not covered, sensor 14 is covered, and sensor 18 is covered.  Statement 2 will execute if sensor 11 is covered, sensor 14 is covered, and sensor 18 is not covered.

Now let’s make our CoDrone fly when we cover these sensors:

This will send THROTTLE = 70  to our CoDrone for as long as sensor 11 is not covered, sensor 14 is covered, and sensor 18 is covered.

This will send the kill switch CoDrone.FlightEvent(Stop) to our CoDrone for as long as sensor 11 is covered, sensor 14 is covered, and sensor 18 is not covered.  The kill switch will instantly stop all motors.  To be safe, you should always code in a kill switch.

You can now use advanced triggers and IR sensors to control your CoDrone!

Activity: Joyride

Using what your learned in this lesson, write a flight controller for your CoDrone using only the digital IR sensors!

You should be able to completely maneuver your CoDrone using only the IR sensors on your controller.  No joysticks allowed.

Hints and Tips:

• To TakeOff using THROTTLE, you must use a THROTTLE value of 60 or above  (THROTTLE = 60)
• There are two options for moving sideways.  You can either use ROLL to move sideways, or YAW to turn sideways then PITCH to go forward.  You don’t necessarily need to be able to YAW and ROLL
• Don’t forget, only the first condition that is true in an if-else if-else chain will execute
• Use combination commands to add more complexity to your code!  (ex. bt1 && bt2  && !bt7 && bt8)

Don’t forget to have a kill switch!