Hardware Reference
In-Depth Information
pin : the analog pin number to read (0 to 5).
analogRead() returns an integer between 0 and 1023 (representing 0 to 5
volts).
If you changed the IOREF jumper so that you're using 3.3 volts,
you must not send more than 3.3 volts to the analog input pins.
It's also important to know that the returned value from
analogRead() will still be on the scale of 0 to 1023 to represent
0 to 5 volts, even if IOREF is set to 3.3 volts.
Code and Syntax Notes
Like previous examples, Example 4-2 also introduces a few more program-
ming concepts to help you along your way.
Constants
To store the pin numbers in memory, you previously used variables. However,
in Example 4-2 you tried something a little different:
const int potentiometerPin = 0;
This syntax creates a spot in memory for an integer called potentiometer
Pin and stores 0 in it, just like a variable. Except this isn't a variable, it's a
constant . After you assign it the value when you initialize it, you cannot
change the value again. If you try to, you'll receive an error when you compile
the sketch.
This might be helpful if you want to store a value in memory and you know
that it should never be changed. Since the compiler won't compile it and will
return an error, you'll know when you've done something wrong in your code.
map()
Because the return value of analogRead() is a value between 0 and 1023,
you'll sometimes need to scale that value to another range (see Figure 4-9 ).
In the case of Example 4-2 , you used the map() function to scale that to a
percentage (0 to 100) with the following code:
int displayValue = map(sensorReading, 0, 1023, 0, 100);
 
Search WWH ::




Custom Search