Arduino - delay() 函式



delay() 函式的工作原理很簡單。它接受一個單一的整數(或數字)引數。這個數字表示時間(以毫秒為單位)。當程式遇到此函式時,它應該等待直到移動到下一行程式碼。然而,問題是,delay() 函式不是使程式等待的好方法,因為它被稱為“阻塞”函式。

delay() 函式語法

delay (ms) ;

其中,ms 是暫停的時間(以毫秒為單位)(無符號長整數)。

示例

/* Flashing LED
   * ------------
   * Turns on and off a light emitting diode(LED) connected to a digital
   * pin, in intervals of 2 seconds. *
*/

int ledPin = 13; // LED connected to digital pin 13

void setup() {
   pinMode(ledPin, OUTPUT); // sets the digital pin as output
}

void loop() {
   digitalWrite(ledPin, HIGH); // sets the LED on
   delay(1000); // waits for a second
   digitalWrite(ledPin, LOW); // sets the LED off
   delay(1000); // waits for a second
}
arduino_time.htm
廣告