Practical lesson
In the practical lesson three applications are created. 1) “Stopwatch”: Interval = 100 (a tenth of a second). Global t: Integer; in the Timer t := t + 1; Label1.Caption := IntToStr(t div 10) + '.' + IntToStr(t mod 10); – for example t = 125 gives 12.5 seconds. Let the Start/Stop button be Timer1.Enabled := not Timer1.Enabled; 2) “Traffic light”: Interval = 1000, global i: Integer; in the Timer i := i + 1; k := i mod 14; – at k = 0…5 red (6 s), at k = 6 yellow (1 s), at k = 7…13 green (7 s); the lights are drawn on an Image with Brush.Color := clRed and Ellipse. 3) “Clock”: a Label with time and date. Important: do not create colour changes that flash more than three times a second on the screen, because this can harm some people; treat the traffic light only as a learning model, on a real road you must obey the signals.
A pedestrian crossing signal repeats in a fixed cycle. To know which colour is on 100 seconds after the start, it is enough to divide 100 by the cycle length and look at the remainder. A programmer also finds the state this way, through the remainder.
Programmers of traffic signals, game programmers and timetable makers find the state within a repeating cycle by using the remainder.
If a game round repeats every 5 minutes, divide 17 by 5 and use the remainder to find which round and which minute of it you are at in minute 17.
| i | k = i mod 14 | light |
|---|---|---|
| 13 | 13 | green |
| 20 | 6 | yellow |
| 30 | 2 | red |
“The light table”: a pair makes a 25-second table: it writes in advance which light is on each second (k = i mod 14) and checks it with the application.