1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Learning Python - some advice please

Discussion in 'HTML, Graphics & Programming' started by Mel_P, 1 Apr 2020.

  1. Mel_P

    Wise Guy

    Joined: 27 Feb 2004

    Posts: 2,166

    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: 1 Apr 2020
  2. AHarvey

    Sgarrista

    Joined: 6 Mar 2008

    Posts: 9,996

    Location: Stoke area

    what I would do is create 2 functions, one for turning from go to stop and the other from stop to go, both functions can take a traffic_ligh_name so it knows which one to act on.

    Then, you need to create 2 variables, these are to hold the current status of each traffic light.

    Then use a time function to wait and just loop through them.

    so if light_1 is go you know light_2 should be stop.

    then loop

    wait x seconds, move light_1 to stop, wait y seconds, set light_2 to go etc

    Edit: if you're on Facebook check out the Python Programming Language, i'm one of the admins on there and it's more active than Python Programming Society which is our other group which is mainly for beginners but appears to be dying slowly.
     
  3. Mel_P

    Wise Guy

    Joined: 27 Feb 2004

    Posts: 2,166

    Location: Kent

    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
     
  4. AHarvey

    Sgarrista

    Joined: 6 Mar 2008

    Posts: 9,996

    Location: Stoke area

    Once you've got it working as you'd like, maybe you could try writing something that sends data to each other so only one of yours is on green while all of his are red etc :D
     
  5. MrWhippy

    Wise Guy

    Joined: 15 Sep 2005

    Posts: 1,501

    this type of problem is a good example of a state machine.
    google for traffic light state chart or state machine images.

    i was suprised by how many different examples this throws up.
     
  6. muttclugg

    Wise Guy

    Joined: 10 Dec 2007

    Posts: 1,769

    Location: SE

    OP - Have you heard of Cambridge Raspberry Jam AKA Camjam? Check it out here: https://camjam.me/ really nice kits with nice worksheets available online. Great for kids.