Arduino map() 方法 - 为什么?

发布于 2024-12-29 07:57:22 字数 1531 浏览 4 评论 0原文

我只是在看一些示例代码并遇到了一行,我不完全理解为什么需要这样做。我知道您正在接受模拟值。这个值显然在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

才能让你更想念 2025-01-05 07:57:22

模拟输出的可接受范围仅在 0 到 255 之间。

因此,该值必须映射到可接受的范围内。

地图方法的文档在这里:http://arduino.cc/en/Reference/map

由于 Arduino 的 AnalogRead 分辨率为 0-1023,而 AnalogWrite 分辨率仅为 0-255,因此来自电位计的原始数据需要在使用前进行缩放...

这个解释来自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

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

忘东忘西忘不掉你 2025-01-05 07:57:22

为什么?有时您需要将 0 到 1023 转换为 0 到 1023 以外的一系列值,而 map() 函数试图让您(工程师)更轻松地完成此操作。 我在此论坛帖子中详细解释了一种情况,我可以将具有 0 到 1023 整数值的数组的 0 到 90 或 100 索引转换为 xy 图形!

idx 范围从 0 到接近 100 的某个值。
test[idx] 是 ADC 值,因此范围为 0 到 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);
}

因此上面的代码将 0-~100 的 x 和 0-1023 的 y 转换为:
map() 将数组的索引及其值转换为该图!

我的构建的 写在这里。 (截至 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.

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:
map() translated an array's index and its values into that plot!

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文