In the last lesson, you learned to make the Finch move and turn. This uses the motors in the wheels, which are Finch outputs. Outputs are ways that the Finch can act on the world or communicate with the user; they include the motors, lights, and buzzer. The Finch also has inputs called sensors. The sensors collect data about the environment around the Finch and provide this data to your program. In this lesson, you will explore some of the kinds of data that you can collect with the Finch sensors.
Lesson 2 – Exploring Sensors
Data types, variables, arithmetic, printing to the screen
Creating a New File
Open a new file. Remember, every program that uses the Finch must import the Finch library and declare a Finch object. This means that every program must use the template shown here. All the code that you write should go in the space marked by the “Write code here!” comment. For the remainder of these lessons, we will assume that your program has these elements, and we will not show them in the sample code.
Printing Sensor Values
The distance sensor measures the distance between the Finch and the nearest obstacle. You can read the value of the distance sensor with the getDistance() function. This method returns a numerical value that is the distance in centimeters. In programming, to return a value means to provide that value to the program when a function or method is called.
To see the value of the distance sensor, you can use the print() function to print the value to the screen.
Try out this sample code. It prints the value of the distance sensor with a label. Run your program with the Finch at different distances from the wall or an obstacle (the distance sensor must be pointed at the obstacle to see it). What is the smallest distance you can measure? What is the largest?
Print the values of some of the other Finch sensors using the methods below. Each value should have a label. Use your program to explore these sensors. How can you change their values? Don’t worry if you don’t understand these methods completely. You will learn more about the sensors in future lessons; you can also consult the Finch Python Library Description for a full list of sensor methods.
- getLight(‘R’) – returns the value of the right light sensor
- getLight(‘L’) – returns the value of the left light sensor
- getButton(‘A’) – returns the value of button A on the micro:bit
- getOrientation() – returns the orientation of the Finch
- getEncoder(‘R’) – returns the number of rotations that the right wheel has turned
You probably noticed in Exercise 2 that these Finch methods gave you different kinds of data. In Python, every value has a different data type. There are four common data types in Python:
- int – Values of type int are integers, or whole numbers.
- float – Values of type float are decimal numbers, which are often called floating point numbers in computer science.
- bool – Type bool is short for Boolean. A Boolean value is either True or False. For example, getButton(‘A’) is True when button A is being pressed and False otherwise.
- str – Values of type str are strings. A string is a group of characters, like “hello”. In Python, strings are surrounded by single or double quotes.
You can use the type() function to find the data type of a value. For example, this line of code prints <class ‘int’> to the screen. That tells us that the value returned by getDistance() is type int.
Find the data types returned by all the methods in Exercise 2.
Variables and Arithmetic
Often, you want to save a value to use it later. You can do this in Python by using a variable. A variable is a name that represents a value. To create a variable, choose a name and use the equals sign to assign a value to the variable. For example, this line of code creates a variable named currentDistance and assigns the value of the distance sensor to it.
To assign a value to a variable, the name of the variable must be on the left of the equals sign. Note that this is a little different from how the equals sign is used in math. In math, the equals sign indicates that two quantities are equal, and it doesn’t matter which one is on the left side of the equals sign. In Python, the equals sign is called the assignment operator. It means that the value on the right side of the equals sign is being assigned the name on the left side. The first time you assign a value to a variable is called initializing or declaring a variable.
After a variable has been initialized, you can use it later in the program. Every time the program sees the name of the variable, it will substitute the value of that variable. You can use arithmetic operators in Python to perform addition (+), subtraction (-), multiplication (*), and division (/) with numbers and variables.
Assign the values of the left and right light sensors to variables. Then use these variables to calculate the difference between the two sensors. Store that in a variable called difference and print the value of difference to the screen.
Extend your program from Exercise 5 so that it also calculates the mean of the left and right light sensors. Store that in a variable called mean and print the value of mean to the screen.
Hint: You may need to use parentheses as well as arithmetic operators.