You have decided that a problem with your torch is that if someone leaves it switched on the battery goes flat. What you need to do is change your light switching program so that if the light is on too long it turns off automatically.

To do this you will use a counter which is set to 0 when the torch is turned off. When the torch is turned on we increment the counter each time round our loop. When the counter hits a particular value we turn the torch off.

You may have a problem with the speed of the PICmicro. It can count very quickly, so it will hit the end of one counter before your light has been on for a decent length of time. To solve this you must put one counter inside another. When the inner counter reaches a limit you increase the outer counter by one and reset the inner to 0. You then continue until the outer value reaches a limit.

You should check the counter when you are waiting for the pin to change state. This means that you now have something to do in the while loops.

I found that setting both the inner and the outer counters at 100 gives around 8 or 9 seconds of light.

If you run program 2.5 I have invented functions which we may use later when we want to time things. The function do_time sorts out what we need to do while we wait for key presses. The function start_clock clears the counters. We call start_clock when we turn the light on.

Note that do_time checks to see if the light is on before it updates the counters. This stops us wasting time if the light is not on.

You can learn a lot from studying this code. You might even decide that you can think of a better way of doing the job. If you do decide this, it means that you are learning. I do not pretend that this is the best way of doing the job, but it does work and I haven't found any bugs in it.