在 Arduino 中新增延遲


為了在 Arduino 中新增時間延遲,你可以使用delay()函式。它的引數是毫秒為單位的延遲值。以下給出了一個執行示例 −

示例

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
}
void loop() {
   // put your main code here, to run repeatedly:
   Serial.print("Hello!");
   delay(2000);
}

上述程式碼每 2 秒列印一次 "Hello!"。你可能已經猜到了,使用 delay 函式可以實現的最小延遲是 1 毫秒。如果想要更短的延遲怎麼辦?Arduino 具有一個 delayMicroseconds() 函式,它以微秒為單位接收延遲值作為引數。

示例

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
}
void loop() {
   // put your main code here, to run repeatedly:
   Serial.print("Hello!");
   delayMicroseconds(2000);
}

上述程式碼每 2 毫秒(2000 微秒)列印一次 "Hello!"。

更新於: 2021 年 3 月 23 日

1 千次以上瀏覽

開啟你的 職業生涯

完成課程以獲得認證

入門
廣告
© . All rights reserved.