Inksaver

Raspberry Pi Traffic Lights

Jan 26th, 2021
979
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import gpiozero as io #shortened version, but retaining full class path
  2. import time
  3.  
  4. WAIT = 4 # Constant for time to keep light on red or green
  5.  
  6. def off(leds):
  7.     '''Pass dictionary of LED defined in main and set all to off'''
  8.     for led in leds.values():
  9.         led.off()
  10.  
  11. def on(leds, colours):
  12.     '''Pass dictionary of LED defined in main and list of colours to switch on'''
  13.     for colour in colours:
  14.         leds[colour].on()
  15.     time.sleep(1.5) # built in timer
  16.  
  17. def main():
  18.     # define dictionary of LEDs
  19.     leds = {'red':io.LED(17), 'yellow':io.LED(27), 'green':io.LED(22)}
  20.     while True:
  21.         off(leds)
  22.         on(leds, ['red'])
  23.         time.sleep(WAIT)
  24.         on(leds, ['red', 'yellow'])
  25.         off(leds)
  26.         on(leds, ['green'])
  27.         time.sleep(WAIT)
  28.         off(leds)
  29.         on(leds, ['yellow'])
  30.  
  31. main()
RAW Paste Data