Program

Lesson 6 – Finch Light Sensors

Required Python Concepts

Logical operators (and/or), While loops

Teaching Materials

The Finch has two light sensors. These sensors enable you to detect whether the area around the robot is dark or bright. For example, you can write a program to make the robot move forward until it comes to a dark area. To measure the values of the light sensors, you use the light() method. This method returns a tuple object with two values of type float. The first value is for the left light sensor, and the second is for the right light sensor. Both values are between 0.0 (dark) and 1.0 (maximum light). In the line of code below, these two values are stored in variables named leftLight and rightLight . Exercise 1:

Print the values of the light sensors to the screen. Are they the same or different? How can you change the values of the light sensors?

Exercise 2:

To make a decision with the light sensor, you need to find a threshold. Write a program to find a good threshold. Your program should do the following:

• Prompt the user to place the Finch in a lighted area
• Measure the values of the right and left light sensors
• Find the average of these values and store it in a variable called lightAverage
• Prompt the user to place the Finch in a dark area
• Measure the values of the right and left light sensors
• Find the average of these values and store it in a variable called darkAverage
• Calculate the threshold by finding a value halfway between lightAverage and darkAverage
• Report the threshold to the user

Exercise 3:

Use the threshold you found in the previous exercise to write a program that turns on the Finch’s beak when it is dark. If either light sensor detects that it is dark, the beak should turn on for two seconds, and its color should be random. Otherwise, the beak should be off. Why should you store your threshold in a variable?

You can use the light sensor value to make a decision using an if-else statement, but you can also use the light sensor value to set the value of the Finch beak, motors, or buzzer. For example, the command below sets the brightness of the beak using the light sensors. The value of each light sensor is in the range 0-1, but the led() method expects parameters from 0-255, so we multiply each light sensor value by 255. Exercise 4:

When you execute the line of code above, the Finch’s beak will be bright when there is a lot of light around it. If you run it when the Finch is in a dark area, the beak will be dim. How can you modify this line of code so that the beak of the Finch is bright when you run the program in a dark area?

You can also use the Finch sensors within the Boolean condition of a while loop. For example, the loop below will print the value of the right light sensor until that sensor detects that it is dark. Notice that you need to update the values of the light sensor variables each time through the loop. Otherwise, the value you print will not change, and you may get stuck in an infinite loop. Note:

Since finch.light() returns a tuple, you could use square brackets to access the values in the tuple, rather than saving the values into two variables. As an example, the code below is equivalent to while loop shown above. Accessing the tuple values directly shortens the code, but we feel it is less readable for beginning programmers. For this reason, we will typically store the light sensor values in two variables rather than accessing the tuple values with square brackets. Exercise 5:

Write a program to make the robot move forward until both light sensors detect that it is dark.

Exercise 6:

Now it is time to use both the temperature and light sensors! Write a program that repeatedly sets the beak brightness based on the light sensor values as long as the temperature of the Finch is below some temperature threshold.