Lesson 3 – Controlling the Lights

Java Concepts

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.

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 pause() method. The pause() 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. This code turns the beak red, waits one second, and then turns the 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”. 

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.

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. To get information from the user, you must declare an object of type Scanner. This requires that you import java.util.Scanner at the top of your program, as shown in this example. The Scanner method nextInt() waits for the user to enter an integer with the keyboard. When the user hits the enter key, the method returns their response to the program. The code below stores the response in the variable userResponse. Notice that the code closes the Scanner at the end of the program. Java may give you a warning if you don’t do this.

Exercise 5

Try out the sample program above. What happens if you enter a letter instead of a number? What happens if you enter a number that is not between 1 and 4?

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.

Back to Top