Hi all, Im designing a beginner level project in proteus, Its a 4 way traffic light system with additional features like a 60 sec countdown timer using 2 digit 7 segment display
I opted to design the traffic lights using standard delay, And from this point I think im stuck because I cant think how to implement a 60sec countdown timer with all these delay functions.
the only successful approach that I thought of seems to be using a second arduino uno/arduino duo (Because its impossible to make 2 void loops, one for traffic and one for the timer)
which isnt a logical approach. I am limited with a single arduino uno and an mcp23017 for digital pin extension for the design process.
also any help on proteus is appreciated, Im struggling to find a weight load cell and a ready-to-use hx711 module for proteus simulation.
Thanks for the feedback everyone, I guess millis() is what I must use,I wasnt aware of the millis function despite the fact that I used it before (heard about it vs delay and tried it), The problem is that it would take a lot of time to implement millis related stuff in such a design for a beginner (like myself), I did some search today and began to re-write the traffic part and it seems like the code is going to be hundreds of lines on the 4 way traffic alone Im sure it could be done in less but hey, Atleast it'll make space for everything else and solve my problems.
fishboneDiagram:
What does a countdown timer and display have to do with traffic lights?
It's my idea, a bit quirky ik but the concept here is to signal to the driver the remaining time of the current period (e.g yellow to red) and how much time left for him to go to complete stop
fishboneDiagram:
? That makes no sense
I swear millis did not cross my mind before the post xD I only used it like once for experimenting like knowing about the fact that Arduino has an internal clock.
lastchancename:
To implement a smart 4-way junction with load sensing, ped buttons & crossings etc, is a substantial project.
It would be literally impossible to do this ‘properly’ with any blocking delay strategies.
Well I think I underestimated this whole thing then xD but hey, Atleast I did complete the 4 way traffic light part with millis(), 141 lines, could be less but it's all that I need and its satisfying
Uh-Oh!
So after getting the 4 way traffic part done with millis(); I got stuck with my second objective, a countdown timer on 2x 7 ca segments
For sure I do know that the timer must be done using millis(); before I even started working on it, the problem is I have no clue how to implement a 60 second countdown timer despite trying and squeezing every braincell I have.
here's what I tried and the exact problem Im facing:
if (CurrentTime > 5000 && CurrentTime< 6000 ) // this is a millis period dedicated for
//the event
//CurrentTime is a value that equals current millis() subtracted from zero
{
for (int y = 0; y < 4; y++)
{
digitalWrite(x[y], z[s][y]); // using arrays and loop to print a number
//on one of the segments
//"s" represents the numbers' rank in the array
}
s++; // the issue lies here: s will keep incrementing as fast as possible
//during that second
}
As you can see I have an idea why the seven segment counts very fast.
the problem is I dont think its possible to make it count in seconds, I mean is it even possible to increment s for only once during that single second Millis() period I made? or incrementing s for once during its own period or void loop()?
It worths to mention that I have lots of millis() periods scattered around for the traffic part, that means s++ will basically repeat itself even if it was outside the seven segment's millis() period and inside void loop() because other events in void loop() take time to happen
and even If I figured that out I would still face problems because this code needs to be applied for every single second. and I used a lot of big millis periods
I'd write a sketch that writes numbers on your seven segment display. Write a function that takes a byte parameter and displays it.
Then fuse that with the blink without delay example. Whenever you're supposed to turn the led on, increment a counter and pass it to your display function.
Then take that functionality and splice it back into your main sketch.
Your issue seems to be that you count when the time is in that “window’
You want to increment once a second while in that window.
You need another millis(( timer in your loop()... it’s possible to use many simultaneously with different intervals (they’re just numbers counting away!)
Once you have a stand-alone function that displays the static digits, call that as needed (every second), not every time around loop !
Everything else will run in its own good time, and the countdown function will only happen when it’s needed.
lastchancename:
Your issue seems to be that you count when the time is in that “window’
You want to increment once a second while in that window.
You need another millis(( timer in your loop()... it’s possible to use many simultaneously with different intervals (they’re just numbers counting away!)
Can you please elaborate for what use is that Millis()?
lastchancename:
Once you have a stand-alone function that displays the static digits, call that as needed (every second), not every time around loop !
wildbill:
I'd write a sketch that writes numbers on your seven segment display. Write a function that takes a byte parameter and displays it.
Then fuse that with the blink without delay example. Whenever you're supposed to turn the led on, increment a counter and pass it to your display function.
Then take that functionality and splice it back into your main sketch.
Here's what I got, I think im starting to get it, first these 2 advises are basically suggesting the same method(or maybe not?)
Which is a function for displaying static digits + a function for incrementing, Im still not getting where EXACTLY the increment process should take place though, and how to do the function of incrementing
TL;DR version: I am very in need for code examples to understand what you guys are suggesting,if you may please provide them and Ill be very grateful for them