1

If Statements and Digital IR Sensors

Infrared light, or IR, sensors are used to detect changes in infrared light. Your CoDrone remote control comes with multiple digital and analog IR sensors that you can program. In this lesson, you’ll learn how to program them as triggers using if statements.

Each square on the bottom of your remote control’s Smart Inventor Board is an IR sensor, and the picture below shows you which digital IR sensors are available for you to program. You’re going to focus on sensors 11, 14, and 18 in this lesson. 12 and 17 are kind of in the restricted section, so you’ll learn how to program those in a later lesson!IR sensors shoot infrared light outwards and detect whether or not it bounces back. When you cover an IR sensor, like sensor 18 on the right, the IR light bounces off your finger and the sensor is triggered.When the sensor isn’t covered, the IR light has nothing to bounce off of, so it’s untriggered.You’re going to program your CoDrone to do different things depending on if the IR sensor is triggered or untriggered.When you use any of your sensors, you need to manually tell your code to check whether or not the sensor is triggered. To do this, you can use a variable in your code:

byte name = digitalRead(pin number);

What all of that means:

  • Byte is the type of variable. Bytes can be any whole number from 0 to 255.
  • Name is the name of the saved value. You can name the value anything, as long as there are no spaces or special characters!
  • Pin number is the sensor you want to read. Each sensor is attached to a different pin on the Smart Inventor Board and they usually share the same number!

If you’re planning on programming sensor 18, this is what your code would look like:

byte bt8 = digitalRead(18);
  • Byte is used for the variable since the value of 18 is between 0 and 255.
  • bt8 is short for button 18.
  • 18 is inside the parentheses for digitalRead since that is the sensor you’re reading.

When this code is run, digitalRead(18)will either equal 1, which is true, or 0, which is false, depending on if the sensor is triggered or untriggered. A triggered, or covered, sensor will equal 1 and an untriggered, or uncovered, sensor will equal 0.

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

#include <CoDrone.h>

void setup(){
   CoDrone.begin(115200);    
   CoDrone.pair(); 
}

void loop(){   
    byte bt8 = digitalRead(18); 
}

digitalRead(18) is in void loop because you should continuously check if sensor 18 is being triggered. If you put it in void setup, that will only happen at the beginning of the program!

Your sensor is checked and saved with your variable, which is bt8. Your sensor’s value can equal either 1 (which is when the sensor is triggered) or 0 (which is when the sensor is untriggered) depending on what it’s reading.

If statements are conditionals, which are used to help your program make decisions. You actually use conditionals every day! Some examples might include:

  • If my alarm goes off, then I will wake up.
  • If I am hungry, I will eat something.
  • If it’s sunny out, I will wear sunscreen.

If statements are usually written like this:

if (Condition) {
    Statement;
}
  • The condition is what needs to be true in order for the statement to be performed.
  • The statement is what will happen if the condition is true.
  • if the condition is false, then your program will move on to the next line of code.

For example, if you wanted to use “If my alarm goes off, then I will wake up” in your program, then it would look like this.

if(my alarm goes off) {
    I will wake up;
}

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

Here are some example codes!

Example 1:

int x = 10;
int A = 0;

if(x > 5){
    A = 1;
}

Pseudocode:

  • Variable x is 10.
  • Variable A is 0.
  • If x is greater than 5, A will be 1.
  • Since x is 10 and greater than 5, A will be set to 1.

Example 2:

int x = 10;
int A = 0;

if(x <= 8){
    A = 1;
}

Pseudocode:

  • Variable x is 10.
  • Variable A is 0.
  • If x is less than or equal to 8, A will be 1.
  • Since x is 10, which is more than 8, A will not be set to 1.

Example 3:

int x = true;
int A = 0;

if(x){
    A = 1;
}

Pseudocode:

  • Variable x is true.
  • Variable A is 0.
  • If x is true, then A will be 1.
  • Since x is true, A will be set to 1.

Remember your code from earlier that read the value of sensor 18?

#include <CoDrone.h>

void setup(){   
    CoDrone.begin(115200);    
    CoDrone.pair(); 
}

void loop(){   
    byte bt8 = digitalRead(18); 
}

Now you can include an if statement!

#include <CoDrone.h>

void setup(){   
    CoDrone.begin(115200);    
    CoDrone.pair(); 
}

void loop(){   
    byte bt8 = digitalRead(18);    
    
    if(bt8){   
        //Statement   
    }
}

Variable bt8 will be true when sensor 18 is triggered, so whenever you cover that sensor, the statement inside the if statement will execute.

Make your CoDrone fly when you cover sensor 18 by adding a statement in the if statement!

#include<CoDrone.h>

void setup(){
   CoDrone.begin(115200);
   CoDrone.pair();
}

void loop(){ 
    byte bt8 = digitalRead(18);
    byte bt4 = digitalRead(14);
    byte bt1 = digitalRead(11);

    // Once pin 18 is triggered, do a takeoff, wait 2 seconds, then land
    if(bt8){
     CoDrone.takeoff();
   
     CoDrone.land();
   }

   else if(bt1 && bt4 && !bt8){
      CoDrone.emergencyStop();
    }
}

This is the pseudocode:

  • Include the CoDrone library.
  • Begin communication at a baud rate of 115200.
  • Connect to a nearby CoDrone.
  • Check the value of sensor 18.
  • If sensor 18 is triggered, the CoDrone takes off for 2 seconds and then lands.
  • Else, if sensors 11 and 14 are triggered, the CoDrone will stop.

Challenge: Wait For It…

Use what you learned in this lesson to write programs so your drone flies a certain way when different sensors are covered!

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
  • Hover for 2 seconds
  • Land

Test your triggers to see how your CoDrone reacts!

Hint: To hover, send the command CoDrone.Control()without any flight movements (throttle, pitch, yaw, or roll) before it.