pulseIn()

描述

讀取 pin 腳上的脈衝 (高或低電位) 持續時間;舉個例子,如果輸入值是 HIGH,pulseIn() 會等 pin 腳上的電位變成 HIGH 之後開始計時,等到又變回 LOW 才停止;之後會回傳脈衝的持續時間,單位是微秒;假如在指定的時間內沒有出現任何脈衝或是脈衝長度太長,則會放棄讀取並回傳 0。

指定時間的長短是取決於經驗的,通常在長時間的脈衝下較容易發生錯誤,脈衝的測量長度介於 10 毫秒到 3 分鐘之間。

語法


pulseIn(pin, value)
pulseIn(pin, value, timeout)

參數

pin: 用來讀取脈衝的 pin 腳 (int)
value: 讀取的脈衝型態,HIGHLOW (int)
timeout (非必要): 設定一段時間來等待脈衝開始,預設是一秒 (unsigned long)

回傳

脈衝的時間長度 (單位是微秒) 。如果在超時前沒有讀到脈衝的話則是回傳 0 (unsigned long)。

範例

int pin = 7;
unsigned long duration;

void setup()
{
  pinMode(pin, INPUT);
}

void loop()
{
  duration = pulseIn(pin, HIGH);
}

語法參考主頁面

本頁由熱血青年 LBU 譯自英文版。

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.