In the last lesson, you learned to create Boolean blocks with the Finch sensors. You used these blocks in the if, if else, and wait until blocks. In this lesson, you will learn to use one more decision block and to write programs that make more complicated decisions. As your programs become more complex, remember that you will need to spend time carefully planning what a program should do and how it should do it.
Lesson key available for teachers.
You may have used a repeat block to repeat a series of blocks some number of times. Snap! also includes the repeat until block. This block contains a space for a Boolean block. The actions inside the repeat until block will be repeated until the Boolean block is true. The repeat until block is a loop, just like the forever block. Each time through the loop, the program checks the Boolean block. If the block is false, the program repeats the blocks inside the loop. If the block is true, the program skips the blocks inside the loop and moves on to the next statement in the program.
The example program shown below will make the Finch move forward and back until you pressed the ‘q’ key. Then the Finch will stop. How is this program different from a program that uses a forever loop? What happens if you quickly press and release the ‘q’ key while the robot is moving forward? Explain why this occurs.
Modify one of your programs from the last lesson to use a repeat until loop instead of a forever loop. Describe one advantage of using a repeat until loop.
So far, you have used comparison operators (>, <, and =) to create Boolean blocks. Sometimes, you want to make more complicated decisions. For example, if you want the robot to stop when it reaches an obstacle, you probably want to check both the right and the left obstacle sensors. You can use the logic operators and, or, and not to make complex decisions. Each of these operators is a Boolean block that contains space for one or two other Boolean blocks.
Make the Finch beep while it is level. When the Finch is not level, it should stop beeping.
Write a program that makes the Finch move forward if there is nothing in its way. If the robot detects an obstacle (on either side), it should move backward. The robot should stop when you press the ‘q’ key.
The logic operators can be used to program the Finch for more complex behaviors. You have also created complex behaviors by placing if else blocks inside loops. This is called nesting. You can take nesting even further.