Beginner Series Lesson 6: If-Statements and Digital IR Sensors
This lesson will introduce digital IR sensors and if-statements

Summary

IMG_1464

IR sensors, or infrared light sensors, are used to detect changes in infrared light.  Our CoDrone controller comes with multiple programmable digital and analog IR sensors. In this lesson, we will learn about digital IR Sensors and how to use them as triggers for our CoDrone.

 

How does an IR Sensor Work?

IR sensors work by shooting infrared light outwards and detecting whether or not it bounces back.   The picture below shows you all of the available digital IR sensors on the CoDrone Controller.

Digital IR Sensors Pins

Each of the little squares on the bottom of your CoDrone controller are IR sensors.  (The two sensors on the left and right of 14 are disabled when using the CoDrone library.  We will discuss how to use these restricted sensors in the Advanced Lessons section.)

Let’s focus on the right IR sensor, Sensor 18.  When the sensor is not covered, the IR light has nothing to bounce off of, so the sensor is “untriggered”.  If the sensor is covered, then the IR light bounces off your finger and the sensor is “triggered”.

 

IMG_1625red

Untriggered

IMG_1627red

Triggered

We’ll learn to utilize these sensors as triggers for our CoDrone programs.

Reading the IR Sensor

When using sensors, you always have to manually tell your code to check whether or not the sensor is triggered.  To do this, we use the code:

byte name digitalRead(pin number);

byte name = digitalRead(pin number)

  • name – the name of the saved value.  This name can be anything you want it to be
  • pin number – The pin number of the sensor that you want to read
  • byte – The type of variable

Since we’re using sensor 18, we put 18 inside the parenthesis.  We’re also naming the variable bt8, which is just short for byte 18:

In programming, a value of 1 is also called true and a value of 0 is also called false .   digitalRead(18)  either equals 1 (true) or (false) based on whether or not our sensor 18 is triggered or untriggered. A covered sensor is true (1) and an uncovered sensor is false (0) and a .

IMG_1625red

bt8 = false = 0

IMG_1627red

bt8 = true = 1

Using the Sensor Value

Here’s an example of a code that reads IR sensor 18:

Notice that we put  digitalRead()  in void loop() .  This is because we want to continuously check if our sensors are being triggered.  Code within   void loop() is read over and over, so including our code in here will ensure that our sensor will be checked over and over as well.

We have it so our sensor is checked and saved as the variable  bt8 , which equals 1 (true) or 0 (false) depending on our sensor. We can now combine our sensors with if-statements to create triggers in our program that only execute when we cover our IR sensors.

If-Statements

We’ll start by introducing if-statements

If-Statements

if (Condition) {
Statement
}

  • Condition – If the condition enclosed in the parenthesis is true, then the statement will be executed
  • Statement – Anything enclosed within the brackets is the statement and will only execute if the condition is true
  • if the condition is false, then this code does nothing.

The if-statement consists of a condition and a statement.  If the condition is true, then the statement executes.  Otherwise, the statement doesn’t happen.

Some conditions that we can use are:
>  Greater Than
<  Less Than
>=  Greater Than or Equal To
<=  Less Than or Equal To
==  Equal To
!=  Not Equal To
!  Not

Example 1:

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

Example 2:

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

Example 3:

The condition (x) is true, since x = true
So the statement A = 1 executes.   The final value of A is 1.

IMG_1625red

bt8 = false = 0

IMG_1627red

bt8 = true = 1

Using Triggers

Here’s code from earlier that reads IR sensor 18

When we combine this with an if-statement, we have a trigger
We know that bt8  is equal to true  when the IR sensor 18 is covered.  The statement will execute if sensor 18 is covered.

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

This will make our CoDrone take off, wait 2 seconds, then land when we trigger the IR sensor 18.

You can now use if-statements and IR sensors to activate your CoDrone’s flight code whenever you want!

Activity: Wait for it…

Using what your learned in this lesson, write a code that programs your IR sensors in the following ways:

IR Sensor 18:

Take off for 2 seconds
Pitch = 60  for 1.5 seconds
Land

IR Sensor 14:

Take off for 2 seconds
Roll = 60  for 1.5 seconds
Roll = -70 for 1.5 seconds
Land

IR Sensor 11:

Take off for 2 seconds
Throttle = 50 for 1.5 seconds
Stay Still for 2 seconds
Land

Now test your triggers and see how your CoDrone reacts!

Hint: To stay still, just send the command CoDrone.Control();  without any THROTTLE, PITCH, ROLL, or YAW before it.