# Traffic lights with a pedestrian button, in MicroPython for the Micro:bit.
# LEDs with 220 ohm resistors: red on pin 1, yellow on pin 2, green on pin 8. Button on pin 14 to GND.
from microbit import *


def set_lights(red, yellow, green):
    pin1.write_digital(red)
    pin2.write_digital(yellow)
    pin8.write_digital(green)


pin14.set_pull(pin14.PULL_UP)     # 1 when not pressed, 0 when pressed

while True:
    set_lights(0, 0, 1)           # green for the cars
    while pin14.read_digital() == 1:
        sleep(20)                 # wait for the button
    sleep(1000)                   # the cars get a moment
    set_lights(0, 1, 0)           # yellow
    sleep(1000)
    set_lights(1, 0, 0)           # red: pedestrians cross
    sleep(4000)
    set_lights(1, 1, 0)           # red and yellow
    sleep(1000)
