|
本帖最後由 a26265788 於 2018-3-19 11:02 編輯
我用的是PARALLAX的PING超音波
他只有GND.VCC.SIG三根腳位
SIG腳位我改在digital 7
下面是他們提供的程式
我有單獨測試超音波程式有成功回傳距離回來
- const int pingPin = 11;
- unsigned int duration, inches;
- void setup() {
- Serial.begin(9600);
- }
- void loop() {
- pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
- digitalWrite(pingPin, LOW); // Ensure pin is low
- delayMicroseconds(2);
- digitalWrite(pingPin, HIGH); // Start ranging
- delayMicroseconds(5); // with 5 microsecond burst
- digitalWrite(pingPin, LOW); // End ranging
- pinMode(pingPin, INPUT); // Set pin to INPUT
- duration = pulseIn(pingPin, HIGH); // Read echo pulse
- inches = duration / 74 / 2; // Convert to inches
- Serial.println(inches); // Display result
- delay(200); // Short delay
- }
複製代碼
如果單獨跑上面程式
並把 duration = pulseIn(pingPin, HIGH); // Read echo pulse
這一段改成
並把 duration = pulseIn(pingPin, HIGH,50); // Read echo pulse
回傳值就都變成0 |
|