Solutions

Questions

  1. Answer: pushbuttons
  2. Answer: The if statement is inside another if loop.

 

Exercises

  1. Answer: There will be an error and the script will not run if lines are not properly indented.
  2. Answer: The index value can be placed inside a loop with a + / - used to increase or decrease the index value.  Incrementally changing the index value will move through the list items assigned to the corresponding index values.
  3. Answer: The pushbutton states are read, 0 or 1, and that information is then passed to other components via write statements to dictate their behavior. Ex: read_digital and write_digital.

 

Projects

1. Solution.

# rotate_leds_leds_off

from microbit import *

pin = [pin13, pin14, pin15]
time = [200, 200, 200]
index = 0
state9 = 0
state6 = 0

display.off()
pin9.set_pull(pin9.NO_PULL)
pin6.set_pull(pin6.NO_PULL)

while True:
    state9 = pin9.read_digital()
    state6 = pin6.read_digital()

    if state9 is 1:
        index = index + 1
        if index > 2:
            index = 0
        pin[index].write_digital(1)
        sleep(time[index])
        pin[index].write_digital(0)
        
    if state6 is 1:
        index = index - 1
        if index < 0:
            index = 2
        pin[index].write_digital(1)
        sleep(time[index])
        pin[index].write_digital(0)

 

2. Solution.

#project_solution

from microbit import *

pin = [pin13, pin14, pin15]
time = [1500, 1000, 500]
index = 0
state9 = 0
state6 = 0

display.off()
pin9.set_pull(pin9.NO_PULL)
pin6.set_pull(pin6.NO_PULL)

while True:
    state9 = pin9.read_digital()
    state6 = pin6.read_digital()

    if state9 is 1:
        index = index + 1
        if index > 2:
            index = 0
        pin[index].write_digital(1)
        sleep(time[index])
        pin[index].write_digital(0)
        sleep(time[index])
        
    if state6 is 1:
        index = index - 1
        if index < 0:
            index = 2

        pin[index].write_digital(1)
        sleep(time[index])
        pin[index].write_digital(0)
        sleep(time[index])


3: Solution.

# buttons_with_leds_and_zip

from microbit import *

pin_list = [pin13, pin14, pin15]
time_list = [500, 1000, 1500]
state9 = 0

display.off()
pin9.set_pull(pin9.NO_PULL)

while True:
    state9 = pin9.read_digital()
   
    for pin, time in zip(pin_list, time_list):
        
        if state9 is 1:
            pin.write_digital(1)
            sleep(time)
            pin.write_digital(0)
            sleep(time)