Lesson 3 – Controlling the Lights

Python Concepts

Pausing a program, user input

Teacher Materials

Get Access

In this lesson, you will learn to use more of the Finch outputs. You will learn to control the lights in the Finch’s beak and tail. Each of the lights in the beak and tail actually has three tiny light elements inside it. One is red, one is green, and one is blue. This is important for programming the lights.

Controlling the Beak Light

To set the color of the beak, use the setBeak() method. This method requires three parameters that set the values of three colors. By combining different amounts of these three colors, you can also create other colors. For example, red light and blue light will combine to make purple.

bird.setBeak(100,0,0) 
 The setBeak() function has three parameters. All three must be between 0 and 100. The first parameter is the amount of red light, the second is the amount of green light, and the third is the amount of blue light.

The setBeak() function sets the color of the beak and then moves on immediately to the next line in the program. To pause on each color, use the sleep() function. The sleep() function takes a single parameter that is a number of seconds for the program to pause; this can be a whole number or a decimal number such as 0.5. The sleep() function must be imported from the time library. The code below shows how to import the sleep() function. This program turns the beak red, waits one second, and then turns the beak off.

Note: If you are using Brython, do not include the “from time import sleep” line. The sleep() function is included automatically in Brython.

 

from BirdBrain import Finch # Import Finch library 
from time import sleep # Import sleep() function 
bird = Finch() # Declare Finch object 
bird.setBeak(100,0,0) # Beak red 
sleep(1) # Wait 1 second 
bird.setBeak(0,0,0) # Beak off

Exercise 1

Try out the code shown above. Experiment with the red, green, and blue parameters to see what colors you can make.

Exercise 2

Write a program to make the beak turn purple, then aqua, then yellow.

Controlling the Tail Lights

The Finch’s tail contains four lights. To control these, use the setTail() method. This method is similar to setBeak() except that it has one additional parameter. The port parameter controls which light is turned on. This parameter should be set to either an integer from 1 to 4 or the string value “all”.

bird.setTail(1,0,100,0) 
 The setTail() function has four parameters. The first is the light that should be turned on. It can be 1, 2, 3, 4, or "all". Parameters 2 to 4 must be between 0 and 100. The second parameter is the amount of red light, the third is the amount of green light, and the fourth is the amount of blue light.

Exercise 3

What do you think this code will do? Make a prediction and then try it out. Notice that instead of turning the lights off individually, you can use the stopAll() method to turn off all the Finch outputs. It is a good idea to get into the habit of calling this at the end of each program.

bird.setTail(1,0,0,100) 
bird.setTail(4,0,0,100) 
sleep(2) 
bird.stopAll() # All outputs off

Exercise 4

Write a program that shows a rainbow of colors on the Finch’s beak and tail.

User Input

You can write programs to let a user control the Finch. You can use the input() function to ask the user a question and wait for a response. When the user hits the enter key, the function returns their response. The code below stores the response in the variable userResponse.

userResponse = input("Which tail light (1-4) should be red? ")

The value returned by input() is a string. In this case, we know that the user entered a number, so we can use the int() function to convert userResponse to a variable number of type int.

number = int(userResponse)

Exercise 5

Try out this sample code. What happens if you use userResponse directly in setTail() instead of converting it to number?

userResponse = input("Which tail light (1-4) should be red? ") # Get user input 
number = int(userResponse) # Convert to int 
bird.setTail(number,100,0,0) 
sleep(1) 
bird.stopAll()

Exercise 6

Write a program that asks the user for the amount (0-100) of red, green, and blue in a color. The beak and tail should stay that color for three seconds.

Exercise 7

Write a program that moves the Finch in a square. The user should choose the side length of the square, and the Finch tail should be a different color for each side of the square.