Scratch 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

Difficulty: Beginner

Approx. Time: 10 Minutes

Programming steps

1. Open the BirdBrain Robot Server.

2. Plug in your Finch and wait for the screen to say "Finch Connected".

3. Click "Open Scratch".

4. Let's make our program start when the space key is pressed. Drag a "When key pressed" block from the "Events" category to the middle of your screen.

5. 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 repeat forever loop from the "Control" category to the end of the "When key pressed" block.

6. Tell the Finch to move forward by placing a "Move Finch" block from the "More Blocks" category inside the forever loop.

7. 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.

8. 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.

9. Now we need to supply a condition that will determine if the code should be run. Move a "Finch left obstacle" block from the "More Blocks" category into the slot on the if block.

10. 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).

11. 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.

12. 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".

13. Connect the new if statement below the first if.

14. Replace the "Finch left obstacle" block with a "Finch right obstacle" block.

15. 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, click the red stop button.

Example file

example-file-2-obstacle-avoider.sb2