Lesson 7 – Finch Light Sensors

Required Java Concepts

Logical operators (and/or), While loops

Teaching Materials

Get Access

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 getLeftLightSensor() and getRightLightSensor() methods. Each method returns an integer value that represents (in arbitrary units) the amount of light measured by that sensor. The integer value of each light sensor is between 0 (dark) and 255 (maximum light).

Note:

You can also use the getLightSensors() method, which returns an array with both light sensor values. We feel that this is less readable for beginning programmers, so we have chosen to use getLeftLightSensor() and getRightLightSensor() instead.

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 to turn 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 for two seconds. 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 for the Finch beak, motors, or buzzer. For example, the command below sets the brightness of the beak using the light sensors.

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 left light sensor until that sensor detects that it is dark. Here, we have chosen to use a variable to hold the value of the left sensor, rather than calling getLeftLightSensor() twice. Notice that you need to update the value of this variable each time through the loop. Otherwise, the value you print will not change, and you may get stuck in an infinite loop.

If you want the robot to move until something happens, you can use the setWheelVelocities() method without the duration parameter. This will turn on the wheels of the robot and then move immediately to the next line of code. For example, the code below will make the robot move forward until the left light sensor detects that it is dark. The stopWheels() method stops the Finch.

Exercise 5:

Try out the code above, then modify it to make the robot move forward until both light sensors detect that it is dark. Why don’t you need any code inside the while loop?

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.