Arduino map() 方法 - 为什么?
我只是在看一些示例代码并遇到了一行,我不完全理解为什么需要这样做。我知道您正在接受模拟值。这个值显然在 0 到 1024 之间?这是为什么呢?为什么输出需要映射在0到255之间?是什么决定了这里使用的参数?有问题的行:
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1024, 0, 255);
在代码中突出显示:
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);
}
非常感谢您的回复。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模拟输出的可接受范围仅在 0 到 255 之间。
因此,该值必须映射到可接受的范围内。
地图方法的文档在这里:http://arduino.cc/en/Reference/map
这个解释来自Arduino传感器教程(在“代码标头): http://arduino.cc/en/Tutorial/AnalogInOutSerial
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
This explanation comes from an Arduino sensor tutorial(under the 'Code' header): http://arduino.cc/en/Tutorial/AnalogInOutSerial
为什么?有时您需要将 0 到 1023 转换为 0 到 1023 以外的一系列值,而
map()
函数试图让您(工程师)更轻松地完成此操作。 我在此论坛帖子中详细解释了一种情况,我可以将具有 0 到 1023 整数值的数组的 0 到 90 或 100 索引转换为 xy 图形!idx
范围从 0 到接近 100 的某个值。test[idx]
是 ADC 值,因此范围为 0 到 1023。因此上面的代码将 0-~100 的 x 和 0-1023 的 y 转换为:

我的构建的 写在这里。 (截至 2013 年 7 月 31 日,正在进行中)
我个人认为,清楚地说明“为什么”是最好的解释。我希望我的回答可以帮助任何质疑这个“为什么”的人……为什么。
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.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.