Required Java Skills: Boolean logic, if-else statements
Sample code available for educators. Get Access
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.
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.
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.
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.
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?