Lesson 2 – Tri-Color LEDs

Hummingbird Components

1 Tri-Color LED

Java Concepts

Variables, for loops

Teacher Materials

Get Access

In this lesson, you will learn to use the tri-color LEDs. A tri-color LED is a small light with four wires. The tri-color LED actually has three tiny lights inside it. One is red, one is green, and one is blue. You can combine different amounts of red, green, and blue light to make different colors.

If you want to complete this lesson with a robot, try building an animal or a jitterbug.

Use the terminal tool to plug a tri-color LED into TRI-COLOR port 1 on the Bit. The red wire connects to ‘R,” the green to ‘G,’, the blue to ‘B,’ and the black to ‘-.’

Open a new file. Remember, every program that uses the Hummingbird must declare the Hummingbird object and call stopAll() and disconnect() at the end of the program. This means that every program must use the template shown here. All the code that you write should go in the space marked by a “Write code here!” comment. For the remainder of these lessons, we will assume that your program has these elements, and we will not show them in the sample code.

Use the setTriLED() method to control the tri-color LED. This method requires four parameters. The first parameter is the number of the Hummingbird port to which the LED is attached. This will be 1 or 2. The red parameter controls the amount of red light from 0 (none) to 100 (maximum brightness). The green and blue parameters control the amount of green and blue light, respectively, from 0 to 100. 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.

Exercise 1

Try out this sample code.The tri-color LED should turn red, then green, then blue.

Exercise 2

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

To make the tri-color LED change color multiple times, you can use a for loop. A loop is a structure that repeats actions within a program. A for loop repeats whatever is inside it a certain number of times. For example, this code turns the tri-color LED on and off 5 times.

The for loop creates a variable, which is a name that represents a value. Here, the variable is an integer named i. The for loop sets this variable equal to 0 and checks whether i is less than 5. If it is, then the program executes the four lines of code within the loop. These four lines of code make the LED blink green once. Then the program goes back to the top of the for loop and increases the value of i  by 1 (i++). It checks again whether i is less than 5 and executes the four lines of code within the loop, blinking the LED again. This process continues until i is equal to 5. When i is equal to 5, the program checks i < 5 and and finds that it is false. The loop ends; the program skips the lines of code inside the loop and moves on to whatever code follows the for loop.

Notice that the four lines of code inside the for loop are contained within curly braces. This is how Java knows what is inside the for loop. All the lines within the braces are inside the for loop, and the next line outside the braces is outside the for loop. For example, this code turns the LED red after the for loop ends.

Exercise 3

Try out this code, then modify the code to make the LED blink green 10 times and then turn red..

Exercise 4

Write a program to make the tri-color LED blink red 6 times, then blue 3 times, then green 5 times.

Exercise 5

Write a program to make  the tri-color LED blink from purple to green slowly 5 times and then blink from aqua to red quickly 10 times.

Extra Challenge

What do you think this code will do? Make a hypothesis, and then test it out. How can you modify this program to do something different?

Back to Top