Hardware Reference
In-Depth Information
Figure 4-9.
The map function is responsible for scaling a value from one set
of ranges to another. In the preceding example, if the input scale went from
0 to 1023 and the output scale went from 0 to 100, inputting the value 256
would return 25.
Knowing what the result of this line of code is without looking at any reference
information, you might be able to tell what each parameter is. If not:
The syntax of
map()
is:
map(input, inFrom, inTo, outFrom, outTo)
The parameters of
map()
are:
•
input
: the input value to be scaled
•
inFrom
: the first number in the input scale
•
inTo
: the second number in the input scale
•
outFrom
: the first number in the output scale
•
outTo
: the second number in the output scale
map()
returns a value on the scale of
outFrom
to
outTo
.
To work with
map()
on your own, try modifying
Example 4-2
so that it outputs
the approximate voltage going into the analog input pin. You can also try
taking analog input reading and remap it to an analog output pin.
Variable Resistors
While there are many that do, not all analog sensors work like the potenti-
ometer does, which provides a variable voltage based on some factor (such
as the amount the dial on the pot is turned).
Some sensors are simply variable resistors that resist the flow of electricity
based on some factor. For instance, a photocell like the one in
Figure 4-10
will
act like a resistor that changes values based on the amount of light hitting
the cell. Add more light and the resistance goes down. Take away light and
the resistance goes up. The force sensitive resistor decreases its resistance
as you put pressure on the pad. You'll learn more about this in
“Force Sensi-
tive Resistor” on page 84
.