瑞龍關廟鳳梨資訊網

瑞龍關廟鳳梨資訊網
瑞龍關廟鳳梨資訊網

2022年3月13日 星期日

delay的替代方案=使用 millis() 進行計時,Blink Without Delay的中英對照

使用IDE 中的 BlinkWithoutDelay 示例

Blink Without Delay的中英對照

/*
Blink without Delay

Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.

The circuit:
- Use the onboard LED.
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
is set to the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your
Arduino model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products

created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
modified 11 Nov 2013
by Scott Fitzgerald
modified 9 Jan 2017
by Arturo Guadalupi

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW; // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated

// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)

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

void loop() {
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}

// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}

/*
無延遲閃爍

打開和關閉連接到數字引腳的發光二極管 (LED),
不使用 delay() 函數。這意味著其他代碼可以在
同時不會被 LED 代碼打斷。

電路:
- 使用板載 LED。
- 注意:大多數 Arduino 都有一個可以控制的板載 LED。在 UNO,MEGA
和零它連接到數字引腳 13,在引腳 6 上的 MKR1000。 LED_BUILTIN
設置為正確的 LED 引腳,與使用哪個板無關。
如果您想知道板載 LED 連接到哪個引腳
Arduino 模型,請在以下位置查看您的電路板的技術規格:
https://www.arduino.cc/en/Main/Products

創建於 2005 年
大衛·A·梅利斯
2010 年 2 月 8 日修改
保羅·斯托弗雷根
2013 年 11 月 11 日修改
斯科特·菲茨杰拉德
2017 年 1 月 9 日修改
阿圖羅·瓜達盧皮

此示例代碼位於公共領域。

http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// 常量不會改變。此處用於設置引腳號:
const int ledPin = LED_BUILTIN;// LED 管腳編號

// 變量會改變:
int ledState = LOW;// ledState 用於設置 LED

// 一般來說,你應該對持有時間的變量使用“unsigned long”
// 該值將很快變得太大而無法存儲 int
unsigned long previousMillis = 0; // 將存儲上次更新 LED 的時間

// 常量不會改變:
const long interval = 1000; // interval at which to blink (milliseconds)
常量長間隔 = 1000; // 閃爍的時間間隔(毫秒)

void setup() {
無效設置(){
 // set the digital pin as output:
// 將數字引腳設置為輸出:
 pinMode(ledPin, OUTPUT);
}
void loop() {
無效循環(){
// here is where you'd put code that needs to be running all the time.
// 這裡是你放置需要一直運行的代碼的地方。

// 檢查是否到了 LED 閃爍的時間;也就是說,如果差異
// 當前時間和上次閃爍 LED 之間的時間大於
// 您希望 LED 閃爍的時間間隔。
 unsigned long currentMillis = millis();
無符號長 currentMillis = millis();
 if (currentMillis - previousMillis >= interval) {
if (currentMillis - previousMillis >= 間隔) {
// 保存上次閃爍 LED 的時間
 previousMillis = currentMillis;
以前的Millis = currentMillis;

// 如果 LED 熄滅,則將其打開,反之亦然:

  if (ledState == LOW) {
如果(ledState == LOW){
ledState = HIGH;
ledState = 高;
} else {
} 別的 {
ledState = LOW;
ledState = 低;
}

// 使用變量的 ledState 設置 LED:
digitalWrite(ledPin, ledState);
數字寫入(ledPin,ledState);
}

const int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}