Density Based Traffic Lights

Good day! I just got into arduino and I'm working in a project wherein I the time for traffic lights depends upon the traffic hence, density based traffic lights.

I actually found projects already done online such as (Density Based Traffic light by Muhammad Aqib) but it doesn't really fit into what I have in mind. What I have in mind is that I'll be using a ultrasonic sensor and then count the cars passing and store in an in. Then use the value in that int as the number of seconds for the green light and red light. I had problems doing that. First is I can't seem to think of a way for the int to reset back to 0 for the next cycle of counting cars. Second is connected wherein the time for the traffic lights to lit up depents upon the counting of the sensors.

I'm fairly new and I hope to gain insights! Thanks!

Link of Muhammad Aqib's project: Density Based Traffic Light Controller Using Arduino - Hackster.io

Ultrasonic Counter: How to make Bi-Directional visitor counter using single ultrasonic sensor with LCD. - Robotica DIY

The example you gave (link), I think just checks for a car at or near the stop line? It's got absolutely bugger all to do with "traffic density". There's no count, which is presumably why you said it's not what you want.

Coding specifics aside for a moment, how do you count cars when the traffic's stopped on that leg? There's no increment to the count since it's not passing the sensor, but there's a line of cars...

Can you describe for us "a day 10 minutes in the life" of this intersection, from a starting point where (say) there's been no traffic for ages and the lights are in some default state, perhaps giving each side equal time or whatever. What do you want to happen as cars start to arrive on each leg? Where will you be doing the counting?

the time for the traffic lights to lit up depents upon the counting of the sensors.

That's not a coding issue; that's a deciding how you want this thing to work, issue.

Get hold of a traffic engineering handbook and read up on the ways to monitor traffic and time the lights accordingly. The coding (as is very often the case) will be quite straightforward: the real challenge is for you to write down in words what it is you want to happen.

