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.
Lesson 2 – Tri-Color LEDs
1 Tri-Color LED
Variables, for loops
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 import the Hummingbird object and the sleep() function. You must also declare the Hummingbird object and call stopAll() 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.
Note: Remember, if you are using Brython, do not include the “from time import sleep” line. The sleep() function is included automatically in Brython.
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.
Write a program to make the beak turn purple, then aqua, then yellow.
In Python, range(5) produces a list of numbers from 0 to 4: [0,1,2,3,4]. The for loop creates a variable, which is a name that represents a value. Here, the variable is named i. The for loop sets this variable equal to 0 and then 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, sets i equal to the next number in the list (1), and executes the four lines of code within the loop, blinking the LED again. This process continues until i is equal to 4, the last number in the list. When i is 4, the program executes the lines of code inside the loop and then moves on to whatever code follows the for loop.
Notice that the four lines of code inside the for loop are indented four spaces (one tab). This is how Python knows what is inside the for loop. All indented lines are within the for loop, and the next unindented line is outside the for loop. For example, this code turns the LED red after the for loop ends.
Write a program to make the tri-color LED blink red 6 times, then blue 3 times, and then green 5 times.
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.