Shop OBEX P1 Docs P2 Docs Learn Events
Traffic lights — Parallax Forums

Traffic lights

tempesttempest Posts: 10
edited 2004-12-22 15:55 in Learn with BlocklyProp
Use six LEDs; two red, two yellow and two green to create the lights at a traffic intersection. One set of lights will handle the North-South traffic while the other set will handle the East-West traffic. The LEDs will be arranged in the following typical traffic light form:
·
··········· ·· North-South Lanes ··········· East-West Lanes
·
······························································································· Red lights = 5 seconds
······························································································· Yellow lights = 1 seconds
······························································································· Green Lights = 4 seconds
·
·
Connect the LEDs to any Stamp pins that you wish and program the Stamp to light the LEDs in the proper sequence on a continuous basis using the times outlined above.· Show the working system to your instructor to obtain a grade of B for this module.

ok heres my code so far..it kinda works..but not really does anyone have any ideas on whats wrong or a different way I can go with this· code?
OUTPUT 10
OUTPUT 11
OUTPUT 12
OUTPUT 13
OUTPUT 14
OUTPUT 15

repeat:
LOW 10: LOW 11: LOW 12: LOW 13: LOW 14: LOW 15
HIGH 10: PAUSE 500: HIGH 15: PAUSE 400
HIGH 10: LOW 11: LOW 12: LOW 13: LOW 14: LOW 15
HIGH 14: PAUSE 100: HIGH 15: PAUSE 400
LOW 10: LOW 11: LOW 12: LOW 13: LOW 14: LOW 15
HIGH 11: PAUSE 400: HIGH 13: PAUSE 500
LOW 10: LOW 11: LOW 12: LOW 13: LOW 14: LOW 15
HIGH· 12: PAUSE 100:
GOTO repeat

Comments

  • tempesttempest Posts: 10
    edited 2004-12-21 18:48
    oh and then I'm supposed to expand on it and add two more lights for turning
    If a left turn lane is added to the basic traffic light operation, just before the green light for straight through traffic would come on the left turn light must flash on and off at a .2 second rate while the red light stayed on. But if no car is currently waiting in the left-turn lane there would be no need for the left turn light to flash at all.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-21 19:12
    The trouble with the way you've written the program is that it's tough to expand.· You could break the lights down into subroutines and pass a value to them.· I'll give you one hint -- but this is a class project and you should be writing the code.

    Red_On:
      LOW GreenLed
      LOW YellowLed
      HIGH RedLed
      PAUSE timer
      RETURN
    


    Notice that I've given the pins names -- makes reading the code a whole bunch easier, doesn't it?· At the top of your program, you would have something like this:

    RedLed    PIN    15
    


    As it stands, one has to "decode" your program to figure out what's going on -- not good (especially when your teacher wants an explanation).· In the Help file you find a section called "The Elements of PBASIC Programming."· Let me suggest that following this style guideline will probably bump your grade a bit.· It's one thing to write a program that works, it's another to write a program that a fellow programmer (or your teacher) can immediately read and understand -- that means that YOU understand the program and it is expandible and maintainable.· Save criptic code for your C-language programs. smilewinkgrin.gif

    In the Help file you find that LOW and HIGH set a pin's output direction, so all your OUTPUT statements are redundant and a waste of code space.

    The PIN definition will let you scan pins as an input, and this is what you'll need to move on.· Once you can read an input (lots of details on that in our "What's A Microcontroller?" book) you can apply some IF-THEN logic to handle the left-turn lane sequencing.

    Have fun!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • tempesttempest Posts: 10
    edited 2004-12-21 19:32
    ok thx for the help..I was just trying to do something over the christmas break..nothing for class or anything..my friend does this stuff and I using his stamp kit to see if I can learn how too..but ya I'll include information on each line of code after. I was just trying figure out the routine for the coding so the lights would work.
  • tempesttempest Posts: 10
    edited 2004-12-21 19:34
    PS. that how it shows how to do it in the book thing to start out using high low for beginners I guess..but I like the way you did it better. seems more understandable
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-21 19:48
    You'll find that writing clean code will minimize the commenting required. Why do this:

    HIGH 15 ' turn on the red light

    when

    RedLight = IsOn

    is so much easier to type and understand?....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • tempesttempest Posts: 10
    edited 2004-12-22 13:31
    how do I set up the timer so that the red LED goes for 5 secs then and the yellow LED for 1 sec and green LED for 4 secs?
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-12-22 13:48
    Personally, I find "HIGH RedLight" easier than "RedLight = IsOn", but that's personal taste, really.

    The BS2 has a 'PAUSE mSecs' command. So you can say "PAUSE 5000" to pause 5000 mSecs, or 5 seconds.

    It might be good to have a 'Pause1Sec' subroutine, so you can track the various lights, and change them when needed.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-12-22 15:55
    The reason I tend to discourage "HIGH RedLight" is that many circuits use active-low connections. If that were the case here, then "HIGH RedLight" would cause the red light to be off -- the point I was trying to make (which has now been muddied) is that code CAN and should be written to be unambiguous, regardless of circuit design, hence my preference for the "RedLight = IsOn" form.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.