Hardware Reference
In-Depth Information
This function takes a value called
value
in a range between
fromLow
and
fromHigh
, and remaps that value to a new range set by
toLow
and
toHigh
.
The clearest way to explain
map()
is with an example. Imagine a sensor, con-
nected to an analog pin. It outputs numbers from 0 to 1,023. How would you
convert this to a percentage? The
map()
function could do this in a single line.
result = map(sensorData, 0, 1023, 0, 100);
Mapping can also be used to invert value ranges:
result = map(sensorData, 1, 50, 50, 1);
pow()
pow()
raises a number to the power of
x
.
double result = pow(float base, float exponent);
The base number and exponent are calculated as
float
, allowing for fractional
exponents. The result of this calculation is returned as a
double
.
sqrt()
sqrt()
calculates the square root of a number.
double result = sqrt(x);
The number
x
can be of any numerical data type, and the result is expressed
as a double.
random()
Arduinos are capable of generating pseudo-random numbers using the
ran-
dom()
function:
result = random(max);
result = random(min, max);
This function takes one or two parameters specifying the range for the ran-
dom number to be chosen. If the
min
parameter is omitted, the result will be a
number between zero and
max
, otherwise the number will be between
min
and
max
. The
result
is returned as a
long
.
Computers cannot generate purely random numbers, and instead use com-
plex algorithms. While the output may indeed seem random, it is actually a
sequence that is extremely long but always the same. To prevent your Arduino
Search WWH ::
Custom Search