Code a Twinkle Animation
The program at right is short, but makes a fun, sparkly pattern of lights that randomly turn on, then fade out. We’ll see how this program works, and how to change the color of the illuminated LEDs with small changes to the code. You can view the program in the MakeCode editor at: https://makecode.microbit.org/_MzdLV1Co5Ws5
Simple Twinkle Program
Let’s look at the different code blocks. All the code is inside a forever loop, which means the twinkling will persist as long as the micro:bit and Bright Board are powered. If we want the twinkle animation to stop after a while, we would need to move the code blocks out of the forever loop and into a different loop that repeats a finite number of times.
The first block inside the loop reads for index from 0 to 11. The for block is a loop, but unlike the forever loop, it repeats 12 times, once for each pixel in the Bright Board. The block index is a variable that increases by 1 each time the loop is run, to represent the position of each Bright Board pixel.
Inside the for loop, the conditonal code block (below) checks each pixel to see if it is already lit or if it is black. If the pixel is unlit (black), the code generates a random number which determines whether it should light up that particular pixel.
The random number that is generated determines whether the conditions inside the if block are satisfied. The random value may be between 0 and 100, but the conditions are satisfied (we’ll turn on the pixel) if the value is less than 20. That gives each pixel a 20% chance of becoming illuminated each time it is checked. If we made the value greater than 20, then the odds of lighting up each pixel would increase, and more pixels would be illuminated at a time.
The next code block, set pixel only runs if the conditions in the conditional block are met. If the pixel is unlit and the random value generated is in the correct range, the pixel will be set to the specified color. You can change the color of the twinkle pattern by changing the color in the set pixel block as shown below.
The set pixel block by itself won’t change the LEDs on the Bright Board, because it saves its changes to a buffer – a memory area in the program to store data. To move the color changes from the buffer to the display, we’ll need to call the show block. After the for code block has looped over all 12 pixels in the bright board, the show block will make any changes in pixel color and brightness visible on the display.
After the show block, there is a pause code block. This makes the program wait 50 milliseconds before moving on to the next code block and keeps the program from running too quickly.
The final instruction in the program is a fade pixels by code block which takes a value from 0-255. It will dim the brightness of all the pixels on the board. A value of 0 turns all pixels off, and a value of 255 maintains the same brightness. Values in between will fade the pixels by different amounts, which makes any illuminated pixels become dimmer and dimmer until eventually fading away. You can play with this value to make the twinkles fade out quickly or linger a bit longer. Like the set pixel block, this effect won’t be seen until the next time a show block is called.
Changing the Twinkle Colors
The colors of the twinkling LEDs are selected in the
We’ve seen that clicking on the colored oval will change the color of the LEDs. We can also create multi-colored twinkles by using the pick random block over the colored oval. It will pop into place.
Download the code and watch the twinkles become multi-colored.
Another trick to try, if you’d like to have the LED colors vary, but stay inside a color family, is to use the HSL (Hue, Saturation, Lightness) code block to choose the LED colors. Try dragging the HSL block into the color selection input of the set pixel block:
Then we can choose from a restricted range of allowed hues by adding a pick random code block.
The value of the hue input can vary from 0 to 255, and each value represents a different hue in the spectrum. Selecting a range of hues, restricts the color of the LEDs to stay within that family.