Print

JavaJava: Lesson 6 – Finch Temperature Sensor

Resources

Resources available for teachers.   

New Required Java Skills: Boolean logic, if-else statements

So far, you have learned to use the Finch outputs: the motors, beak, and buzzer. These elements enable the Finch to act on the world around it and communicate with the user. The Finch also has sensors that act as inputs. These sensors enable the Finch to collect information about the world around it. Your Finch can sense light, temperature, obstacles, and acceleration. In this lesson, you will learn to use the temperature sensor.

To measure the temperature with the Finch, you use the getTemperature() method. This method returns a value of type double that is the temperature in Celsius.

Exercise 1:

Use the code below to measure the temperature with the Finch. Then cover the temperature sensor with your finger and measure the temperature again. How does the temperature change?

Exercise 2:

The Finch measures temperature in Celsius. Write a program that tells the user the temperature in Fahrenheit.

You can use a Finch sensor to make a decision. For example, suppose you want to sound an alarm when the room gets too hot. You can use an if statement to do this. For example, the code below will play a tone with the buzzer if the temperature is greater than 22 degrees. The Boolean expression in the if statement compares the sensor value to a specific value called a threshold; in this case, the threshold is 22. It is good programming practice to use a variable for the threshold because you may need to modify the threshold if you use your program in a different environment or with a different robot. Using a variable for the threshold makes these modifications easier.

Exercise 3:

Test the code above with your robot. If you run the program with your hand on the temperature sensor, the alarm should go off. You may need to modify the threshold for your robot.

Exercise 4:

Write a program that turns the Finch’s beak blue if the temperature is below the threshold. If the temperature is above the threshold, the beak should be red.

Exercise 5:

Come up with your own way to use the temperature sensor in a program! How can you make the movement of the Finch depend on temperature?