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.
Lesson 3 – Controlling the Lights
Controlling the Beak 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 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.
Try out the code shown above. Experiment with the red, green, and blue parameters to see what colors you can make.
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”.
Write a program that shows a rainbow of colors on the Finch’s beak and tail.
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.
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?
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.
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.