I am trying to come up with an algorithm for calculating when a light will change to green/how long it will stay green.
I have the following simplified example data: Current time: AM 7:42:20 seconds(red) Light turns green at 7:43:00 exactly. Green light lasts 120 seconds, until 7:45
We then have a red light for 60 seconds until 7:45:59 Light then turns green again at 7:46.
In other words: Green: 120 seconds Red:60 seconds
While I(as a programmer) know I can calculate the time it will turn green in 24 hours and store it in an array, I am trying to solve problems more mathematically and efficiently.
How would a more mathematically inclined person calculate if the light was currently green or red at any time in the day knowing the length of the green and red light, and a base time at which it was calculated. Eg turned green at 7:43?
I have tried finding a pattern with modulo, and also tried just using the last digit and subtraction, but I am having trouble getting anything solid.
I am more interested in the thought process than the actual answer. Thank you!
convert all times to seconds and consider the quantity ..
$$\delta t \equiv (t-t_{ref}) \mod 180 $$
if $\delta t < 120$ then the light is green and will remain so for $(120-\delta t)$ seconds
otherwise the light is red and will remain so for $(180-\delta t)$ seconds