如何在開始前延遲2秒才實現-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 680|回复: 3

[未解决] 如何在開始前延遲2秒才實現

[复制链接]
发表于 2022-1-13 19:57 | 显示全部楼层 |阅读模式
請問要如何在開始閃燈前, 先延遲2秒才觸發閃燈?
PS. 只在開始時發生一次2秒延遲

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

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

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

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

  10. void setup() {
  11.   // set the digital pin as output:
  12.   pinMode(ledPin, OUTPUT);
  13. }

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

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

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

  23.     // if the LED is off turn it on and vice-versa:
  24.     if (ledState == LOW) {
  25.       ledState = HIGH;
  26.     } else {
  27.       ledState = LOW;
  28.     }

  29.     // set the LED with the ledState of the variable:
  30.     digitalWrite(ledPin, ledState);
  31.   }
  32. }
复制代码
发表于 2022-1-14 10:27 | 显示全部楼层
放在setup里一段,或者设置一个标志位,用个if判断是否是第一次执行
 楼主| 发表于 2022-1-14 21:17 | 显示全部楼层
mounsea1 发表于 2022-1-14 10:27
放在setup里一段,或者设置一个标志位,用个if判断是否是第一次执行

不好意思, 我是新手.
可否說明詳細一些?
发表于 2022-1-14 22:23 | 显示全部楼层
本帖最后由 shouzama 于 2022-1-15 09:22 编辑
andywwf 发表于 2022-1-14 21:17
不好意思, 我是新手.
可否說明詳細一些?

在 setup() 裏面放一條延遲 2 秒的指令
delay(2000);
就只有在開機時才會延遲
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 16:02 , Processed in 0.126763 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表