In another thread about line monitoring and obstacle avoidance, I suggested that poster lay out his or her path and walk around it. You could do the same: chalk out your intersection on your driveway or draw it on flip chart sheets on the floor, and get a pile of stones to be cars. Move them around and get it clear in your head how counting the movement of cars will actually translate into something you can use to adjust the timing. (It may not need to be too scientific: a sort of fuzzy approach might work- if the traffic is "a bit heavier" on one side, give that side "a bit more" green; if it's "way heavier" give it "way more" green.)

This is a classic case of trying to Don't put the coding horse before the application understanding cart; pardon the cliche. (I normally avoid cliches like the plague....) (edit: apology: Was a bit harsh there, you are asking in Project Guidance after all, not Programming, and your question is about the approach to turning the count into time, not the lines of code that do that.)

Edit... There are very likely to be state machines in your future.

I wonder how you want to calculate the green/red light times if you only can count cars while the lights in that direction are green? Do you want to wait until no more cars are passing while the light is green?

I'd think that the length of the rows of waiting cars is better suited for simple yet efficient traffic lights management.

BTW we did computer based traffic lights simulations in 1971 already.

DrDiettrich:
I wonder how you want to calculate the green/red light times if you only can count cars while the lights in that direction are green? Do you want to wait until no more cars are passing while the light is green?

That's exactly why I asked the OP to walk through this on a mock-up, so that s/he can get a proper mental picture of how this might work.

Coding's relatively easy once you figure that out.... Deciding what to do is not a trivial matter, and is the subject of thick books and PhDs.

DrDiettrich:
I'd think that the length of the rows of waiting cars is better suited for simple yet efficient traffic lights management.

If I recall from my days in traffic engineering class (graduated as a civil engineer 40 years ago next month, so it's a long time ago...) the delay to the traffic is the best way to decide on the timing, but a queue length can serve as a proxy for that.

DrDiettrich:
BTW we did computer based traffic lights simulations in 1971 already.

Indeed, but OP's project is about real time adjustment to timing on the fly for actual conditions.

All that said, there's one practical way OP could go about actually doing some coding and getting some blinkenlites working, and that's to code up a simple intersection with the cycle time and phase times all coded as variables. (Cycle time being the time between 2 identical conditions starting, eg start of northbound green to start of next northbound green, phases being eg northbound green and eastbound green)

Code that up according to the best principles here in the forum, all delay()-less of course :wink: ( see for instance Robin2's "many things" tutorial and Robin2 on how to plan a project) , and have a function where those variables are calculated. In that function, for now, you just have the values hardcoded. Simplest case would be to have say a cycle of 60 seconds and northbound and eastbound greens of 30 each (and less a bit for yellow and an all-round safety red, say).

In other words, forget how the counting of cars may inform the timing: timing's hardcoded in a function, and the point is that later you can re-write that function using a million different approaches and slot them in from a "bank" of functions by changing the name and or commenting the old one out pro tem.

Just getting that working with no delay()s (the one you linked uses delay()) should keep you occupied for a while, and if properly structured will allow you later to slot in whatever algorithms you care to use for changing the timing.

That's the best of both worlds maybe: separate the complexity of traffic engineering theory from coding a minimum viable product as the Agile guys-n-gals would say, which MVP you can enhance as your traffic engineering ideas evolve.

(In fact, a not-insignificant challenge would be to re-jig that one you linked not to use delay().....)

Structure of the code from previous could look like this:

void setup()
{
//stuff
}

void loop()
{
calculateTheTimings();
switchTheLights(cycleTime, northTime, eastTime);
}

void calculateTheTimings()
{
// this is the hello world version, and checks that switchTheLights() actually works
// keep this intact as your mvp "fall back"
cycleTime=60;
northTime=30;
eastTime=cycleTime-northTime;
}

void switchTheLights(cycleTime, northTime, eastTime)
{
// do stuff with millis() to turn the lights on and off
//   according to the values sent in
}

/* only ever have one version of calculateTheTimings() active at a time
void calculateTheTimings()
{
// do stuff with counters and phases of the moon and other voodoo
cycleTime= <the result of some voodoo>;
northTime=<the result of some more voodoo>;
eastTime=cycleTime-northTime;
}
*/

sayHovis:
Don't put the coding horse before the application understanding cart; pardon the cliche.

Thanks for your input. I really appreciate someone calling out my naive ideas as I don't have someone teaching me stuff. To give you guys background why I somehow choose this project,a professor let us decide to choose a problem and try to develop a prototype as a solution. And us, a bunch of freshman who just got into arduino, did a lot of surfing in the net and found the project I stated above. And now deadline's approaching about 1 week and I guess we're doing bad knowing that the project I thought would be fairly easy is going bad. Anyways thanks! I would be trying your other suggestion. Although, I wish I could incorporate these ultrasonic sensors that I bought anyway in the project

did a lot of surfing in the net and found the project I stated above. And now deadline's approaching about 1 week and I guess we're doing bad knowing that the project I thought would be fairly easy is going bad

My advice then, would be to ditch your current idea and try to re-jig the one you already found to make it a better (more elegant? use of best practice?) sketch by making it delay()-less.

This...

jerome9012:
I just got into arduino

... coupled with this:

deadline's approaching about 1 week

... I think means you have to lower your sights.

You have little choice though, even with deadline approaching, to work through at least some of these tutorials before you get into an actual project.

May I point out that in the United States, the on-time for green/yellow/red traffic lights are set by a national standard. Not all jurisdictions abide by the standard, however.

Paul

I cross one intersection leading into Boston, MA. The 2 lane divided highway gets 90 seconds of green, my crossing gets enough for about 6 cars. Maybe 8 if folks are on the ball and no turns left and blocks other cars from going straight.

I created a project* back in 88, which does exactly what you’re looking for.
That traffic grid was a distended ‘matrix’, linking 3-4-5 way junctions.
The strategy I used wa exit loops, rather than entry loops at intersections and other strategic points to detect vehicle count, then weighted the forward segments of roadway to determine route priority and timing.**
This also allowed very precise override of the network for traffic management.
if you add complex directional lanes/arrows, interesting things can be done with variable traffic control in peak periods.
These algorithms can be extended in chess-like manner to influence several ‘ply’ depth of junctions.

  • ref C.A.R.S by SL4P 1998

** I always figured it was more useful to know where the traffic was ‘going’ rather than where it was ‘coming from’ !

Not exactly the project for someone brand new to Arduino and a week to do it in.

So OP, if I were you I'd take the project you linked and re-do it to use better programming practice.

Hii,

Well I am not confident if ultrasonic sensor is the best choice for this project. Because,

Generally we have two lane roads now days. Assume a car passes by the sensor on one of the lane (currently it may count no. of cars correctly) but if two cars passes by the sensor in such a way that they are one behind the other. Then still it would count as one car. Talking about practical day life this scenario appears a lot of time. But still if its single lane our system would work great. Nice thought. Keep working on it .

Raj