pulseIn()

[Advanced I/O]

描述

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGHpulseIn() waits for the pin to go HIGH, starts timing, then waits for the pin to go LOW and stops timing. Returns the length of the pulse in microseconds. Gives up and returns 0 if no pulse starts within a specified time out.

The timing of this function has been determined empirically and will probably show errors in longer pulses. Works on pulses from 10 microseconds to 3 minutes in length.

語法

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

參數

pin: the number of the pin on which you want to read the pulse. (int)
value: type of pulse to read: either HIGH 或 LOW. (int)
timeout (optional): the number of microseconds to wait for the pulse to start; default is one second (unsigned long)

回傳

the length of the pulse (in microseconds) or 0 if no pulse started before the timeout (unsigned long)

範例程式碼

int pin = 7;
unsigned long duration;
 
void setup()
{
  pinMode(pin, INPUT);
}
 
void loop()
{
  duration = pulseIn(pin, HIGH);
}

語法參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール