Shop OBEX P1 Docs P2 Docs Learn Events
Traffic Light Duration Issue — Parallax Forums

Traffic Light Duration Issue

Let me preface with I am very new to all of this and have only been doing it for roughly three weeks. Anyways, for a class I need to just write a Pseudo-Code for two traffic lights at an intersection. Simple enough but I wanted to write code for it. The issue is initially the first green light is supposed to be on for 30 seconds while at the same time the second red light is supposed to be on for 40 seconds. Then the yellows each for ten seconds and finally the first red for 40 seconds and the second green for 30. The Pseudo-Code was easy enough but what I am struggling with is getting the lights to turn on together but have separate duration. I'm not sure if I am over complicating it or its just a lack of knowledge. I am not looking for someone to write the code for me, all I am looking for is help in finding out if what I am trying to do is possible with PBASIC 2.5 or at least the right place to look in the help book.

Comments

  • You might think about the idea of having a timer mechanism that ticks every 10’seconds. That “tick” causes a sequencer (a wheel) to advance by one position. The sequencer can actuate up to 6 outputs (switches) which are activated by a pattern of pins set into the wheel. So as the wheel turns, it activates combinations of switches according to the pattern of pins. Each switch gets connected to a light.

    So now you have a mechanical device that would actually work in real life. (As a matter of fact, 40 years ago, this is exactly how traffic lights worked!). All that remains is to emulate this behavior in software.

    I would suggest to you that the “wheel” might be implemented as a software loop, and the “ticker” is a software timer. I’ll leave it up to you to devise how to make the “pins” in the “wheel” work. :)

    Btw: welcome!!
  • That sounds like exactly what I was looking for! Thank you for the advise. I will do some searching to implement this and if I do I will post an update.
  • Cluso99Cluso99 Posts: 18,069
    While you could change all lights concurrently, the time between separate instructions to change each light in a straight sequence would be negligible.
  • I got it!! I was over complicating it and just had to change the location of some HIGH/LOW sequencing with PAUSE duration's in between.
  • JRoarkJRoark Posts: 1,215
    edited 2020-01-26 14:22
    @alex_the_noob writes:
    DO
    
      HIGH 13: HIGH 5
      PAUSE 30000
      LOW 13
      HIGH 14
      PAUSE 10000
      HIGH 4
      LOW 5
      LOW 14
      HIGH 15
      PAUSE 10000
      LOW 4
      HIGH 3
      PAUSE 10000
      PAUSE 10000
      PAUSE 10000
      LOW 3
      PAUSE 10000
      LOW 15
    
    LOOP
    
    That'll get it done! Congratulations!

    Now lets talk about maintainability, because even the most trivial software has to be, at some point, maintained. So its best to bake that idea into the code right at the start. And understand that none of what follows is criticism. This is how the "pro's" do it.

    To a stranger who looks at that code, there is going to be a bit of a learning curve. There are no hints to help him along. So how about adding comments that tell the world what the code is doing at any given point?

    Another trick is to replace the pin numbers with the name of the light they control. Maybe do something like "High EWRed" instead of "High 14" would be clearer to a new reader? Think about how you might assign a pin number to a name in PBasic. Also, breaking your code up into blocks helps readability a bunch.

    I don't actually know which pins are connected to which traffic light, but let me give you an example of what I'm thinking:
    ' Note: Traffic light demo. Driving a pin HIGH turns a light on. Driving it LOW turns it off
    DO
      HIGH EWGreen     ' East/west road: green on
      HIGH NSRed       ' North/south road: red on
      PAUSE 30000      ' 30 second delay
    
      LOW EWGreen      ' East/west road: green off
      HIGH EWYellow    ' East/west road: yellow on
      PAUSE 10000      ' 10 second delay
     ...
    LOOP
    
    See what I mean?

    Anyway... that was a good effort, and you have working code! So drinks all around!
  • @alex_the_noob I wanted to follow-up my earlier post with an example of why comments are so important. This is a code snippet from a video driver demo program written by @rogloh that I trivially modified for use in BASIC. I don't expect you to be able to read this, but just glance at it. Notice that there are 3x more comments than actual code in this section! This is professional-grade code. When Roger wrote this, he effectively baked a users manual right in to the code itself. If he had simply written the code, with no comments, and if he used hard-coded numbers instead of well-named variable/constants, I'd be completely and forever lost. But he did it the right way, which means that anyone who knows the language can pick this up, study it for a bit, and effectively maintain it... or even create something new.
    	' create a VGA display
    	'initDisplay spawns driver COG returns cogid, display starts up blank until a region is attached
    	'display - indicates address of where display data is to be stored
    	'output - indicates type of output - VGA, DVI, COMPONENT_HDTV etc
    	'basePin - pin number of starting pin or group of pins
    	'syncPin - used for vertical sync output in VGA RGBHV mode
    	'flags - indicates if interlaced/progessive, PAL/NTSC 
    	'lineBuf - hub address of two scanline working buffer
    	'maxLineSize - size of each scanline in the line buffer
    	'userTiming - when non-zero points to timing structure to be used to setup display
    
    	dim VID as class using "p2videodrv.spin2" 
    
    	vid.initDisplay(@display, VID.VGA, VGA_BASE_PIN, VGA_VSYNC_PIN, VID.RGBHV, @lineBuffer1, LINEBUFSIZE, timing)
    		
    	' setup and attach a single region
    	vid.initRegion(@region,  VID.TEXT,  0, VID.FLASH_TEXT, VID.getVGAPalette(), VID.getvgafont(), 16, @screenbuf, @display)
    
    	vid.setTextContext(@display, @region, $1F) '!!!!! MUST SET THIS BEFORE ANY TEXT PRINTING STARTS OR IT COULD HANG !!!!
    
    So to the extent that you can, you should try to follow this ethic in your own code. I can pretty much guarantee you'll impress your instructor if you do.
  • JonnyMacJonnyMac Posts: 9,155
    edited 2020-01-28 01:09
    When I started programming I would ask those with more experience to show how they would tackle a particular problem -- I learn a lot by dissecting code written by pros.

    That's not to say you asked, nor that I'm a pro (though I do get paid for coding), I thought I'd do this as a break from my work because I've written hundreds of BS2 programs (but not in a while), and I walk around my city a lot and am frequently watching the traffic lights for their logic.

    As I'm a highly visual person, and this program has a visual outcome, I started with the attached illustration to work out the sequence. In my version, red lights over lap by a few seconds to deal with those people who rush through the intersection on yellow -- having both red for a couple seconds seems like a good idea.

    I use a state/timer approach: each light has a state (red, green, or yellow), and each of these states has a run-time associated with it. The code seems long but it is very simple, very flexible, and mostly self-documenting. My coding style is very specific (you can read about it in StampWorks, a book I wrote for Parallax ages ago [under my previous name]).

    Anyway, I hope this causes you to think about other approaches to your programming tasks.
    1122 x 496 - 13K
  • I want to thank you guys for taking the time to comment and offering help. I went back and added comments, very precise comments I think, and when I had to make altercations I knew exactly where and when to put them. After reviewing the code you posted JonnyMac I realized not only how much I still have to learn, but how much I want to learn. Thank you again.
  • Glad it worked out! These forums are great for Noobs and experienced alike. We like happy endings. Come back often.

    You need to put @Johnymac and @JRoark on your Christmas card list. :)
  • I’m glad it worked out for you, @alex_the_noob!
Sign In or Register to comment.