value in test1. Now that we have the table values surrounding
our input from the GP2D12, we can calculate the slope
between them by taking the difference and dividing by the
span between these points ( 5 centimeters in our test data).
Since we’re doing division and the values on the outer end
of the range get very small, we’ll multiply the difference by
10 before dividing. This will prevent getting a slope value
We’re almost done. The final step is to divide the
difference between our current reading (cVolts) and test2
by the slope, then subtract that from the rough calculation
of distance. Again, we’ll multiply the difference value by 10
— this time to remove the offset introduced by the way we
calculated the slope.
Just to make things crystal clear, let’s work through a
set of numbers. We’ll start with an input voltage of 2.10
volts. The table search will set idx to 1, as this entry (179)
is the first value, less the current value of cVolts. Our rough
calculation of distance, then, is 15 centimeters. At this
point, test2 is indeed less than cVolts, so we have to read
the next lower table value (251) and place this into test1.
Using 251 and 179 for test1 and test2, we get a slope value
of 144; at this point, slope is in millivolts per cm. Using the
BASIC Stamp’s integer math, the difference from our
rough distance calculation works out like this:
((210 – 179) * 10 / 144 = 2
When we subtract 2 from our rough calculation, we end up
with a distance reading of 13 centimeters.
Okay, so much for the theory, how does it work
in practice? I marked up my test rig at one centimeter
intervals and found that it worked pretty well; the readings
across the range were within a centimeter of the actual
distance to my target. I found this perfectly acceptable,
given the (slightly loose) specifications of the GP2D12.
The reason I developed the code I did is that it’s very
easy to plug in different sensor values. I elected to use a
DATA table instead of LOOKUP so that the program can
be more easily expanded with more table entries.
(LOOKUP tables beyond a few values can get unwieldy.)
If you’d like to find a way to plug the voltage value into a
formula in order to get the distance value, I encourage you
to visit the Acroname website and look at their application
note on the GP2D12. That note goes into a very detailed
discussion of finding slope and offset points to linearize the
output from the GP2D12. It’s a little bit complicated and
requires some experimentation, but you may find this
Scare ‘Em, Danno
Before we head out, let’s chat a bit about using the
sensor as I suggested at the beginning of the article. As
I’ve frequently mentioned in the past, we can learn a lot by
mimicking what pros have already done. I was in a public
washroom a few days ago and the sinks had automated
faucets. When one places one’s hands about six inches
from the nozzle, the water starts running.
How would you program the BASIC Stamp to mimic
the faucet control (to apply it to a Halloween display)? This
would be my strategy:
1. Measure distance to target.
2. Is distance less than threshold?
3. If no, go back to Step 1.
4. If yes, check several more times with a delay in between.
5. If target stays in range, trigger the device.
6. Add a [random] delay, allow the prop to run, and reset.
7. Go back to Step 1.
Can you do it? Of course you can — you’re a BASIC
Have a safe and happy Halloween. Until next time,
Happy Stamping. NV
186 processor @ 33 MHz
DOS w/ Flash File system
44 Digital I/O lines w/ CPLD
Console / Debug Serial Port
7-34V DC or 5V DC power 2 Serial Ports
Accepts 8MB DiskOnChip 2 16-bit Timers
512K DRAM & 512K Flash Watchdog Timer
Expansion options with Peripheral Boards
Development kit includes:
Flashlite 186 controller
Borland C/C++ compiler v4.52
FREE Email Technical Support
Serial Driver library
AC Adapter and cable
Manual and Schematic
Call 530-297-6073 Email sales@ jkmicro.com
On the web at www.jkmicro.com
Circle #152 on the Reader Service Card.