We could get the delays that we want by putting for loops at the appropriate points in the code. This would work fine, and the user of the program would be quite happy with this.
However, this is not a good solution, for a number of reasons:
Program Size
It makes the program large. The delay may be needed in the program in a number of different places and if the for loops are placed in each position this will make the code large.
Program Maintenance and Upgrade
It makes the code harder to maintain. If we find a fault in our delay code we must find every instance of the code and fix the problem. This can be tedious.
It makes the program harder to upgrade. If I move the program onto a processor which runs at a different speed I'd have to go thorough and change the limits in all of the delays to replace them with the new value. It is much simpler if this code is only written in one place, because I just need to change a single instance of the code.
Program Design and Construction
If several people are working on a program it is useful if the work that can be spread around different programmers. If the program is broken down into a number of functions they can each be written by different programmers. There would be a big meeting at the start of the project to decide what functions are needed and how they are to be used. Then the programmers can go off and write their part of the system.
void delay( int length )
{
int i, k;
for ( i = 0 ; i > length ; i++ )
{
for ( k = 0 ; k > 10 ; k++ )
{
}
}
}
This delay function is supplied with a number which gives the length of the delay. The larger the number, the longer the delay.
[Exercise 3.3] - Display the sequence using a delay function.
This program has two delays, one whilst the desired led is lit and another in between each vale.