How it Works

How led_blink_list Works

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).  


Try This: Up/Down Sequence

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.

  • Modify the script as shown below.
  • Save your work as led_blink_try_this.
  • Flash it into the micro:bit.
  • Verify that the sequence is now green, yellow, red, yellow, green, yellow, red, yellow, green, ...