有關於wiringPi 的安裝和執行 請參考這篇 wiringPi
然後我發覺我超音波做不出來的原因是因為我理解錯誤....
參考timing diagram
重點是要去數 第三列的pulse為高電位的時間......
int ultrasonic_read(_pin){
unsigned int start_time,end_time,duraction;
pinMode (_pin, OUTPUT) ;
digitalWrite (_pin, LOW) ;
delayMicroseconds(3);
digitalWrite (_pin, HIGH) ;
delayMicroseconds(11);
digitalWrite (_pin, LOW);
pinMode (_pin, INPUT) ;
while(digitalRead(_pin)==LOW)
delayMicroseconds(1);
start_time = micros();
while(digitalRead(_pin)==HIGH)
delayMicroseconds(1);
end_time = micros();
if(start_time > end_time)
duraction = (unsigned int)4294967295 - end_time + start_time;
else
duraction = end_time - start_time;
return (0.17 * duraction);
}
把跑出來的資料plot 出來....結果就像下圖一樣