超聲波模組就是透過發射與接收 (Trig/Echo) 音頻的原理來計算距離的。使用方法參考店家對 US-100 型號的說明:
一個控制口發一個10US以上的高電位,就可以在接收口等待高電平輸出;一有輸出就可以開定時器計時,當此口變為低電位時就可以讀定時器的值,此時就為此次測距的時間,方可算出距離。如此不斷的週期測,就可以達到你移動測量的值了。
這裡我整理了兩個測試程式。其中第一種方式是直接針對腳位作電位的改變,但需要自行將往返的時間 (每公分的音頻速度為 29 微秒);第二種方式則是透過 <<NewPing.h>> 函式庫,採以 Wrapper 較高階的作法來取得音頻的距離。
實際觀察方式就是透過 Serial Port 回傳所感測的距離資料,所以在 Arduino IDE 環境中,需點選【工具】→ 【序列埠監視窗】開啟監視窗觀察測試的結果。
UltraSoniceTest1.ino:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | const byte trigPin = 9; // Trig 接腳編號 const byte echoPin = 10; // Echo 接腳編號 unsigned long duration; // 儲存高脈衝的持續時間 unsigned long ping() { digitalWrite(trigPin, HIGH); // 觸發腳位設定成高電位 delayMicroseconds(5); // 持續 5 微秒 digitalWrite(echoPin, LOW); // 接收腳位設定成低電位 return pulseIn(echoPin, HIGH); } void setup() { pinMode(trigPin, OUTPUT); // 觸發腳位設為輸出 pinMode(echoPin, INPUT); // 接收腳位設為輸入 Serial.begin(9600); // 初始化序列埠 } void loop() { duration = ping() / 58; // 把高脈衝時間值轉換為公分單位 // The speed of sound is 29 microseconds per centimeter. Serial.print(duration); // 顯示距離 Serial.print("cm"); Serial.println(); delay(1000); // 延遲一秒鐘再重新測量 } |
UltraSoniceTest2.ino:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <NewPing.h> //library for the US-100 ultrasonic sensor // ----小車馬達的接腳編號定義---- #define TRIG_PIN 9 // Trig 接腳編號 #define ECHO_PIN 10 // Echo 接腳編號 #define MAX_DISTANCE 200 //the sensor can't measure much further then this distance NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); int uS; // value of US-100 ultrasonic sensor int distance; //distance in cm of ultrasonic sensor void setup() { Serial.begin(9600); // 初始化序列埠 } void loop() { getDistance(); delay(1000); // 延遲一秒鐘再重新測量 } void getDistance() { uS = sonar.ping(); distance = uS / US_ROUNDTRIP_CM; if (uS == NO_ECHO) // if the sensor did not get a ping { distance = MAX_DISTANCE; //so the distance must be bigger then the max vaulue of the sensor } Serial.print("Ping: "); //to check distance on the serial monitor Serial.print(distance); Serial.println("cm"); } |
至於紅外線複眼 (IR Compound Eye)模組,是用四對紅外線LED陣列來實現光感應功能;此感應或實現:追蹤,循跡,避障等功能。
該模組共需用到7條接腳,相對麻煩許多。與超聲波模組不同的是,它可以感測前方屏障物上下左右四個角度的距離;但它的靈敏度與準確性也比較不足,而且很容易受到外界光線與鄰近裝置的干擾。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #define IRup 1 // Compound Eye Up - analog input A1 #define IRleft 2 // Compound Eye Left - analog input A2 #define IRdown 3 // Compound Eye Down - analog input A3 #define IRright 4 // Compound Eye Right - analog input A4 #define IRleds 8 // Compound Eye LEDs - digital output D8 // IR Sensors - sensori IR int updown; int leftright; int leftIRvalue; int rightIRvalue; int upIRvalue; int downIRvalue; void setup() { Serial.begin(9600); pinMode (IRleds,OUTPUT); } void loop() { IReye(); delay(1000); } // =====Read IR compound eye===== void IReye() { digitalWrite(IRleds,HIGH); delay(5); leftIRvalue=analogRead(IRleft); rightIRvalue=analogRead(IRright); upIRvalue=analogRead(IRup); downIRvalue=analogRead(IRdown); delay(2); digitalWrite(IRleds,LOW); delay(5); leftIRvalue=leftIRvalue-analogRead(IRleft); rightIRvalue=rightIRvalue-analogRead(IRright); upIRvalue=upIRvalue-analogRead(IRup); downIRvalue=downIRvalue-analogRead(IRdown); Serial.print("left: "); Serial.println(leftIRvalue); Serial.print("right: "); Serial.println(rightIRvalue); Serial.print("up: "); Serial.println(upIRvalue); Serial.print("down: "); Serial.println(downIRvalue); Serial.println(""); } |