Beginner Series Lesson 10: Using Flags Part I
The first part of a two lesson series on Event Flags and their uses in programming

Copy of IMG_9828

 

In this lesson, we learn about what an event flag is and why they are useful to us.  We will be using event flags in almost all of our programs from this point on.

What Are Event Flags?

Back in the ye olden days, long before the internet or telephones were invented, colorful flags and banners were used as a form of communication called “semaphore”.  This was especially useful for ships and seafarers, where someone on one boat would hold up a flag to send a message to another boat.

Flags_1

hi

Different flag signals would have different meanings.  For example, you could say:

“If I hold up the red flag,
then attack.

If I hold up the blue flag,
then retreat”

In programming, flags have a very similar role.  We usually use flags as conditions in if-statements:

if ( FLAG == 1){
//Event 1
}

else if (FLAG == 2){
//Event 2
}

Depending on the value of FLAG, our program will either do Event 1 or Event 2.

Using Flags

It’s important to remember that flags are just variables that perform a specific function.  Just like any variable, you have to initialize it before you can use it.

and, just like any variable, you can name it whatever you want.  We’ll stick with the name “Flag” for clarity.

The important part of an event flag is that an event depends on it.  The flag itself is the condition for a statement

Notice how Flag is used in the conditional section of the if-statement   if ( Flag == 0 ) .  Depending on the value of our Flag variable, either Event 1 or Event 2 will occur.

This is pretty useful for switching between multiple events.  Using flags like this will help organize our code and allow us to more effectively control the flow of our programs.

Now we have two events that depend on the flag variable (Event 1 and Event 2).  The next step is to be able to control the value of Flag . To do this, we’ll use what we’ve learned about IR sensors in the previous lessons and create a new if-statement to control Flag.

If the right IR sensor (bt8) is triggered,
Then change Flag to 1

We should also be able to change Flag back to 0

If the left IR sensor (bt1) is triggered,
Then change Flag to 0

Now we have one set of if-statements to control our flag variable, and one set of if-statements that respond to our flag variable (don’t forget to initialize your sensors!):

Now we have a working event flag!

 

What’s The Point?

There are a few differences between using plain if-statements and using event flags

Switching

If you want to continuously send a throttle command to the CoDrone using an if-statement, you would have to cover the sensor for the entire duration

A flag will only need to be triggered once

Flag will equal 1 until it is explicitly changed.  The advantage to this is that you don’t need to keep holding the sensor.  The disadvantage is that you might need a separate statement to change Flag to something else, or “switch” the flag off.

Multiple States/Modes

Possibly the greatest advantage of using event flags is that you can define multiple states.  For example:

This defines 3 different states or modes.  We can now create 3 unique events to correlate with each state.

This allows us to send multiple different maneuvers to our CoDrone simply by changing the state of our flag variable.  Our sensors  bt1  and bt2  essentially have 2 different modes now.

 

Self-Canceling

Another cool trick you can do with flags is to create an event that turns itself off

Here, when Flag == 1 , we will send our throttle command exactly once before the flag is turned off.  This is useful in a few specific situations that we will encounter in some more advanced lessons.

 

Activity: Opposite Day

Take your controller flight code from the previous lesson and add a Reverse Controls mode.

Ex:  When I trigger sensor bt1, Mode = 2

If Mode = 2, controls are reverse

When I trigger sensor bt2, Mode = 1

If Mode = 1, controls are normal

 

Be sure to include a kill-switch!

 

In the next lesson, we’ll learn how to cycle through multiple flag values using a single IR sensor.