------------------------------------------------------------"Thousand Dollar Alarm Clock" from November, 1989------------------------------------------------------------ I guess everybody has dreams of making a million dollars. Iknow I have, and I have had hundreds of money-making ideasin the past. But for some reason, none of them has paid offyet. For the life of me, I cannot figure out why. For example, I was sure my inflatible dart board was goingto be a big hit, but none of the toy companies wereinterested. I worked for years on freeze dried water, but Icould never find the right formula. I just knew ediblesilverware would be a success, but my test subjects consumedan entire drawerful of forks with every meal. I had another terrific idea for this month's column: why notwrite a BASIC program for the Tandy 1000 that will simulatethe operation of a digital alarm clock? With the properprogram, we can take an amazingly powerful home, business,and educational computer that costs one thousand dollars ormore, and have it duplicate the function of a ten-dollaralarm clock. To be honest, that idea does not look quite as good now thatI see it in black and white. In fact, it stinks. But, Ihave already written the program, and my deadline is fastapproaching, so what the heck, let's use it anyway. Actually, I am being facetious. Although this month'sprogram may not be as exciting as some of my past offerings,it does serve a useful purpose ... and it also illustrates afew interesting BASIC programming tricks. Time to Begin ------------- What happens after you type RUN? The program must firstlearn how to draw big, two-inch high pictures of the digitszero through nine. It does that using a neat trick. Eachdigit is printed in normal size in the upper left handcorner of the screen. The POINT command is used to seewhich of the pixels (or video "dots") in that area of thescreen are turned on. It uses that information to constructa bigger picture of the digit in the center of the screenusing boxes (about 1/4" wide) in place of each dot. The resulting picture is stored using the GET command. Ifyou check in your BASIC manual, you will find that thekeyword GET is used for two very different purposes. In onesituation, GET is used to retrieve information from a randomaccess disk file; that is not the one we are interested in.GET can also be used to grab and store a portion of thevideo display. By telling the GET command what portion ofthe screen to look at, you can in effect make a "rubberstamp" of that section, which can be reprinted over and overagain anywhere you wish on the screen. The PUT command also has two purposes. In one situation, itis used to send data to a random access disk file. Inaddition, the PUT command is the one we will use to displaythat "rubber stamp" created by GET. When you GET a pictureand store it in an array variable, you can display itanywhere you want on the screen using the PUT command. Eventhough GET and PUT involve the transfer of a large amount ofdata, they operate very quickly. In fact, I was somewhatworried before I wrote this program that the graphic portionwould not be fast enough to keep up; it must re-display thetime once each second. However, there was no reason toworry. GET, PUT, and Tandy BASIC are more than adequate forthis task. So once the program has "learned" how to draw the digits,what happens next? The screen is cleared, a frame is drawn,and the time is displayed in hour/minute/second format usingthose big numerals. Keeping Time ------------ You might be wondering about another question: how doesBASIC know what time it is? Easy! When you boot up yourcomputer and enter the time and date, both are stored in aspecial place in your Grandy's memory. If you have a smartclock chip, then the time is always automaticallytransferred to that memory location. The time is updatedonce a second by your computer's central processing unit(CPU). In BASIC, you can PRINT or examine a special stringcalled TIME$ to see what time it is. TIME$ always returns an eight-character-long string in theform HH:MM:SS, where HH represents the hour, MM representsthe minutes, and SS represents the seconds. For example,when it is exactly thirty-four minutes and fifty-six secondspast noon, TIME$ will be equal to "12:34:56". TIME$ usesmilitary time, so at 10:00 at night, TIME$ will return"22:00:00". Obviously, the program uses TIME$ to know which numerals todisplay on the screen. But is that all the program does --display the current time? No, there is also an alarmfunction. If you press the letter "A", then you will beprompted to select a time for the alarm to sound. The alarmtime is initially set to noon, but you can change the hourof the alarm by pressing the "greater than" and "less than"keys (or the comma and period keys). Select the minutessetting using the left and right arrow keys. Once the alarm time setting is correct, you must pressENTER. Now sit back and relax. Once the time reaches thealarm setting, your computer will "beep" at you until youpress any key to acknowledge the alarm. After that, theprogram returns to its original state -- quietly displayingthe time second by second. When you have had enough, pressESC to exit the program. Programming Details ------------------- Take a look at the program listing to see how everything isaccomplished. Since this program requires the use ofgraphics, a graphic screen mode is required. We will usescreen mode 1 -- a medium resolution mode -- as set in line160. The eleven arrays dimensioned in lines 170 and 180will be used by the GET statements. Array A will hold apicture of the numeral zero; array B will hold the numeralone; array C will hold the numeral two; and so on. Arrayvariable K will hold a picture of the colon. Lines 230 through 330 enable the computer to "learn" how todisplay those big pictures of the numerals. Each call tosubroutine 960 transforms the character passed in variableA$ into a big two-inch high picture. The GET statements atthe end of each line move the picture into the proper arrayvariable. (By the way, I actually used the letter "O"instead of the number zero inside the quotation marks inline 230. I like the way the "O" looks -- without the slashthat runs diagonally through the number zero.) Line 370 draws the frame for the clock. Lines 380 and 390display two short messages. The program spends most of itstime in a loop defined by lines 430 to 460. The call tosubroutine 780 displays the time. If the current timematches the alarm time, then the program branches to line730. If the user presses ESC, then it exits in line 450. Bythe way, if you would like the program to return to the DOSA> prompt when you press ESC, replace the command END inline 450 with the command SYSTEM. If the user presses "A" to set the alarm, then lines 500through 690 perform that function. Lines 500 through 540print some messages. Lines 550 through 580 construct thestring ALARM$ using the current values of HR and MN (forhour and minute). The alarm setting is displayed in line590. Lines 640 through 690 make the proper adjustments toHR and MN based on the user's keystrokes. Lines 730 and 740 are called when the alarm time is reached.The PLAY statement is used to provide a couple of shortbeeps. The WHILE-WEND loop allows the beeping to continueuntil a key is pressed. Subroutine 780 actually displays the time in big characters.The FOR-NEXT loop examines each of the eight characters inthe HH:MM:SS time string, then branches to one of the lines820 through 920 to display the proper character stored inarray variables A through K. Finally, subroutine 960 isused early in the program to transform the characterdisplayed in the upper left corner of the screen into alarger graphic picture in the center of the screen. Wrapping Up ----------- How might you use this program? I enjoy telecommunicatingusing my modem. But when all the local bulletin boardservices (BBS) are busy, I take a ten or fifteen minutebreak and read a story or play a game with my daughter.During that time, rather than turn off the computer, I startup CLOCK and set the alarm to remind me to come back and tryto connect with those BBS systems again later. My family plays a lot of board games. Some of them are evenmore fun when we play against the clock. To keep them fromrunning on too long, we start up CLOCK and set the alarm fora mutually agreed upon ending point. When the alarm sounds,the game ends, we count up the points or the money, and seewho won. There can be no doubt about the ending time wheneveryone hears the beep. In addition to its intended purpose -- to keep time -- youmight also learn something about BASIC and find some usefulsubroutines that you could use in your programs. Here is aninteresting experiment: RUN the program, then press ESC toexit to BASIC's OK prompt. Then type SCREEN 1: A$="X":GOSUB 960 then press ENTER. You will see the letter "X"displayed in the large graphic format. Substitute anycharacter you wish for the "X" and try it; don't forget youcan also use A$ = CHR$(x) where x is any number between 1and 255 to define the character to be displayed. As always, I look forward to hearing from you. Send yourcomments, suggestions for future columns, and questionsabout past columns to me in care of ONE THOUSAND magazine.See you next month!