Working with colors + random

Working with colors + random Titelbild

Working With Colors

When we are working with the Pixel LEDs on the shield we can mix most colors. You can also use what is called a Color Picker to help find the values of the colors you would like to use. You can find many on the internet to assist you in finding your preferred colors. We have also prepared a table of colors which you can use to mix your color. This will always set the first led on the Unicorn to a certain color.

.setPixel(0,55,0,0) red
.setPixel(0,255,180,0) orange
.setPixel(0,255,255,0) yellow
setPixel(0,0,255,0) green
.setPixel(0,0,255,155) turquoise
setPixel(0,0,0,255) blue
.setPixel(0,255,0,255) purple
.setPixel(0,255,255,255) white
.setPixel(0,0,0,0) black/turned off

It is important that you always use the .show() function after using the .setPixel() and the .setAll() functions. If you forget it, the colors on the shield won't change. You can also use the .clear() function instead of the .setAll(0,0,0) function because black and turned off are the same for our Pixel LEDs.

Now it is your turn: Can you create your favorite color on the Pixel LEDs? Observe what happens when you alter the numbers by just a little bit?

The Coincidence

Generally, when writing a program we want to be certain that 3+3 is always 6 and not sometimes 7. It would be quite awkward if we used print("Hello") and you see "Bye Bye" in your terminal. In contrast however, it is sometimes really practical if we use this coincidence and not always get the same return value when we are calling a function. To do this, we can use the random library and the randint() function which is included in it.

import unicornshield as unicorn
from time import sleep
from random import randint

while True:

After executing the program you should see that every four seconds a different color on your Unicorn Shield is displayed on the Pixels. As always, you can stop the program with Ctrl + C.

If we give our program a closer look, we see that it does not really look so different from the other programs. In line 3 we are importing the random library. Because we only need one function from this library we are only including this one function in the import. In line 5 we are creating a while loop which is continuously running and we begin this loop in line 6 with the .setAll() function. To give you a better overview, in lines 7, 8 and 9 we only use one parameter in each line of code. You could also put all of this on the same line.

The function .setAll() takes 3 parameters. One parameter for every RGB color corresponded with a value between 0 and 255. Instead of giving a fixed value we use the following code: randint(0,255). With this function we can generate random numbers for the color values. The first parameter is the minimum value and the second sets the maximum value. This function basically says: "Give me a random number with a return value between 0 and 255". We need to call this function three times, once for each color value. You have probably already noticed that the parameter of one color can be the return value of another function. You could do that over and over again. In line 10 we call the .show() function so that the beautiful color we are creating is displayed on the Pixel. In the next line we use the sleep() function so that we can have time to enjoy the color.

It is your turn: What happens if you change the time for the delay?

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!