# Finch 2.0: BirdBlox Lessons (4 – Going Further)

## Control Structures & Line Tracking

Steps
Steps
Steps
Steps
Steps
Steps

### Control Structures & Line Tracking

In this section, you will learn to use control structures to create more complex behaviors for the Finch, like following a line until the Finch finds an obstacle. Control structures determine what happens in a program. Some examples of control structures are loops and if-else statements.

### Making a Line to Track

For this lesson, you will need large sheets of white paper and a thick black marker. Start by making a thick black line on a white background. The line should be about 2.5 cm wide. Avoid sharp turns; it is easier for the Finch to follow gradual curves.

### Basic Line Tracking

To track a line, the Finch should turn right or left based on whether or not it detects the line. Let’s track the left side of the line with the left line sensor as the Finch moves clockwise around a loop. When the left line sensor is over the black line, the Finch should turn left. When the left line sensor is over the white surface, the Finch should turn right.

Notice that this sample code uses the Finch Wheels block to turn using only one wheel at a time. This means that both turns move the robot slightly forward. As the Finch repeats the if else block over and over, it will follow the line as it turns back and forth.

Note: As you run a BirdBlox program, the app relays commands over Bluetooth to the Finch. There is some delay in this process, and that means that you need to turn slowly to track the line.

Try out this code. You may need to adjust the line sensor threshold and the turning speed to track your line. The Display block will show you the line sensor value so that you can choose a good threshold. Remember, the Finch needs to turn slowly.

### The Repeat Until Block

Your current code works if you want the Finch to keep tracking the line forever. But what if there is an obstacle in the Finch’s path? Then you don’t want to repeat the if-else forever. You only want to repeat it until you find an obstacle. In this case, you’ll want to use the repeat until block on the Control menu.

This loop repeats the blocks inside it until the Boolean block is true. For example, this program makes the Finch blink its beak until the Finch’s beak points up.

CHALLENGE

Use a repeat until block to make the Finch track your line until it finds an obstacle. The Finch should stop when it finds an obstacle.

to see example code.

### Logical Operators

As you build more complex programs, you will sometimes want to make decisions based on more than one sensor. For example, you might want to turn on the Finch beak and tail only if both light sensors detect that it is dark. To do this, you can use the logical operators on the Operators menu. These operators are Boolean blocks that contain spaces for other Boolean blocks.

The and block is true if both blocks inside it are true and false if either block inside it is false. The or block, on the other hand, is true if either block inside it is true. The or block is only false when both blocks inside it are false. The not block is the opposite of the block inside it. It is true when the block inside is false and false when the block inside is true.

As an example, this program starts with the beak and tail lights off and then waits until the values of both light sensors are below the threshold. Then it turns all of the Finch lights on.

CHALLENGE

Modify your line tracking program to track the line until the Finch finds an obstacle or you place it in the Beak Up position.

to see example code.

### Creating Complex Behaviors

By using logical operators and all of the control structures that you have learned, you can create complex behaviors for the Finch. You can also use multiple control structures and nest blocks inside one another to get the behaviors you want. For example, what do you think this code will do? Think carefully and make a prediction, and then try it out!

CHALLENGE

Turning left or right based on a single line sensor is the simplest version of a line tracking algorithm, but there are many variations. Write a program to implement the alternate line tracking algorithm described below. Which works better for your path?

• If the left line sensor is over white and the right is over black, turn right.
• If the right line sensor is over white and the left is over black, turn left.
• If both line sensors are over the same color, move forward in a straight line.
• For an extra challenge, modify your program to make the robot automatically restart tracking the line when you remove the obstacle!

to see example code.

Try out these activities to practice using control structures.

### Randomness

In previous modules, you have used control structures to make your Finch behave differently in different situations. In this module, you will learn to generate random numbers and use them in your Finch programs to create interesting and sometimes unexpected behaviors for your Finch. For example, you can use random numbers to set the Finch’s beak and tail lights to random colors.

### Using the Pick Random Block

The pick random block is on the Operators menu in BirdBlox. This block randomly chooses a number between the two limits given. For example, this block generates a number between 1 and 100. Each time you tap the block, it will give you a different number.

You can use the pick random block anywhere that you would use a number in your program. For example, this program sets the speed of each wheel to a random number every two seconds. Notice that each wheel gets a different random number, so the Finch will mostly turn but sometimes move straight.

CHALLENGE

Use the pick random block to set the Finch’s beak and tail to different random colors.

to see example code.

### Using Randomness in Control Structures

You can also use a random number within a control structure. For example, this program will make the Finch beep a random number of times from 1 to 5.

CHALLENGE

Make the Finch play a random number song! The Finch should play a random number of notes, and each note in the sound should be random. For an extra challenge, make each note last a random number of beats from 0.1 to 1 (you will need a math block from the Operators menu).

to see example code.

Try out these activities to practice incorporating randomness into your programs.

### Variables

