Required Python Skills: Data types, 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 its environment. Your Finch can sense light, temperature, obstacles, and acceleration. In this lesson, you will learn to use the temperature sensor.
Use the code above 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?
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 23 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 23. 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 Finch 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?
Teacher Note: If other Finch methods are used before the temperature() method is called, then the first temperature measurement may not be valid (for instance, you may get a negative number). In this situation, you should simply call the temperature() method a second time to clear out the inaccurate number and get a valid measurement.