سؤال

كنت مجرد النظر في بعض رمز المثال واتصل عبر خط، لا أفهم تماما سبب حاجة إليه.أنا أفهم أنك تأخذ في قيمة تناظري.هذه القيمة بين 0 و 1024 على ما يبدو؟لماذا هذا؟لماذا يجب تعيين الناتج بين 0 و 255؟ما الذي يملي الحجج المستخدمة هنا؟الخط المعني: giveacodicetagpre.

أبرزت في الرمز: giveacodicetagpre.

شكرا جزيلا للردود.

هل كانت مفيدة؟

المحلول

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: 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top