Decisions with the Finch (Scratch)
In the last lesson, you were introduced to some of the sensor blocks for the Finch. In this lesson, you will learn to use those blocks to help the Finch make decisions about what it should do.
First, consider the Finch obstacle blocks, Finch left obstacle and Finch right obstacle. These blocks are Boolean blocks. That means that the value of the block is either true or false. If an object is in front of the right obstacle sensor, then the Finch right obstacle block is true. Otherwise, it is false. Each obstacle sensor can detect objects about 1-4 inches from the sensor.
Exercise: Drag both obstacle blocks into the Scripts area. Click on each block when nothing is near the Finch. Both blocks should be false. Then try placing a cardboard box close to the Finch, first on the right side and then on the left. Verify that each obstacle block is true when that sensor is about 1-4 inches from the box.
You can use the obstacle blocks in an if then else block. The if then else block in Scratch is a decision block. It is found on the Control menu.
The if then else block has spaces in the middle that can hold other Scratch blocks. In addition, the if then else block contains a hexagonal space. This space requires a Boolean block. If the Boolean block is true, the program runs the blocks in the top of the if then else. Otherwise, the program runs the blocks in the bottom of the if then else. For example, the program below turns the Finch’s beak red when an object is close to the left sensor. Otherwise, the beak is green.
Note for Chromebook Users: ScratchX does not include Boolean blocks, but the Finch obstacle blocks still have a value of true or false. You will need to use an equality operator (Operators menu) to check whether the obstacle block is true, as shown in the example below.
Exercise: Try out the script shown above. When you press the spacebar, it will check the left obstacle sensor and turn the beak red or green.
The script above makes a decision each time you press the space bar. You may also want to repeat this decision over and over. Then the beak will turn red whenever something gets too close to the sensor. To do this, place the if then else block inside a forever loop.
Exercise: Write a program that will make the Finch turn right when the left obstacle sensor detects an object. Otherwise, the Finch should move straight ahead. Large, lightly-colored objects (like cardboard boxes) make the best obstacles.
The obstacle blocks are the only Finch blocks that are Boolean blocks. However, you can use the other sensor blocks to create Boolean blocks. You can do this using the three blocks shown below, which are found on the Operators menu. These are called comparison operators, or comparison blocks.
Create the Boolean block shown below. This block is true if the value of the left light sensor is less than 10, and false otherwise. The value that a Boolean block uses to make a decision is called the threshold. In this case, the threshold is 10.
Use the Finch left light block to measure the amount of light in your room. Then measure the value of the light sensor when you cover it with your hand. The average of these two values is a good threshold for the light sensor.
Exercise: What do you think the script below will do? After you have made a hypothesis, test it out.
You should always use the < block or the > block with the Finch light, acceleration, and temperature blocks. The = block does not work well with these sensor blocks because the value of a sensor is rarely equal to a particular number. For example, the value of the light sensor might change from 52 to 49 to 51. A block checking whether the value is equal to 50 would be false for all of these values, but a block checking whether the value is greater than 50 would detect the changes.
Exercise: Write a program that makes the Finch buzz when the user turns the Finch upside down. Otherwise, the Finch should do nothing. This program should use one of the following blocks: Finch X acceleration, Finch Y acceleration, or Finch Z acceleration.
Orientation of the Finch
In the last exercise, you had to use one of the acceleration blocks. There is also a Finch block called Finch Orientation. This block has one of seven values: Level, Beak_Up, Beak_Down, Left_Wing_Down, Right_Wing_Down, Upside_Down, and In_Between. Tilt the Finch in different directions and click on the Finch Orientation block to see the different values.
You can use the = block to check if the Finch Orientation block has a particular value. In fact, you must use the = block because the other blocks don’t make sense. What would it mean to be greater than “Beak_Up”? The Boolean block below is true when the Finch’s left wing is down and false otherwise. You must include the underscores for the code to work.
Exercise: Modify your program from the last exercise to use a Finch Orientation block to detect when the Finch is upside down. What changes do you need to make?
So far, you have only used Boolean blocks inside if and if then else blocks. You can also use Boolean blocks inside a wait until block. This block stops the program until the Boolean statement in the wait until block is true. For example, the program below turns the Finch’s beak red and then waits until the left obstacle sensor detects an object. Then it turns the beak off.
Exercise: What will happen if you run this script when there is already an object next to the left obstacle sensor? Try it and find out!
Exercise: Write a program that makes the robot move in a large circle until it finds an area that is dark.
As you start to write programs that make decisions, you will notice that you need to think carefully before you begin to write a program. Planning a program carefully can save you from spending a lot of time debugging. Experienced programmers often draw a diagram (called a flowchart) of how a program will work or write out the steps that the robot will follow (this is called pseudocode).