Traffic Light with 2s delay

Cwh8750

Member
Join Date
Feb 2016
Location
Flower mound
Posts
2
I've spent the last week or so combing through this forum reading the replies and advice given to previous users (since this is a frequently asked question).

Attached is my work using cascading timers to operate a traffic light. While i do have the two second delay implemented into the programming, the problem I am running into is with the delay between the lights actually becoming energized. For a moment, between the Amber North/South light de-energizing and the North/South & East/West light energizing there is a time when neither red lights are on, resulting in the simulation sending any stopped cars through the intersection resulting in a (possible) crash.

The only solution I've thought of is using an additional yet separate timer to run independently and energize both red lights milliseconds before the amber light is finished. But over time this would result in it's own problems and I'm not sure how to go about setting the timer and having it reset on it's own just after the two-second delay is finished. And i understand i could adjust the scan/s, but I would rather fix the logic behind it.
Are there any other possible solutions?

And as always, please and thank you! o_O

TrafficLight.jpg
 
Firstly, Welcome to the forum and well done for using the search function. I always think that doing it with cascading timers is the hard way of doing it and prefer a state sequence approach but, either way, what you are in need of is an extra state where all lights are red - actually this should be your starting point when the lights are powered on.

Have you written the sequence down a piece of paper? If not, do so and then see how your code compares to the sequence. This is good practice in software design and, even after 30 years experience, I still usually start with a piece of paper to get my thoughts straight before hitting the keyboard - this is especially true for sequential logic or iterative code.

In a state/transition method, the code is programmed as a state engine that steps through the sequence from one state to the next by means of a transition. A transition might be the down signal from a timer.

So:

Power On
State 1: All light red
Transition 1: Wait x Seconds for traffic to stop
State 2: All lights red
Transition 2: Hold 2 seconds with all traffic stopped
...
...
...
...
...
Transition x: Go to State 2

I can't really see the code in your picture clearly so I can only hope that this points you in the right direction.

Nick
 
Seems like you would want the red light to be the "default".
If neither the amber or green are on, then the red would always be on.
grn amb red
--|/|----|/|-----()
 
I definitely agree with Manglemender.

Often when I start a project, I begin by drawing a graph of inputs/outputs as a timing diagram, then using the diagram, derive the logic from it. It's really intuitive. This helped me to not code myself into a corner.
 
Did this exercise myself a while back. I attacked it just as Manglemender suggests. Using stages.
S0 N-S Green State (NS Green EW Red) ~1 min
S1 NS to EW Transition State (NS Yellow EW Red) ~15 seconds
S2 All Red State (EW Red NS Red) ~5seconds
S3 EW Green State (EW Green NS Red) ~1 Min
S4 EW to NS Transition State (NS Yellow EW Red) ~15 seconds
S5 All Red State (EW Red NS Red) ~5 seconds
Jump to S0
Each state had a 3 branch output.
2 lamps, and 1 timer. The timer done bit causes the jump instruction.
You could, with some extra logic reuse the all red state, but for this exercise, I just created two identical stages, and kept the flow linear.
 
There's a typo in the previous post. Stage 4 should be
S4 EW to NS Transition State (EW Yellow NS Red) ~15 seconds

Is it not possible to edit a post on this forum?
 
My professor did provide a timing diagram, but even then i was still unsure what to do about that small delay between the timer starting and the light energizing. Today he mentioned that we wouldn't be penalized for thinking outside of the box and using other instructions we haven't yet covered, like state comparisons.

But i seem to have gotten it now, adding in that simple instruction travispedley mentioned seems to have worked. Program ran the entire time while i typed this with no crashes! I was so focused on having accurate cascading timers i simply forgot the basics!

I really appreciate the input and quick replies that i received, especially on such a sore topic! Thank you all very much!

Chris.
 
If anyone is interested this is a traffic light program I created. Instead of controlling individual outputs I’m moving words to an integer and then moving the integer to the outputs. That way I can use a sequencer to move different values to the word used to control the outputs. I’m also using a sequencer to change the main timer’s value so when the lights change from “red/green” to “red/yellow” I change the timing also (the yellow light is shorter than the “red/green”). This allows me to only use one timer to control the lights. However I also have two other timers, one for a flash mode and the other turns the red lights on (both N/S and E/W) for three seconds after the lights come out of flash. When the controller goes into run a first pass is used to start the flash routine. However I also have it so that when the main input (input 1) goes off, stopping the main routine, I also trigger the flash routine. The first pass and the “input 1” going off are redundant and truth be told I could do away with the first pass. FYI, part of the sequence has both red lights on at the same time for ¾ of a second. This is how I deal with not having anytime where there isn’t any lights on (which I think was the original problem in this thread).
Please note that I’m a self taught PLC programmer so I’m sure there is a better way to do this but this sort of thing is how I teach myself how to program. It’s more “fun” than useful.
 
Is this some kind of school project that is common?

Real traffic light have different sensors that reacts to cars as well as adaptive algorithms for traffic flow and coordination with other traffic lights I imagine.
 
I’m pretty sure that a traffic light program is used a lot in PLC programming classes. I’ve not take such a class however they do pop up here from time to time and it’s usually someone who is new to PLC’s (and sometimes someone looking to cheat on their homework). If you think about it a traffic light program makes a great training tool. You’ve got inputs, outputs, timers and you have to create a logical flow of the process. If I were teaching a class I’d use one for training.
 

Similar Topics

Hi all. I'm here, again, with traffic lights program. I write this question earlier and I get one really good answers with it, but now I really...
Replies
5
Views
4,412
I am a complete newbie to this stuff and wanted to try this out and was looking for any information on how I can complete this by Wednesday...
Replies
19
Views
12,737
Hey Folks I've done some browsing along the forum for some help in a matter. It would seem the question has been asked many times but it just...
Replies
41
Views
16,850
I'm looking for a simple, cost-effective way to control traffic lights from a central location. The lights are actually mounted on signs next to...
Replies
2
Views
1,867
Hey guys, I'm fairly new to PLC and RSLogix5000, as I am currently a student. I am doing a traffic light project, which is only running off of...
Replies
8
Views
1,958
Back
Top Bottom