RGB LEDs and lists
RGB LEDand lists
Sometimes it is just really hard to make decisions. And then there is doubt. What should we eat for breakfast and dinner? Should we eat strawberry or raspberry ice cream for dessert? And of course, should we use the green or the blue LED? But the red one is also cool…
We may not have a solution for all of the problems (Though you can likely create a program that helps you decide!), we do have a solution for the last problem involves using the RGB LED. You can think of this component as a super LED because with this one LED you can show the colors green, red and blue. Every color has its own leg at the led, with the longest leg being for ground. Please keep that in mind.
Because the RGB Led is quite similar to a normal LED you already know how to use it. We encourage you to try it out by yourself first. Even if it might take a while to get everything working properly, keep working on it by yourself. You can use a lot of the code we wrote in the last chapters as a reference.
For the circuit you can use the image above. If you don’t know how to get any further just follow the link at the bottom or the code on the next part ;)
After doing some brain exercise and thinking of your creative solution it is time to learn something new again!
We have already discussed quite a bit regarding variables. A variable is like a “box”, in which you can save anything you want. Just like boxes have their limitations however, so do these variables, especially when we want to save complex or large amounts of information. Yes we could create a single variable for every bit of information, but sometimes we just really want to save a lot of data in a single or few locations. For this we have what is called a list.
A list is quite similar to a variable, except you have the ability to save more than one value. Imagine you want to program a friendship book that includes an overview and information about all your friends. If you wanted to this with normal variables it would look like this:
name = "octopus" favorite Dish = "burgers" hobby = "skydiving"
And you could reiterate this information for every friend you have. As you can imagine that would be a lot of work. You would need to give new names for every variable so that you don’t overwrite them. This brings us to the use of this fancy solution called lists.
Like the Variable, the list has a name, but you can save different values between square brackets
 and separate them with a comma
octopus = [2, "burgers", "skydiving"]
For simplicity we use the name of the friend as the name of the list. In the squared brackets we save everything important about that friend like, age, favorite dish, and hobby. This is way more practical than using single variables.
In addition, accessing one of this values is actually quite simple. Every value in this list has a number. The first value is 0, the second is 1 and so on. To access one of the values you simply write the name of list and then in squared brackets the number of the value’s position(Beginning with 0). So:
What would be the output? Exactly, the favorite dish, or in this case: burgers.
This is the age so in this case the output would be 2.
You can also print the whole list by just not adding the squared brackets.
Essentially, you can imagine a list as being a box within a box. You can save different types of value in a list including strings and integers. Now it is your turn! Create a list with your name and then save the values of your age, your favorite dish and your hobby. If you change your mind whilst writing the program, you can always change the value in a list. This can be done with the following coding:
norbert = "raspberry cake"
It is so easy! So go on and change some values.
RGB-LED and lists united
As always, we have prepared a practical example. And of course wildly blinking examples are the best examples! That’s why we are building party lights with the RGB LED. Connect the RGB Led if you haven’t already. After this create a new file with the exciting name rgb-party.py and write in it the following code:
import RPi.GPIO as gpio import time import random gpio.setmode(gpio.BCM) ledPin = [18, 23, 24] gpio.setup(ledPin, gpio.OUT) gpio.setup(ledPin, gpio.OUT) gpio.setup(ledPin, gpio.OUT) while True: randomPin = random.randint(0,2) gpio.output(ledPin[randomPin], gpio.HIGH) time.sleep(1) gpio.output(ledPin[randomPin], gpio.LOW)
All of the new stuff is in line 6. In this line we create a list with the name ledPin and enter numbers in it. These numbers are for the GPIO-Pins which we have connected to the RGB LED. Sorted and saved in a list.
In the following 3 lines we are set every GPIO Pin as an output because we want to use the GPIO Pins for the LED. To do this we use the technique we have learned for accessing the specific parts of the list with the content of the pins.
In line 11 you find something new. Here we create a variable called randomPin and assign it the value
random.randint(). This function is quit simple and needs two parameters to work properly. Like below:
This will create an output of a random number between 0 and 5. Because we only need a random number between 0 and 2 for our list, we only use those values in the function within our program. Always keep in mind that you start counting with a 0 in a list.
In line 10 we use another handy trick. With
while True: we create a while loop which is always running. WIth this, you only can close the program be shutting down the Pi or by pressing
ctrl + c. Until then, the while loop will always be running. At every start a new random number between 0 and 2 will continue being generated and used to pick a gpio and make this color shine for a second with the RGB LED. This application is an easy way for us to generate a party light.