2012-01-26 11 views
7

मैं बस कुछ उदाहरण कोड देख रहा था और एक रेखा में आया, मुझे पूरी तरह से समझ में नहीं आता कि इसे क्यों किया जाना चाहिए। मैं समझता हूं कि आप एक अनुरूप मूल्य ले रहे हैं। यह मान स्पष्ट रूप से 0 और 1024 के बीच है? ऐसा क्यों है? 0 और 255 के बीच आउटपुट को मैप करने की आवश्यकता क्यों है? यहां उपयोग किए गए तर्कों का क्या निर्देश है? सवाल में लाइन:Arduino मानचित्र() विधि - क्यों?

// 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);      
} 

धन्यवाद।

उत्तर

11

0 और 255 के

के बीच इसलिए, मूल्य स्वीकार्य सीमा में मैप किया गया है एनालॉग आउटपुट केवल एक स्वीकार्य सीमा है। नक्शा विधि के लिए

प्रलेखन यहाँ है: http://arduino.cc/en/Reference/map

क्योंकि Arduino 0-1023 के analogRead संकल्प, और केवल 0-255 के analogWrite संकल्प, पोटेंशियोमीटर से कच्चे डेटा बढ़ाया जा करने की जरूरत है उपयोग करने से पहले ...

इस विवरण ('कोड' शीर्षक के अंतर्गत) एक Arduino सेंसर ट्यूटोरियल से आता है: http://arduino.cc/en/Tutorial/AnalogInOutSerial

+3

कोई तर्क दे सकता है कि 4 से पूर्णांक विभाजन भी काम करेगा, लेकिन नक्शा() ठीक काम करता है। – Mchl

+3

पकड़ने वाले हाथ पर एक पूर्णांक को 2- –

+0

द्वारा सही-स्थानांतरित कर सकता है यदि उत्पादन 0-2524 के बीच होना चाहिए तो एनालॉग इनपुट 0-1024 क्यों होना चाहिए? –

1

क्यों? कभी-कभी आपको मूल्यों की एक श्रृंखला में 0 से 1023 का अनुवाद करने की आवश्यकता होगी अन्य 0 से 1023 और map() फ़ंक्शन आपके लिए यह आसान बनाने का प्रयास है, इंजीनियर। I explain one situation in some detail on this forum post, जहां मैं एक एक्स-वाई ग्राफिकल साजिश में 0 से 1023 पूर्णांक के मान वाले 0 से 9 0 या 100 इंडेक्स को सरणी में परिवर्तित करने में सक्षम हूं! 0 से 100 के
test[idx] के पास कुछ मूल्य को

idx पर्वतमाला एडीसी मान, इसलिए 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 और का एक्स है 0-1023 में y: map() translated an array's index and its values into that plot!

मेरा निर्माण write up is here है। (और 7-31-2013 के रूप में, प्रगति पर है)

मैं, व्यक्तिगत रूप से, यह पाया कि "क्यों" सबसे स्पष्ट स्पष्टीकरण है। मुझे उम्मीद है कि मेरा जवाब किसी को भी "क्यों" पूछताछ करने में मदद करता है ... क्यों।

+0

बहुत ही रोचक और जानकारीपूर्ण।अभी भी मंच पोस्ट के माध्यम से अपना रास्ता काम कर रहा है, लेकिन मुझे यह बहुत अवशोषित लगता है! –

+1

धन्यवाद, साइमन! कुछ प्रकार के स्केलिंग फ़ंक्शन के रूप में 'मानचित्र() 'के बारे में सोचें ...? उस मंच पर प्रश्न पूछने के लिए आपका स्वागत है। :) –