Learning Python - some advice please

Soldato
Joined
27 Feb 2004
Posts
2,571
Location
Kent
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
 
Last edited:
Thanks for the guidance, "AHarvey" - tidying up a bookshelf of "collected items" (i.e. junk in the eyes of the other member of the household. When I have done that (after lunch) will turn my attention to Python.

Will also check out Facebook - which I am a reluctant member of!

Mel
 
Back
Top Bottom