Programming a basic traffic light with pedestrian button

I'm trying to code a traffic light with a pedestrian button. The idea is the default on-time for the Green, Yellow, and Red LEDS is (15s, 3s, 15s). While the light is green, every 1 sec, the program will check to see if the button has been pressed and if it has, the remaining time is halved. The yellow light duration also drops from 2s to 1.5s, and red light to 5s.

The limitations are that I cannot use timers, or interrupts. I am having difficulty with how to code it so that the program checks every 1 sec for a button press and how to keep track of how much time has passed if I cannot use any timers.

Any help would be great!

Does that include the millis() and micros() functions?

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

I’m trying to understand the traffic light system an how it works in practice with a pedestrian button. Are the red, yellow and green lights controlling the traffic on the road?
At what point, during the shortened cycle, does the pedestrian get a chance to cross the road and how is this point made visible to that pedestrian?

I suppose, in this context, the limitations on use of timers refers to explicit use of timer libraries. That leaves only millis() for your 1 second polling of the button. You can’t use delay() here.

Apart from the non-blocking ‘timing’, this a perfect example of where to implement a ‘state machine’.
Normally, it would run the red-yellow-green cycles, but when a button is pressed, it can branch off to a pedestrian cycle.

How far you develop the state tree is up to you,

For non-blocking code based timers check out my tutorial
How to code Timers and Delays in Arduino

For writing multi-tasking in Arduino check out my tutorial
Multitasking Arduino on any board

Halving the time left to run is simple with millisDelay
timer.start(timer.remaining()/2);

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.