Skip to content

Instantly share code, notes, and snippets.

@RobinBoers
Created July 19, 2021 09:13
Show Gist options
  • Save RobinBoers/9c33272939ae998943eab25b287948ed to your computer and use it in GitHub Desktop.
Save RobinBoers/9c33272939ae998943eab25b287948ed to your computer and use it in GitHub Desktop.
Simple script to control a traffic light using a micro:bit and stop:bit
from microbit import *
# Kitronik STOP:bit blocks
# turn each LED on or off individually
def stopBitLight(colour, illuminate):
if colour == "Red":
if illuminate == "On":
pin0.write_digital(1)
elif illuminate == "Off":
pin0.write_digital(0)
elif colour == "Yellow":
if illuminate == "On":
pin1.write_digital(1)
elif illuminate == "Off":
pin1.write_digital(0)
elif colour == "Green":
if illuminate == "On":
pin2.write_digital(1)
elif illuminate == "Off":
pin2.write_digital(0)
# turn each LED on or off individually
def stopBitState(state):
if state == "stop":
pin0.write_digital(1)
pin1.write_digital(0)
pin2.write_digital(0)
elif state == "getReady":
pin0.write_digital(1)
pin1.write_digital(1)
pin2.write_digital(0)
elif state == "go":
pin0.write_digital(0)
pin1.write_digital(0)
pin2.write_digital(1)
elif state == "readyToStop":
pin0.write_digital(0)
pin1.write_digital(1)
pin2.write_digital(0)
while True:
stopBitLight("Red", "On")
stopBitLight("Yellow", "Off")
stopBitLight("Green", "Off")
sleep(5000)
stopBitLight("Red", "Off")
stopBitLight("Yellow", "Off")
stopBitLight("Green", "On")
# Tell vehicles to drive here
sleep(5000)
stopBitLight("Red", "Off")
stopBitLight("Yellow", "On")
stopBitLight("Green", "Off")
sleep(1000)
# Tell vehicles to stop here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment