In Add a Light [1] you built a second LED and wrote scripts to control both of them. In Intro to Lists, you got to see how to create and access lists. In this activity, you will add a third LED, and then use lists to set up custom LED on/off sequences. You will even use a list of LED pins and a list of on times in the same for loop to fully customize the light sequence.
(1) Setup from Add a Light [1]
(1) LED - Red
(1) Resistor - 220 Ω (red-red-brown-gold)
(1) Jumper wire (black)
This script will blink the LEDs in sequence according to a list named pin_list.
# led_blink_list from microbit import * pin_list = [pin13, pin14, pin15] while True: for pin in pin_list: pin.write_digital(1) sleep(500) pin.write_digital(0)
Instead of string objects like 'green', 'yellow', and 'red', the list in this example contains microbit.pin objects: pin13, pin14, and pin15.
pin_list = [pin13, pin14, pin15] while True:
The first time through the for pin in pin_list loop, pin gets set to pin13. So, in the first pass, pin.write_digital(1) is really pin13.write_digital(1), which turns the P13 light on. Likewise, pin.write_digital(0) is really pin13.write_digital(0), which turns the P13 light off.
for pin in pin_list: pin.write_digital(1) sleep(500) pin.write_digital(0)
The second time through the loop, pin is pin14, so pin.write_digital(1) is really pin14.write_digital(1), and pin.write_digital(0) is really pin14.write_digital(0). The third time through the loop, pin is pin15. So, pin.write_digital(1) and pin.write_digital(0) are really pin15.write_digital(1) and pin15.write_digital(0).
Sometimes a little change can make a big difference in the way something behaves. For example, adding just one pin to the list will make the sequence look like the lights bounce back and forth between red and green.
Sometimes, a Python script needs a loop to access each successive item in more than one list. For example, maybe each color has a corresponding on-time in a different list. When used in place of in range(), Python’s zip() function makes this easy. Here’s an example:
# lists_with_zip from microbit import * color_list = ['green', 'yellow', 'red'] time_list = [ 1000, 200, 500] for color, time in zip(color_list, time_list): print('color:', color, '| time:', time) print('Done!')
The first time through the loop, the color variable stores 'green', while the time variable stores 1000. The second time through, color is 'yellow', and time is 200. The third time through, color is 'red' and time is 500. Each time through the loop, print('color:', color, '| time:', time) displays both variables.
Now that you’ve seen how to use a single loop with the zip() function to access related items in more than one list, let’s try it with custom LED on-times.
Create a script that turns the green on constantly for 10s, then flashes the yellow, and then finally ends with a solid red.
Solution.
name_list = [Emmanuel, Izzie, Inez, Jon] score_list = [456, 435, 482, 421] for name, score in zip(name_list, score_list):
Solution:
from microbit import* while True: pin13.write_digital(1) sleep(10000) pin13.write_digital(0) for n in range(0, 10): pin14.write_digital(1) sleep(200) pin14.write_digital(0) sleep(200) pin15.write_digital(1)
Links
[1] https://learn.parallax.com/tutorials/language/python/led-lights/add-light
[2] https://python.microbit.org/v/2