In this module, you will learn to use variables. A variable is a name that represents a value. Variables enable your Finch programs to save values and use them later.

For example, in the last module, you set the Finch’s wheels to two random speeds. If you want to set them to the same random speed, then you need to save one random number and then use it to set the speeds of both wheels. You can do this with variables.

### Creating a Variable

To create a variable in BirdBlox, go to the Variables menu and then click Create Variable

Give your variable a name. You should choose a name that describes what your variable is. In this case, let’s name our variable randomSpeed.

Notice that the Variables menu changes once you have created a variable. You will see a block for your variable, a set block, and a change block.

### Assigning a Value to a Variable

You can use the set block to assign a value to your variable. If you have multiple variables, use the drop-down menu to select the one you want to set. In this example, the loop sets the variable randomSpeed equal to a random number between -100 and 100. It resets the value of the variable every two seconds.

To use the variable, drag the block with the variable name into your program. You can use randomSpeed anywhere you would use another number block in BirdBlox. For example, you can use it to set the speeds of both Finch motors. You can also use the Display block to see the value of the variable on the screen. Notice that in this program, only the set block changes the value of randomSpeed. The randomSpeed block just fetches this number and uses it within the Display and Finch Wheels blocks. That means you can be sure that both wheels will have the same speed.

CHALLENGE

Create three variables named red, green, and blue. Use these variables to set the color of the Finch’s beak and tail to the same random color. The color of the beak and tail should change once per second.

to see example code.

### Changing a Variable

You can use the change block to modify the existing value of a variable. For example, this program sets blue to 0 at the top of the program and then adds 1 to blue each time through the loop. What will the value of blue be at the end?

You can use a loop like this to gradually change the value of a Finch output. For example, you can use blue to gradually increase the brightness of the Finch beak. You can even use a math block from the Operators menu to make the R value for the Finch beak go from 100 to 0 as the B value changes from 0 to 100. What do you think will happen when you run this code?

CHALLENGE

Modify the code above by adding another repeat block below the existing one. Use this repeat block to gradually change the value of blue from 100 back to 0.

to see example code.

### When to Use a Variable

Any time you need to use the same value more than once in a program, you can use a variable. Variables are also helpful if you may need to change a value later. For example, this program has two loops that use a threshold for the light sensor. One controls the Finch beak, and one controls the Finch buzzer. Depending on the amount of light in the room, you may have to adjust the value of the threshold in the future. This is a great opportunity to use a variable! Notice that we only set the value of the threshold variable once, at the very beginning of the program. If we need to change the threshold in the future, we will only need to change it in one place.

CHALLENGE

Write a program that does the following:

• The Finch should move forward until it is 10 cm from the wall.
• Then the Finch should back up until it is 30 cm from the wall.
• Then the Finch should move forward until it is 20 cm from the wall.
• All of these movements should be made using a variable named distanceThreshold that is equal to 10 cm. Your program cannot contain the numbers 20 or 30. Hint: You need to use a multiplication block!

What will your program do when you change distanceThreshold to 20?

to see example code.

Try out these activities to practice using variables.

### Lists

In the last topic, you learned to use variables. The Variables menu in BirdBlox also contains a button to create a list. While a variable only holds a single value, a list can hold many values. You can use a list to store data that your program needs, or you can use a list to record data with the Finch sensors. For example, you can use a list to record Finch light sensor readings over time.

### Creating a List

On the Variables menu, tap the Create List button. Name your list noteList and tap OK.

At first, the list that you create is empty, so start by adding some values. You can use the add block to add a value to the end of the list. For example, this code will add 60 to the list and then display the list on the tablet screen.

Use this script to add 5-10 different values to the list. All the values should be between 32 and 135.

### Accessing Items in a List

Once you have values in your list, you can use them within your program.  To access a value within the list, use the item block. For example, when you tap this block, it will show the first item in the list.

Declare a variable named count. You will use this variable to access each value in your list.

What block on the Variables menu can you use to find the number of items in your list?

### Using Data in a List

To access the data within a list, you can use a loop and your count variable. Consider this example code. At the beginning of the program, count is set equal to 1. Then the program uses a repeat until block to move through all the items in the list. Each time through the loop, the program displays an item of the loop and then increases the value of count. When count becomes larger than the number of items in the list, the loop ends.

CHALLENGE

Modify the loop above to make the Finch play a note for each item in the list. Now you can use a list to store a song for your Finch!

to see example code.

### Storing Finch Data in a List

You can add measurements from the Finch sensors to a list to store them. For instance, this loop adds ten light measurements, one per second, to a list named lightList. Then it displays the list on the screen.

CHALLENGE

Try out the code above, and then add a loop that moves through the list and uses each of the light measurements to set the brightness of the Finch tail lights. For example, if the list contains the numbers 22, 50, etc., then the brightness of the tail lights should be 22 for one second, then 50 for one second, and so on. Note: If you need to delete all the items from your list, use the delete block and select “all.”

