BASIC stamp manual v2.2

5: BASIC Stamp Command Reference – LOOKUP
BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com Page 279
A great use of LOOKUP is in combination with LOOKDOWN to "map"
non-contiguous sets of numbers together. For example, you may have an
application where certain numbers are received by the BASIC Stamp and,
in response, the BASIC Stamp needs to send a specific set of numbers.
This may be easy to code if the numbers are contiguous, or follow some
known algebraic equations… but what if they don't? The table below
shows some sample, non-contiguous inputs and the corresponding
outputs the BASIC Stamp needs to respond with:
Table 5.56: Non-Contiguous
Number Example.
Each of these values received
(inputs):
Needs to result in each of these
values sent (outputs):
5 16
14 17
1 18
43 24
26 10
22 12
30 11
So, if we receive the number 5, we need to output 16. If we received 43,
we need to output 24, and so on. These numbers are not contiguous and
they don't appear to be derived from any simple algorithm. We can solve
this problem with two lines of code, as follows:
LOOKDOWN value, [5, 14, 1, 43, 26, 22, 30], value
LOOKUP value, [16, 17, 18, 24, 10, 12, 11], value
Assuming our received number is in value, the first line (LOOKDOWN)
will find the value in the list and store the index of the location that
matches back into value. (This step "maps" the non-contiguous numbers: 5,
14, 1, etc, to a contiguous set of numbers: 0, 1, 2, etc). The second line
(LOOKUP) takes our new value, finds the number at that location and
stores it back into value. If the received value was 14, LOOKDOWN stores
1 into value and LOOKUP looks at the value at location 1 and stores 17 in
value. The number 43 gets mapped to 3, 3 gets mapped to 24, and so on.
This is a quick and easy fix for a potentially messy problem!
Demo Program (LOOKUP.bs1)
' LOOKUP.bs1
' This program uses Lookup to create a Debug-window animation of a spinning
' propeller. The animation consists of the four ASCII characters | / - \
' which, when printed rapidly in order at a fixed location, appear to spin.
' A little imagination helps a lot here....
U
SING LOOKUP WITH LOOKDOWN TO
"MAP" NON-CONTIGUOUS SETS OF
NUMBERS
.
1