Lesson 14 – Multiple Finches

Python Concepts

Object-oriented programming

Teacher Materials

Get Access

Throughout these lessons, you have been using an object of type Finch. The Finch is defined in Python as a class, which is a programming structure that includes all the functions 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!

Encapsulating functions and variables in classes is called object-oriented programming. When it is inside a class, a function is called a method. A class enables a programmer 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. Functions 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 or BirdBrain Brython, 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.

The BlueBird Connector will show all the Finches that are available. To connect two Finches, just click on both of them.

This picture shows the BlueBird Connector when two Finches are available.

Both Finches should show up in the Connected section. One will be labeled with an ‘A’ and one with a ‘B.’

This picture shows the BlueBird Connector with two Finches connected.

To connect a second Finch with BirdBrain Brython, click the expand button. Then click Find Robots and select the second Finch.

This picture shows the location of the expand button in BirdBrain Brython.

Both Finches should show up in the Connected section. One will be labeled with an ‘A’ and one with a ‘B.’

This picture shows BirdBrain Brython with two Finches connected.

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.

bird1 = Finch('A') # Declare first Finch object 
bird2 = Finch('B') # Declare second Finch object

Both of the objects that you declare can access all the Finch methods. For example, this code calls the setBeak() method for both objects to make the two beaks blink together.

for i in range(10): # Synchronized blinking 
bird1.setBeak(0,100,0) 
bird2.setBeak(0,100,0) 
sleep(1) 
bird1.setBeak(0,100,100) 
bird2.setBeak(0,100,100) 
sleep(1) 
bird1.stopAll() 
bird2.stopAll()

Exercise 1

Write a program to make two Finches do some synchronized dancing! The robots should move, light up, and make noise.

You can also use the sensors on one Finch to control the other Finch. For example, this code turns the tail of one Finch on and off using the buttons of the second Finch.

while bird1.getOrientation() == "Level": 
if bird2.getButton('A'): 
bird1.setTail("all",0,0,100) 
if bird2.getButton('B'): 
bird1.setTail("all",0,0,0) 
bird1.stopAll() 
bird2.stopAll()

Exercise 2

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.

Exercise 3

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: The getAcceleration() method returns a list, so you can store the acceleration measurements in individual variables, as in Lesson 8, or you can store them in a list named acceleration and access the elements as acceleration[0], acceleration[1], and acceleration[2].

Exercise 4

What can you think to do with two Finches? Use all that you have learned about Python to come up with a creative program that makes two Finches interact in an interesting way!