to see example code.

Try out these activities to practice using lists.

### Timers

You can use a timer in BirdBlox to measure how long something takes to happen or to make the Finch repeat a set of actions for a certain amount of time. For example, you can program the Finch to move around randomly searching for an obstacle but stop the program if it doesn’t find anything within 15 seconds.

### The Timer Block

You can find the timer block on the Tablet menu. This block measures the amount of time in seconds that has passed since the timer was reset. To use the timer block in BirdBlox, you should always first use the reset timer block.

As an example, this program measures the time that it takes the user to place the Finch in the Beak Up position. First, the program resets the timer and tells the user what to do. Then it waits until the Finch is in the Beak Up position. After the Finch is placed in that position, the program tells the user the value of the timer, which is the amount of time between the beginning of the program and the Finch being placed Beak Up. The join block is  used to combine pieces of information to display them on the screen; you can find it on the Operators menu.

CHALLENGE

Write a program that makes the Finch move forward until it finds a line. The program should display how long it takes the robot to find the line.

to see example code.

### Loops with Timers

You can also use the timer to make the robot repeat a set of actions for a certain amount of time. For example, this program will make the Finch blink its beak for 5 seconds. When the value of the timer is more than 5 seconds, the loop stops.

CHALLENGE

Modify this code to make the Finch blink its beak for 15 seconds or until it sees an obstacle, whichever happens first. Then, have the program report to the user whether or not the robot has seen an obstacle.

to see example code.

### Variable with Timers

You can even combine timers with what you have learned about variables. For example, suppose you want the robot to move randomly for 15 seconds, and you want to change the random wheel speeds every second. You can do this with a variable and a timer using the code shown below. The program starts by setting the variable n equal to 1 and resetting the timer. Then a loop repeats until the value of the timer is more than 15 seconds. The action that is repeated inside the loop is to set the speeds of the wheels to random values every second. If the value of the timer is greater than n = 1, it sets the wheel speeds and increases the value of n to 2. If the value of the timer is greater than n = 2, it sets the wheel speeds and increases the value of n to 3. This process continues until the value of the timer is greater than 15 and the loop ends.

CHALLENGE

Try out the code above and make sure you understand what is happening. Then modify this code so that the Finch moves randomly until either it finds an obstacle or the timer reaches 15 seconds. If the Finch finds an obstacle within the allotted time, it should celebrate by turning its tail green and beeping happily. If it does not find an obstacle within 15 seconds, it should turn its tail red and make a low, mournful beep.

to see example code.

Try out these activities to practice using timers.

### Modularity

As you write more advanced programs, your code gets longer, and it is harder to see what is going on. You can make your code easier to understand by breaking it down into pieces; this is called modularity in programming. In addition, some of these pieces can be reused for future projects. For example, you can create a block of code that draws a polygon and then use it within your program to draw a bunch of different polygons. This is more modular than writing a new loop for every polygon.

### Two New Control Blocks

In BirdBlox, you can make your program more modular using two Control blocks, the broadcast and wait block and the when I receive block. These blocks work together in a program. The broadcast and wait block sends out a message. The when I receive block listens for a message and then does something when it receives that message.

Start by tapping the when I receive block and then tap new to create a new message. Name your message “Draw Square.”

Below the when I receive block, add a loop to move the Finch in a square. Then select “Draw Square” in the broadcast and wait block. Now, when you tap on the broadcast and wait block, the Finch will move in a square.

You may notice that there is a broadcast block in BirdBlox in addition to broadcast and wait. The broadcast block sends its message and then immediately moves on to the next block. The broadcast and wait block, on the other hand, sends its message and then waits until the corresponding when I receive script ends before it moves on to the next block. This is usually what we want when working with the Finch, because the Finch will need time to finish moving before it is ready for another message.

CHALLENGE

Use the broadcast and wait block to draw multiple squares. Move or turn the Finch a little bit before drawing each new square. What interesting patterns can you create?

to see example code.

### Creating More Modules

You can create additional modules by using more when I receive blocks and more messages!

CHALLENGE

Use a when I receive block to make the Finch move in a circle when it receives the message “Draw Circle.” Then draw multiple circles with the broadcast and wait block. What interesting patterns can you make with both squares and circles?

to see example code.

### Using Variables

You can use variables to change the behavior of your modules. For example, you can modify the “Draw Square” code to use a variable to determine the side length of the square. You can set this variable before broadcasting “Draw Square” to change the size of the square. For example, the loop on the right will draw five squares with random sizes.

CHALLENGE

Use a when I receive block to make the Finch move in a polygon when it receives the message “Draw Polygon.” Use a variable to set the number of sides of the polygon. To draw a polygon, the Finch has to turn a total of 360°, so you can divide 360° by the number of sides to find the number of degrees for each turn. What interesting patterns can you make with your polygons?

to see example code.

Create your own modules in these activities.