Lesson 7 – Light Sensors

Java Concepts

Generating Random Numbers

Teacher Materials

Get Access

In Lesson 2, you looked at the values of the Finch light sensors. These two sensors detect the amount of light around the Finch. In this lesson, you will write programs that use the light sensors to control the Finch.

Picture showing the placement on the light sensors on top of the Finch. They are on the top of the Finch toward the beak. They are underneath the plastic shell of the Finch.

Using the Light Sensors

The getLight() method returns the value measured by a light sensor from 0 to 100 (no units). This method requires one parameter, a string that indicates whether you are interested in the right or left sensor.

Since getLight() returns a value between 0 and 100, you can call this method inside a Finch method that requires a parameter from 0-100. For example, you can call getLight() inside the setBeak() method. This program will continually set the brightness of the beak based on the right light sensor until you press button A.

Exercise 1

Try out the code above, and then modify it so that the Finch tail lights are also controlled by the left light sensor.

Exercise 2

In Exercise 1, the beak gets brighter when there is more light around the Finch. Modify your code from Exercise 1 so that the beak and tail lights are brighter when it is dark around the Finch.

Exercise 3

Write a program that uses the left light sensor to control the speed of the right Finch motor. The right light sensor should control the speed of the left Finch motor. This program should make your Finch follow a flashlight! The Finch should follow the flashlight until you press either button A or button B.

You can also use the light sensor to control the behavior of the Finch with an if-else statement or a while loop. For example, the code below blinks the Finch tail until the Finch detects that it is dark. The if statement above compares the light sensor value to a specific value called a threshold; in this case, the threshold is 10. It is good programming practice to use a variable for the threshold. If the amount of light in your room changes, you may need to adjust the threshold, and this is easier to do if you use a variable.

Exercise 4

Write a program that moves the Finch forward until it detects that it is dark. The Finch should stop if either light sensor detects that it is dark. You can test your program by having your Finch drive into a box.

Exercise 5

Write a program that makes your Finch afraid of the dark. If both the Finch light sensors detect that it is dark, the Finch should turn red and run around in fear. Otherwise, the Finch should turn a nice, relaxed shade of green.

micro:bit V2 Extra Challenge

If your Finch contains a micro:bit V2, you can also use a temperature sensor in the micro:bit to measure the temperature around the Finch in degrees Celsius. The getTemperature() method returns the int value of this sensor. Room temperature is about 20°C. If you don’t have a micro:bit V2 in your Finch, you will see an error when you use this method.

Write a program to alert you when the Finch is too hot. As long as the Finch is nice and cool, its lights should blink blue. When it gets too hot, the lights should turn red.

Generating Random Numbers

To make the behavior of your robot less predictable, you can incorporate some variation so that your robot does something slightly different each time you run the program. To do this, you can use a random number generator. A random number generator is a function that picks a random number each time you call it. 

To generate random numbers in Java, you must first import the Random class by placing this line of code at the beginning of your program.

Next declare an instance of the Random class in your program. 

Once you have declared the Random object, use the nextInt() method to find a random integer. This function takes one parameter that defines the range of numbers that you are interested in. For example, this line of code will generate a random number from 0 to 5. Notice that the parameter must be one more than the largest number you want to generate.

Exercise 6

What do you think this code will do? Make a hypothesis, and then try it out. Remember to import the Random class at the top of your program.

Exercise 7

Write a program that turns the tail of the Finch a random color every second as long as the Finch detects that it is dark. For an added challenge, turn each of the tail lights a different color.

Exercise 8

The Random class also includes other random number generators. For example, nextDouble() generates a random number between 0.0 and 1.0 (not including 1.0). Use this function to modify your code from Exercise 7 so that the tail stays each color for a random number of seconds between 0 and 1. Can you then make the LED stay each color for a random number of seconds between 0 and 5? 

Note: nextDouble() does not take any parameters.

Exercise 9

Make the Finch into a spinner for a game like Twister or Chutes and Ladders. Write a program that spins the Finch for a random number of seconds between 0 and 2. The Finch’s beak should be red while it is spinning and then green for one second after it stops moving. The program should repeat this cycle until the Finch detects that it is dark.