Parallax BASIC Stamp 2e Programming Manual page 184

Table of Contents

Advertisement

LOOKDOWN - BASIC Stamp Command Reference
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}
SYMBOL
I
=
W0
SYMBOL
Result =
W1
FOR I = 0 TO 100
Result = 255
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}
I
VAR
WORD
NumDig
VAR
NIB
FOR I = 0 TO 1000 STEP 8
NumDig = 5
LOOKDOWN I, <[0, 10, 100, 1000, 10000, 65535], NumDig
DEBUG "I= ", REP " " \ ( 5 – NumDig ), DEC I, TAB, "digits=", DEC NumDig, CR
PAUSE 100
NEXT
Page 182 • BASIC Stamp Programming Manual 2.0b • www.parallaxinc.com
'STAMP directive (specifies a BS1)
' Holds current number.
' Holds mapped result.
' If no match in list, must be 0.
'STAMP directive (specifies a BS2)
' Variable (0-65535).
' Variable (0-15) to hold # of digits.
' If no 'true' in list, must be 65535.
1
2
2
2
2
e
sx
p
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.

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the BASIC Stamp 2e and is the answer not in the manual?

Subscribe to Our Youtube Channel

Table of Contents