Yeah, I'm weakening a bit... but I ain't giving away any secrets...
The Traffic Light problem has been the bane of many a novice programmer. The purpose of the exercise is to get the novice to think in terms of details. Also, to think in terms of cause and effect.
Some might think that it is easy to describe a process. It can be easy. But, of course, it really depends on the complexity of the process.
Where a lot of new programmers get in trouble is thinking that they are very familiar with a particular process and that it should be pretty easy to describe.
Try describing how to tie your shoes!
That is a very familiar process to all of us, and yet, without using pictures or actually showing the steps, it is virtually impossible to describe.
In order to describe something, anything, attention must be paid to the details. In PLC Programming, the details are everything!
Now, if anyone was asked to describe the inner-workings of a computer, most would be over-whelmed at the suggestion that such a thing could be done! Jeez! There are so many different parts!
As in any effort in describing something, the first step is too break the system or process down into it's basic parts. You continue breaking those parts into their basic parts. At some point, you get to a level where you start to see commonality in the basic parts.
The first step in breaking a computer system down produces a split between Power Devices and Logical Devices. Software comes later.
The Power Devices are identified from the wall-plug, to the Power Supply, to the various places where Power is used.
The Logical Devices are identified first as major components; Mother-board, particular cards in the slots, hard-drive, floppy-drive, CD... etc.
Then each of those major components are broken down even further. Eventually, you start to see that there is a great deal of commonality between these Logical Devices. These are the basic building blocks. In order to provide an accurate description of how the devices work, you need to know how the basic building blocks work.
Those are the details.
I am in no way suggesting that this is an easy thing to do. In fact, I am suggesting the opposite; it is hard to do! Paying attention to details can be mind-boggling. And yet, you gotta do it! The Devil and all other denizens of the deep are in the details!
With that in mind, what, exactly, occurs when a traffic light is operating? A very handy tool to use, in this case, is a Timing Diagram.
In the case of a common Traffic Light the pattern simply repeats in a time-oriented fashion.
As shown above, the N-S lights follow a particular pattern. The E-W lights follow a similar pattern but are synchronized with the N-S lights in such a way that you don't have both lights showing GREEN at the same time.
In this example, you can see that there is a particular ratio between the RED, YELLOW and GREEN. If we use YELLOW as the basic time unit, you can see that the ratio of RED-to-YELLOW is 3:1 and the ratio of GREEN-to-YELLOW is 2:1.
GO-STOPPING-STOPPED
The ratio is NOT fixed. That is, you can establish any ratios you want as long as they don't cause conflict.
In a normal light, the ratios would actually be much larger so that the light would be GREEN for a much longer time in one direction (and therefore, RED for a much longer time in the other direction) than YELLOW in either direction.
The ratio of GO-time would be close to the ratio of STOPPED-time, but both would be much larger than STOPPING-time.
Without even considering the specific value of the time-unit, you can see that each segment in the Timing Diagram always produces the same effect. If each light responds according to the particular segment, then the desired result is produced.
So, the task is... how to develop the "segments".
I can Name-that-Tune in one Timer and one Counter.
I hope I didn't give away too much without at least teaching something.