Skip to content

Instantly share code, notes, and snippets.

@miketrebilcock
Created August 31, 2016 19:04
Show Gist options
  • Save miketrebilcock/39ba960cacfb8a788e5886d6e600a065 to your computer and use it in GitHub Desktop.
Save miketrebilcock/39ba960cacfb8a788e5886d6e600a065 to your computer and use it in GitHub Desktop.
Raspberry Pi Traffic Lights Demo
from gpiozero import LED
from time import sleep
green = LED(22)
amber = LED(27)
red = LED(17)
def all_off():
green.off()
amber.off()
red.off()
def green_only():
green.on()
amber.off()
red.off()
def red_amber():
green.off()
amber.on()
red.on
def red_only():
green.off()
amber.off()
red.on()
def amber_only():
green.off()
amber.on()
red.off()
def all_on():
red.on()
amber.on()
green.on()
def normal_seqence(loops):
while loops>0:
red_only()
sleep(3)
red_amber()
sleep(2)
green_only()
sleep(5)
amber_only()
sleep(2)
loops = loops -1
def fast_sequence():
while loops>0:
red_only()
sleep(1)
red_amber()
sleep(0.5)
green_only()
sleep(1)
amber_only()
sleep(0.5)
loops = loops -1
def chaser(speed, loops):
all_off()
while loops>0:
red.on()
sleep(speed)
amber.on()
sleep(speed)
red.off()
green.on()
sleep(speed)
amber.off()
sleep(speed)
amber.on()
sleep(speed)
green.off()
red.on()
sleep(speed)
amber.off()
sleep(speed)
loops = loops -1
def beat_all(speed, loops):
all_off()
while loops>0:
all_on()
sleep(speed)
all_off()
sleep(speed)
loops = loops -1
def beat_ambergreen(speed, loops):
all_off()
while loops>0:
green.on()
amber.on()
sleep(speed)
green.off()
amber.off()
sleep(speed)
loops = loops -1
def beat_green(speed, loops):
all_off()
while loops>0:
green.on()
sleep(speed)
green.off()
sleep(speed)
loops = loops -1
def beat_amber(speed, loops):
all_off()
while loops>0:
amber.on()
sleep(speed)
amber.off()
sleep(speed)
loops = loops -1
def beat_red(speed, loops):
all_off()
while loops>0:
red.on()
sleep(speed)
red.off()
sleep(speed)
loops = loops -1
while (True):
chaser(1,1)
chaser(0.5,2)
chaser(0.25,5)
chaser(0.1,10)
beat_all(0.5,5)
beat_ambergreen(0.2,8)
beat_green(0.1,10)
beat_amber(0.1,10)
beat_red(0.1,10)
beat_amber(0.25,10)
beat_green(0.25,10)
beat_red(0.5,5)
beat_amber(0.5,5)
beat_green(0.5,5)
beat_all(0.1,5)
normal_seqence(5)
fast_sequence(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment