Hardware Reference
In-Depth Information
Note that, as with all subprograms, the procedure is declared at the beginning
of the main code and defined at the end.
Now, to take a more complex example, let's consider the case of a main menu
selection. Suppose we are dealing with a control system which has four main
functions (each of which is to be handled by a secondary menu) together with
a function which closes down the system and exits from the program. The five
main functions will be as follows:
1 Set parameters
2 Heater control
3 Pump control
4 Print report
5 Close down
The following code can be used for the main program loop:
DECLARE SUB Setparams()
DECLARE SUB Heatcontrol()
DECLARE SUB Pumpcontrol()
DECLARE SUB Printreport()
DECLARE SUB Closedown()
REM Main menu selection
WHILE 1
CLS
LOCATE 3, 36
PRINT "MAIN MENU"
LOCATE 6, 30
PRINT "[1] Set parameters"
LOCATE 8, 30
PRINT "[2] Heater control"
LOCATE 10, 30
PRINT "[3] Pump control"
LOCATE 12, 30
PRINT "[4] Print report"
LOCATE 14, 30
PRINT "[5] Close down"
LOCATE 16, 30
PRINT "Option required (1-5)?"
DO
r$ = INKEY$
k% = VAL(r$)
LOOP UNTIL k% < 6 AND k% > 0
IF k% = 1 THEN CALL Setparams
IF k% = 2 THEN CALL Heatcontrol
IF k% = 3 THEN CALL Pumpcontrol
IF k% = 4 THEN CALL Printreport
IF k% = 5 THEN CALL Closedown
WEND
Notice that the main program loop consists of an infinite WHILE ... WEND loop.
The single character string returned by INKEY is converted to an integer and then
tested to see whether it is within range of the valid keyboard responses (note
that depressing the first key returns a value in k% of 1, and so on). The DO ...
LOOP is only exited when a valid keystroke is detected. Having obtained a valid
keystroke, the program checks the response to see which key was depressed
Search WWH ::




Custom Search