| 
 | 
 本帖最後由 vegewell 於 2011-9-27 18:43 編輯  
 
回復 11# zero7386  
 
 
    這個GP2Y0A02YK0F紅外測距傳感器, 
探測距離:20-150cm, 
你測20公分距離的物品,在臨界點,難免有混淆的狀況, 
請你用以下的程式,(先copy到計事本,再copy到arduino編輯視窗) 
把欲測量的物品放在30公分距離,測量後,然後放在100公分距離, 
測試看看出來的值,是否都很準或接近? 
 
#define sensorPin 0 
#define VOLTS_PER_UNIT    .0049F        // (.0049 for 10 bit A-D) 
float volts; 
float proxSens = 0; 
int cm; 
void setup() 
 { 
   beginSerial(9600); 
   pinMode(sensorPin, INPUT); 
  } 
void loop() 
 { 
    proxSens = analogRead(sensorPin); 
     volts = (float)proxSens * VOLTS_PER_UNIT; // ("proxSens" is from analog read) 
     cm = 60.495 * pow(volts,-1.1904);     // same in cm 
     Serial.print(cm);  
     Serial.print(' '); 
  } |   
 
 
 
 |