# Scratch Project 4: Light Follower

Description: Program the Finch to follow a flashlight.

Programming Concepts: Using math blocks and sensor readings to affect the Finch's behavior

Difficulty: Intermediate

Approx. Time: 20 Minutes

## Programming steps

1. We will be continuously adjusting the Finch's speed to help it follow a light source. Let's start with a "When key pressed" block and a forever loop.

The first thing the Finch should determine is which direction it needs to turn. It can do this by determining the difference between the readings of the left and right light sensors. The larger the difference between the two readings, the faster the Finch should turn.

2. Make a new variable to keep track of the difference. Click the "Data" category and select "Make a Variable".

3. Name the Variable "difference".

4. In the forever loop, set the difference variable to the value of the right light sensor minus the value of the left light sensor. The "-" block is in the "Operators" category.

5. Now connect a "Move Finch" block to the set block.

The difference variable will be positive when the light is pointed at the Finch's right light sensor. Therefore, when the value is positive, the Finch should turn right. This means turning the right wheel backwards and left wheel forwards.

6. set the speed of the left wheel to the value of difference. Set the right wheel to 0 minus difference.

7. Run the program by pressing the spacebar and the Finch should turn to face the light source. However, it will turn very slowly and if your flashlight is weak, it might not turn at all.

8. To make the Finch turn faster, we need to amplify the difference between the right and left sensors. Add a multiply block to the "set difference to" block and multiply the value of the subtraction by 2 or 3.

9. Run the code and the Finch should quickly turn to face the light.

10. Now let's make the Finch move forward. To do this, we can add a little speed to the right and left wheels. Connect a "+" block to the left wheel slot and set it to 50 + difference. Make the right wheel slot 50 - difference.

11. Run the code and the Finch should follow your flashlight. If the Finch has trouble following your flashlight, try decreasing the numbers from 50 to 30. If the Finch turns too quickly and keeps overshooting, decrease the multiplier from 2 to 1.5. It also helps if the flashlight is very bright.

## Example file

example-file-4-light-follower.sb2