Beginner Series Lesson 11: Using Flags Part II
The second part of the two lesson series on Event Flags and their uses in programming

IMG_1814

In the previous lesson, we learned about what event flags are and some basic ways to use them.  Here we’ll explore some more advanced uses of event flags

Advanced Flag Control

As you should recall, there are two parts to any event flag:

  1. Flag Statement
  2. Event Statement

The flag statement is the statement that controls the value of the flag

The event statement is the statement that responds to the value of the flag

These two in conjunction with each other allows us to effectively control the flow of our programs.   Now we’ll explore some methods to make our flag codes more efficient and more effective.

 

Incrementing Flag Statements

From what we know so far, the number of different events we can control is limited by the number different flag values we can assign.

For example, if we wanted 3 different modes, we could write the following event statements:

This works fine, but it means we would need to have 3 different Flag statements as well

This would require 3 different sensors and 3 different if-statements.  Using a program like this would take up almost half of our available sensors.

To get around this, we’re going to use an incrementing flag statement

Using an incrementing statement allows us to control an unlimited number of flag values with a single sensor.  Let’s break this down:

Whenever sensor
bt1 is triggered, the new value of
Flag  equals the current value of
Flag  plus
1 .  So, since all variables are initialized with a value of zero, whenever
bt1  is triggered

Flag = 0 + 1;       Flag = 1

Flag = 1 + 1;        Flag = 2

Flag = 2 + 1;       Flag =3

Ect ect…

So you can see the value for
Flag  is just adding
+1  each time
bt1  is triggered.

This line just adds a buzz noise whenever the sensor is triggered.  This makes it easier to keep track of what flag value you are on.

The delay is necessary because of how quickly your program runs.  If we did not have a delay here, then each time you moved your finger over sensor
bt1 , you would accidentally increment the flag multiple times instead of just once.  Adding
delay(300)  will give you 300 milliseconds to move your finger out of the way each time.

 

One last thing we need to add when using an incrementing flag is a reset condition.

This will prevent our flag value from going beyond the number of events we have.

Flag Cycling

 

Sweeping Statements

Sweep statements are useful for when you want a number to increase, then decrease, then increase again (I.E. sweep back and forth).

Using flags, we can separate our goal into two statements

  1. If I reach 10, go into subtraction mode
  2. If I reach 0, go into addition mode

We can convert these into flag statements

Here, we’ll say
Flag = 1  will be “addition mode” and
Flag = 0  will be “subtraction mode”.

“addition mode” would be where you are adding increments to your number, or the upward sweep

Number = Number + 1;

“subtraction mode” would be where you are subtracting, or the downward sweep

Number = Number  1;

 

Turning these modes into event statements

Combining both sections, we get

So whenever
Number  goes above 9, it triggers
Flag = 1  When
Flag = 1 ,
Number  begins to decrease by 1.

When
Number  goes below 1, it triggers
Flag = 1 .  When
Flag = 1 ,
Number  begins to increase by 1.

Sweep Flag

 

We’ll use this method in later lessons to create a circle maneuver.

 

Using a Counter

Repeating a maneuver indefinitely is easy, since
void loop()  repeats forever by itself.  Sometimes, though, you only want to repeat something a few times.  In these cases, we can again use variables to help us.

For example, if we want this maneuver to repeat exactly 5 times

we could put it within an if-statement that uses a counter as a condition

Counter  here is just a normal variable that we named “Counter”.  As long as the value for
Counter  is less than 5, the statement will execute.  Now we just need to add the statement

within the statement to keep track of how many times it has repeated.  Each time the statement runs,
Counter increases by 1.  Once
Counter  reaches 5, the if statement
if ( Counter <=5 )  will no longer be true, so it will stop executing.

Pretty simple right?  To repeat the maneuver again, you can just have a statement somewhere that resets
Counter = 0 .

Another cool thing we can do with counters and incrementing variables is to create a pitching buzzer

As the value of
Counter  increases from 0 to 5, the pitch of the buzz will increase from 3000 to 8000.  This will help you keep track of which count you are on, and it will sound cool too.

These are just a few of the more common ways to utilize variables as event flags.  The possibilities are endless with programming, so don’t be afraid to experiment with your own methods.  Coding is all about creativity!

Activity: Trigger Finger

 

Create a series of maneuvers that can be executed one at a time using a single, incrementing flag variable.

For example, your predefined maneuvers can be:

  1. THROTTLE = 100
  2. PITCH = 70
  3. ROLL = 60
  4. Landing

These maneuvers should use the same flag variable as the condition

  1. Flag = 1
  2. Flag = 2
  3. Flag = 3
  4. Flag = 4

Each time bt8 is triggered, the Flag variable should increment by 1, allowing you to control you CoDrone by cycling through the maneuvers.

Try to fly through an obstacle course using only your single IR sensor.  You will have to time when you switch between maneuvers to properly control your CoDrone.

I.E.

Trigger Sensor bt8

CoDrone THROTTLE  = 100 until

Trigger Sensor bt8 again

CoDrone PITCH = 70 until

Trigger Sensor bt8 again

CoDrone ROLL = 70 until

Trigger Sensor bt8 again

CoDrone Landing

Be sure to include a kills switch!  The kill switch should include the extra statement

To ensure no commands are sent to drone after stopping.