Lesson 2 – Exploring Sensors

Java Concepts

Data types, variables, arithmetic, printing to the screen

Teacher Materials

Get Access

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.

A picture of the Finch with the seven sensors highlighted. The wheel encoders are within the wheels, and the line tracking sensors are on the bottom of the Finch. The distance sensor is underneath the beak, and the light sensors are on the top of the Finch toward the beak. The compass, accelerometer, and buttons are all in the micro:bit in the tail of the Finch.

Creating a New File

Create a new file. Remember, every program that uses the Finch must declare the Finch object and call disconnect() at the end of the program. 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 System.out.println() function to print the value to the screen.

Exercise 1

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?

Exercise 2

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 Java 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

Data Types

You probably noticed in Exercise 2 that these Finch methods gave you different kinds of data. In Java, every value has a different data type. There are many data types in Java. These are four of the most common:

  • int – Values of type int are integers, or whole numbers.
  • double – Values of type double are decimal numbers, which are often called floating point numbers in computer science.
  • boolean – A boolean value is either true or false. For example, getButton(“A”) is true when button A is being pressed and false otherwise.
  • String – A string is a group of characters, like “hello”. In Java, strings within programs are surrounded by double quotes.

Exercise 3

What are the data types returned by all the methods in Exercise 2?

Variables

Often, you want to save a value to use it later. You can do this in Java by using a variable. A variable is a name that represents a value. To create a variable, you must first tell Java the type of data that you want to save and the name you want to give it. You use the equals sign to assign a value to the variable. For example, this line of code creates a variable named currentDistance and stores the value of the distance sensor in 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 Java, 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.

Variables and Arithmetic

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 Java to perform addition (+), subtraction (-), multiplication (*), and division (/) with numbers and variables. 

Exercise 4

What will this code do if the robot is 20 cm from the wall? Make a prediction, and then try out this code to test your prediction.

Exercise 5

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.

Exercise 6

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. What data type should mean be?

Back to Top