Throughout these lessons, you have been using an object of type Finch. The Finch is defined in Java as a class, which is a programming structure that includes all the methods and variables that relate to a single object. An object can be something abstract like a database, but in the case of the Finch, it is a physical object!
Lesson 14 – Multiple Finches
Encapsulating methods and variables in classes is called object-oriented programming. A class enables the user to use the class’s methods without having to understand how they are implemented. This is an example of abstraction in computer science. This means that the details are handled once in a piece of reusable code. Others can then use that code to solve new problems without worrying about those details. Methods are also an example of abstraction.
Connecting Multiple Finches
You can connect up to three Finches (or Hummingbird Bits or micro:bits) with the BlueBird Connector, and then use the Finch class to create multiple independent Finch objects! This means that you can program the robots to interact with one another. In this module you will learn to use two Finches together.
Using Multiple Finches
In all the previous lessons, you have been declaring just one object of type Finch. If you connect two Finches in the BlueBird Connector, you can declare two Finch objects. When you have more than one Finch object, you must declare each one by calling Finch() with a parameter that is the letter (“A”, “B”, or “C”) that identifies that Finch in BlueBird Connector.
Write a program to make two Finches do some synchronized dancing! The robots should move, light up, and make noise.
Write a program that uses one Finch to control the movement of the other Finch. This program should use the getOrientation(), setMove(), and setTurn() methods.
- When Finch #1 is “Beak down”, Finch #2 should move forward.
- When Finch #1 is “Beak up”, Finch #2 should move backward.
- When Finch #1 is “Left wing down”, Finch #2 should turn left.
- When Finch #1 is “Right wing down”, Finch #2 should turn right.
Write a program to use one Finch’s accelerometer to directly control the speed of the other Finch’s motors. For example, as you tilt one Finch more to the left, the other Finch should turn left faster. When the beak is tilted further down, the Finch should move forward faster.
Note: Remember, getAcceleration() returns an array, so you can store the acceleration measurements in individual variables, as in Lesson 8, or you can store them in an array named acceleration and access the elements as acceleration, acceleration, and acceleration.
What can you think to do with two Finches? Use all that you have learned about Java to come up with a creative program that makes two Finches interact in an interesting way!