Hello, I am new to Python (3) and Raspberry pi 4. in the lockdown I have bought 1 for me and 1 for my (geographically distant) grandson and have bought some miniature LED traffic lights kits. I am going to try to help him learn.
I have programmed (in a very simple way) 3 x LEDs to switch the Red / Amber / Green lights in their correct sequence for 1 set of LEDs connected to the PIOI.
from gpiozero import LED
from time import sleep
#to identify pins used
ledred = LED(21)
ledamber = LED(20)
ledgreen = LED(16)
while True:
#1st traffic light
ledred.on()
sleep(9)
ledamber.on()
sleep(2)
ledred.off()
ledamber.off()
ledgreen.on()
sleep(5)
ledgreen.off()
ledamber.on()
sleep(2)
ledamber.off()
Actual code lines are indented correctly. What I need to do now is to add code to operate another set of lights so that when the first set are on red the other set are on green, etc. i.e., coordinated as a real pair of traffic lights!
Not sure how I do this - e.g as subroutines? do they even exist in Python!
General guidance would be welcome. Thanks, Mel
I have programmed (in a very simple way) 3 x LEDs to switch the Red / Amber / Green lights in their correct sequence for 1 set of LEDs connected to the PIOI.
from gpiozero import LED
from time import sleep
#to identify pins used
ledred = LED(21)
ledamber = LED(20)
ledgreen = LED(16)
while True:
#1st traffic light
ledred.on()
sleep(9)
ledamber.on()
sleep(2)
ledred.off()
ledamber.off()
ledgreen.on()
sleep(5)
ledgreen.off()
ledamber.on()
sleep(2)
ledamber.off()
Actual code lines are indented correctly. What I need to do now is to add code to operate another set of lights so that when the first set are on red the other set are on green, etc. i.e., coordinated as a real pair of traffic lights!
Not sure how I do this - e.g as subroutines? do they even exist in Python!
General guidance would be welcome. Thanks, Mel
Last edited: