EasyManua.ls Logo

Parallax BASIC Stamp 2e - Page 184

Default Icon
353 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
LOOKDOWN - BASIC Stamp Command Reference
Page 182 BASIC Stamp Programming Manual 2.0b www.parallaxinc.com
Demo Program (LOOKDOWN.bas)
' This program uses LOOKDOWN followed by LOOKUP to map the numbers:
' 0, 10, 50, 64, 71 and 98 to 35, 40, 58, 62, 79, and 83, respectively. All other
' numbers are mapped to 255.
'{$STAMP BS1} 'STAMP directive (specifies a BS1)
SYMBOL I = W0 ' Holds current number.
SYMBOL Result = W1 ' Holds mapped result.
FOR I = 0 TO 100
Result = 255 ' If no match in list, must be 0.
LOOKDOWN I, (0, 10, 50, 64, 71, 98), Result
LOOKUP Result, (35, 40, 58, 62, 79, 83), Result
DEBUG "I= ", #I, "Result=", #Result, CR
PAUSE 100
NEXT
Demo Program (LOOKDOWN.bs2)
' This program uses LOOKDOWN to determine the number of decimal digits in a number.
' The reasoning is that numbers less than 10 have one digit; greater than or equal
' to 10 but less than 100 have two; greater than or equal to 100 but less than 1000
' have three; greater than or equal to 1000 but less than 10000 have four; and greater
' than or equal to 10000 but less than 65535 (the largest number we can represent in
' 16-bit math) have five. There are two loopholes that we have to plug: (1) The number
' 0 does not have zero digits, and (2) The number 65535 has five digits. To ensure that
' 0 is accorded one-digit status, we just put 0 at the beginning of the LOOKDOWN list.
' Since 0 is not less than 0, an input of 0 results in 1 as it should. At the other end
' of the scale, 65535 is not less than 65535, so LOOKDOWN will end without writing to the
' result variable, NumDig. To ensure that an input of 65535 returns 5 in NumDig, we just
' put 5 into numDig beforehand.
'{$STAMP BS2} 'STAMP directive (specifies a BS2)
I VAR WORD ' Variable (0-65535).
NumDig VAR NIB ' Variable (0-15) to hold # of digits.
FOR I = 0 TO 1000 STEP 8
NumDig = 5 ' If no 'true' in list, must be 65535.
LOOKDOWN I, <[0, 10, 100, 1000, 10000, 65535], NumDig
DEBUG "I= ", REP " " \ ( 5 NumDig ), DEC I, TAB, "digits=", DEC NumDig, CR
PAUSE 100
NEXT
1
2
e
2
sx
2
p
2
NOTE: This is written for the BS2
but can be used for the BS2e,
BS2sx and BS2p also. Locate the
proper source code file or modify
the STAMP directive before
downloading to the BS2e, BS2sx or
BS2p.

Table of Contents

Related product manuals