Snap! Project 2: Obstacle Avoider
Description: Make the Finch automatically detect and drive around obstacles.
Programming Concepts: Using sensor readings and if statements to affect the Finch's behavior
Approx. Time: 10 Minutes
1. Open the BirdBrain Robot Server.
2. Plug in your Finch and wait for the screen to say "Finch Connected".
3. Click "Open Snap!".
4. Select level 4.
5. A sample project will load. After reading the note, delete both the note and the blocks by dragging them to the sidebar on the left of the screen.
Before we make the Finch move, we should have a way of stopping the robot if we need to. Note that clicking the stop button does not stop the Finch.
6. We will tell the Finch to stop when the Q key (as in "quit") is pressed. Drag a "When key pressed" block from the "Control" category to the middle of your screen and set the key to Q.
7. When Q is pressed, the program should stop everything that is running and then stop the Finch. Attach a "stop all but this script" block from the bottom of the "Control" category to the "When Q key pressed" block.
8. Finally, stop the Finch using a "Stop Finch" block from the "Motion" category.
9. Now we can move on to the main part of our program. Let's make our program start when the space key is pressed. Drag a "When key pressed" block from the "Control" category to an empty part of the screen.
10. In this program, the Finch will continuously scan for obstacles and drive around them. Because this check will be occurring repeatedly, we will need to use a repeat loop. Connect a forever loop from the "Control" category to the end of the "When key pressed" block.
11. Tell the Finch to move forward by placing a "Move Finch" block from the "Motion" category inside the forever loop.
12. We don't want the Finch to move too fast, or it might miss an obstacle. Set the motor power of both wheels to 50.
13. When the Finch detects an obstacle, it should perform a series of actions to avoid that obstacle. This code should only be run if an obstacle sensor sees something in its way. To make a piece of code that is only run when a certain condition is true, we can use an if statement. Attach an if statement from the "Control" category below the move block.
14. Now we need to supply a condition that will determine if the code should be run. Move a "Finch left obstacle" block from the "Sensing" category into the slot on the if block.
15. If an obstacle is detected on the Finch's left, the Finch should turn and drive around it. Add a move block inside the "if" block and make it turn the left wheel backward (-50) and the right wheel forward (50).
16. To give the Finch time to turn away from the object, add a "wait 1 secs" block (from the "Control" category) after the move block.
17. The Finch will now avoid obstacles on its left. Now let's do the same for obstacles on the Finch's right. Duplicate the if statement by right-clicking it and selecting "duplicate".
18. Connect the new if statement below the first if.
19. Replace the "Finch left obstacle" block with a "Finch right obstacle" block.
20. Switch the direction of the motors.
Run the program and the Finch should avoid obstacles placed in front of it. Note that the obstacle sensors on the Finch are best able to detect large, white or lightly-colored obstacles like paper or cardboard boxes. To stop the program, press Q.
This file was created with the Birdbrain Robot Server. Use this utility to convert it to a form that can be used with the Chrome Finch connection app.