Traffic Light Duration Issue
in BASIC Stamp
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
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!!
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!
' 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.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.
You need to put @Johnymac and @JRoark on your Christmas card list.