Exploring time with your computer


This set of experiments shows you how to measure and use time in your programs. You will need QBASIC in order to run these experiments. If you have downloaded the seminar materials, then you can also use Delay and Timer from the Programmer's Toolkit.

Run QBASIC

Begin by running the QBASIC program. If you need assistance finding QBASIC on your computer, you can get help here. If you aren't sure how to load and run BASIC programs, check out the Really really basic BASIC course first. When QBASIC is running, you will see this screen:

QBASIC opening screen


TIME$, DATE$, TIMER -- the words for it

BASIC uses three key words to help you keep track of time. To see what they do, enter and run this short program:

Your screen will show the current time, the current date, and the number of seconds since midnight. Notice that TIME$ and DATE$ end in a characteristic dollar sign. This means that they are strings or simply text that gets printed. TIMER is actually a number. To get a continuous printout, add these lines and run your program again.

Both TIME$ and DATE$ get information from the computer's internal clock. You can change these settings in the computer by using the Control Panel and the Date/Time options in Windows. In DOS, just type either DATE$ or TIME$ at the DOS prompt and enter a new value if you wish.

Since TIME$ and DATE$ are strings, you can read or analyze them. The following instructions aren't very interesting, but they will work.

The only use I have found for DATE$ is adding the current date to a printout. The value of the TIMER function is very different. I use this function to measure time and create short delays in many programs.


Got a minute?

Here's an interesting way to use the TIMER function to test your sense of time. When you run this program, the computer loops continuously, waiting until you press a key. The interval since the last time you pressed the key is then printed. You can use this program at least two ways. Press once to start and then press a key again one minute later. If you are on time, the computer will print 60.000 on the screen. For a different challenge, you can also use this program to see if you can tap the keyboard once per second.

In this program I have used the TAB key to indent some of the lines. This doesn't change the program at all as far as the computer is concerned, but it makes things much easier for humans to read. Notice how the DO and LOOP statements are lined up, as are the IF and END IF statements. Indenting like this definitely worth doing because it makes your programs much more clear. The INKEY$ statement detects if any key is pressed and reads: "If the key pressed is not equal to a blank, then..."


Read life signs on your own tri-corder (not Starfleet issue)

OK, so I can't turn your computer into a real tri-corder, but with this program it will read out your most vital life sign of all -- your pulse rate. Just change one line in your program, run it, and then tap the space bar in time with your pulse to calculate you pulse rate in beats per minute. You will have to figure out how to take your own pulse or somebody else's, but that's the only hard part. Tapping the space bar in sync with a pulse takes a little practice.

If you change the 60 to 600 in this program you can press the space bar every ten beats and get the same result. This is easier and more accurate, but not as challenging to do. You will still have to press the space bar once to start the timing interval, and then continue pressing it every ten beats to read your pulse rate in beats per minute.


Use the Toolkit subroutine Delay...

I use delays so often that I have created a special subroutine to make delays especially easy to add anywhere in a program. I find that being able to insert precise delays easily with these instructions below is worth the trouble of writing a special subroutine. As you can see, things couldn't be easier.

Before you can use Delay and a dozen or so other tools, you must load the toolkit. The easiest way to do this is to open the program START1.BAS. This places all the toolkit subroutines in your program automatically when you use Save as... and give your copy of the start program your own title.

Or type the Delay subroutine into your program. If you don't load the toolkit subroutines, you could add the Delay subroutine to your programs by typing in this code. To add this SUB to your BASIC program, follow these steps:

Select New Sub... in the Edit menu.

Type the name Delay and press Enter.

Now fill in the following code:

Press F2 to see the index of all subs, select your program again, and press Enter. Now you can use the command Delay in your program, just like any other BASIC command. For a more complete discussion on creating subroutines, check out this option:


Your own time clock

Every now and then it's handy to be able to add a running clock somewhere on the screen. I decided not to use this technique in the Type-Oh! game series because there is enough suspense without it. In the Newtona 500 program, a time clock is essential for measuring the total time for ten laps.

The toolkit subroutine Timer keeps track of elapsed time and can be reset to zero. Here's how I use this subroutine in the Toolkit demo program.


Seminar homewww.qwerty.com