I was just looking at some example code and came across a line, I don't fully understand why it needs to be done. I understand that you are taking in an analog value. This value is between 0 and 1024 apparently? Why is this? Why does the output need to be mapped between 0 and 255? What dictates the arguments that are used here? The line in question :
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1024, 0, 255);
Highlighted in the code :
created 29 Dec. 2008
Modified 4 Sep 2010
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
**// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1024, 0, 255);**
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}
Thanks very much for the replies.
map() loops over the items of an input iterable (or iterables) and returns an iterator that results from applying a transformation function to every item in the original input iterable. map() applies function to each item in iterable in a loop and returns a new iterator that yields transformed items on demand.
The map function has two arguments (1) a function, and (2) an iterable.
The maximum input voltage is 5V, and the analog input readings are returned as 10-bit integer values. The maximum numerical value for a (unsigned) 10-bit number is 1023. Therefore, when the maximum input of 5 V is applied to one of the input pins, the maximum numerical value for an analog reading is 1023.
Maps are part of the C++ STL (Standard Template Library). Maps are the associative containers that store sorted key-value pair, in which each key is unique and it can be inserted or deleted but cannot be altered. Values associated with keys can be changed.
The analog output only has an acceptable range between 0 and 255.
Therefore, the value has to be mapped within the acceptable range.
Documentation for map method is here: http://arduino.cc/en/Reference/map
Because the Arduino has an analogRead resolution of 0-1023, and an analogWrite resolution of only 0-255, this raw data from the potentiometer needs to be scaled before using it...
This explanation comes from an Arduino sensor tutorial(under the 'Code' header): http://arduino.cc/en/Tutorial/AnalogInOutSerial
Why? Sometimes you will need to translate 0 to 1023 into a range of values OTHER THAN 0 to 1023 and the map()
function is an attempt to make this easier for you, the engineer. I explain one situation in some detail on this forum post, where I am able to convert the 0 to 90 or 100 indexes of an array having values of 0 to 1023 integers into an x-y graphical plot!
idx
ranges from 0 to some value near 100.test[idx]
is the ADC values, so range from 0 to 1023.
int x1= map(1, 0, idxmax, 0, 160);
int y1= yf - 2 - map(test[1], TPS_floor[_tps], TPS_max[_tps], 0, dy);
for(idx=0; idx < idxmax-1; ){
int x0 = map(idx, 0, idxmax, 0, 160);
int y0 = yf - 2 - map(test[idx], TPS_floor[_tps], TPS_max[_tps], 0, dy);
tft.drawLine(x0, y0, x1, y1, YELLOW);
idx++;
x1 = map(idx+1, 0, idxmax, 0, 160);
y1 = yf - 2 - map(test[idx+1], TPS_floor[_tps], TPS_max[_tps], 0, dy);
}
So the above code translates an x of 0-~100 and y of 0-1023 into this:
My build's write up is here. (and as of 7-31-2013, is in progress)
I, personally, find that a clear illustration of "why" is the best explanation. I hope that my answer helps anyone questioning this "why" as to ... why.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With