Traffic Light Duration Issue
alex_the_noob
Posts: 4
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!!
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: See what I mean?
Anyway... that was a good effort, and you have working code! So drinks all around!
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.