【问题标题】:How can I replace delay() by millis()?如何用毫秒()替换延迟()?
【发布时间】:2015-08-16 16:09:49
【问题描述】:

过了一会儿,我得到了我想要的最终结果,但我不能使用延迟,因为我需要不同的时间来处理不同的条带,所以我需要在这段代码中用 millis() 替换 delay()

#include <FastLED.h>
#define NUM_LEDS1 10
#define NUM_LEDS2 6
#define DATA_PIN1 6
#define DATA_PIN2 7
CRGB leds1[NUM_LEDS1];
CRGB leds2[NUM_LEDS2];

void setup() { 
  FastLED.addLeds<NEOPIXEL, DATA_PIN1>(leds1, NUM_LEDS1);
  FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds2, NUM_LEDS2);
}

int dot_delay1[ ] = { 100,200,300,400,500,600,700,800,900,1000 };
int dot_delay2[ ] = { 100,200,300,400,500,600 };

void loop() {
  for(int dot = 0; dot < NUM_LEDS1; dot++)
    for(int dot = 0; dot < NUM_LEDS2; dot++) 
    { 
      leds1[dot] = CRGB::Blue;
      leds2[dot] = CRGB::Blue;
      FastLED.show();
      leds1[dot] = CRGB::Black;
      leds2[dot] = CRGB::Black;
      delay( dot_delay1[ dot ] );
      // this is where I need to put the second delay,
      // but I can't put more then 1 delay.
      // I need to refactor my code with millis() function instead of delay()
    }
}

【问题讨论】:

  • 在标签中指定使用的语言。
  • @AlexanderPetrov 谢谢我在这里的第一篇文章 :)

标签: c arduino ide delay


【解决方案1】:

您可以使用以特殊频率或时间执行的非阻塞代码模式。下面,我给你一个简短的例子,你可以如何在不阻塞主循环的情况下替换 delay(1000)delay(5000)。此外,您可以检查 stackoverflow 是否有类似的帖子(例如 Pause without Delay() arduino)。

// 1 sec. frequency
unsigned long interval=1000;    // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

// 5 sec. frequency  
unsigned long interval1=5000;    // the time we need to wait
unsigned long previousMillis1=0; // millis() returns an unsigned long.

void setup() {
   //...
}

void loop() {

 // other CMD's...

 // replace delay(1000) 
 if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    // every first second
    // ... 
 }

 // other CMD's...

 // replace delay(5000) 
 if ((unsigned long)(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis();
    // every fifth second
    // ... 
 }

 // other CMD's...
}

【讨论】:

  • 就像你在我的第一个代码示例中看到的那样,我有一个 int dot_delay1[ ] = { 100,200,300,400,500,600,700,800,900,1000 }; 用于同一条带的 10 个延迟(并且条带有 10 个像素,所以 10 个延迟值)无论如何都知道如何为毫秒执行此操作()?如何像我的例子一样制作多个millis(100,200,300)
【解决方案2】:

我看到您正在使用 NeoPixels!碰巧 Adafruit 有一些关于使用 millis()micros() 进行计时而不是 delay() 的精彩教程。如果您有雄心壮志并希望将您的时间安排与主要功能完全分开(如果您有时间和资源,这是一项值得的工作),它们还包括关于中断的好材料。

第 1 部分(使用millis() 的基础知识):

https://learn.adafruit.com/multi-tasking-the-arduino-part-1/overview

第 2 部分(介绍中断):

https://learn.adafruit.com/multi-tasking-the-arduino-part-2/overview

第 3 部分(将所有内容放在一起):

https://learn.adafruit.com/multi-tasking-the-arduino-part-3/overview


编辑 1:

好的,我编辑了你的代码,添加了一个循环来检查两个延迟时间是否使用millis() 完成。为了便于阅读,我试图坚持您一直在使用的变量命名约定。我清理了您的代码语法还有一些其他问题。确保有效地注释您的代码,以便您自己和他人都可以理解它!

希望这能如你所愿!

/*The goal of the sketch is to run multiple strip where each dot could be 
controlled on/off one after the others but with different times on/off between each
pixel on each strip. All be independent and separate and automated and structured
already like FastLED library recommendation.*/

// Library include
#include <FastLED.h>

#define NUM_LEDS1 10
#define NUM_LEDS2 6

// Define pin (each strip be connected on)
#define DATA_PIN1 5
#define DATA_PIN2 6

// Declare some strip name and attribute (number of led per strip)
CRGB leds1[NUM_LEDS1];
CRGB leds2[NUM_LEDS2];

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN1>(leds1, NUM_LEDS1);
  FastLED.addLeds<NEOPIXEL, DATA_PIN2>(leds2, NUM_LEDS2);
}

int dot_delay1[] = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};
int dot_delay2[] = {100, 200, 300, 400, 500, 600};

void loop() {
  for (int dot1 = 0; dot1 < NUM_LEDS1; dot1++) {
    for (int dot2 = 0; dot2 < NUM_LEDS2; dot2++) {
      // LED turns blue
      leds1[dot1] = CRGB::Blue;
      leds2[dot2] = CRGB::Blue;
      // Show LED status
      FastLED.show();
      // LED turns black
      leds1[dot1] = CRGB::Black;
      leds2[dot2] = CRGB::Black;
      // Create timing variables
      unsigned long previousMillis = millis();
      unsigned long currentMillis = millis();
      // Create boolean variables to monitor if the delays have triggered yet
      bool delayFlag1 = false, delayFlag2 = false;
      // Loop continuously
      while (1) {
        currentMillis = millis();
        // If the first delay time has passed, delayFlag1 is true
        if ((unsigned long)(millis() - previousMillis) >= dot_delay1[dot1]) {
          delayFlag1 = true;
        }
        // If the second delay time has passed, delayFlag2 is true
        if ((unsigned long)(millis() - previousMillis) >= dot_delay2[dot2]) {
          delayFlag2 = true;
        }
        // If both delay times have passed (both delay flags are true), exit while loop
        if ((delayFlag1 && delayFlag2) == true) {
          break;
        }
      }
    }
  }
}

这个新代码应该可以工作,我已经对其进行了测试,它可以在我的机器上按预期工作。

作为旁注,您最初的问题是“如何将 delay() 替换为 millis()?”。我自己和之前的答案作者 user3704293 都已回答了这个问题。将来,将您的问题分开以获得更高质量的答案并更好地为那些正在寻找相同问题答案的人提供服务可能会有所帮助。一旦你的问题得到回答,我希望这个问题是现在,你应该接受最适合你的答案。

如果一切顺利,请告诉我!


编辑 2:

要让 LED 彼此独立变化,请将前面代码中的 loop() 函数替换为这个函数。

void loop() {
  // Create timing variable
  unsigned long previousMillis = millis();
  // Loop through all LEDs
  for (int dot = 0; dot < NUM_LEDS1; dot++) {
    // If the first delay time has passed, change first LED strip
    if ((unsigned long)(millis() - previousMillis) >= dot_delay1[dot1]) {
      leds1[dot - 1] = CRGB::Black;
      leds1[dot] = CRGB::Blue;
    }
    // If the second delay time has passed, change second LED strip
    if ((unsigned long)(millis() - previousMillis) >= dot_delay2[dot2]) {
      leds2[dot - 1] = CRGB::Black;
      leds2[dot] = CRGB::Blue;
    }
    // Show LEDs
    FastLED.show();
  }
  // Turn last LED Black
  leds1[9] = CRGB::Black;
}

【讨论】:

  • 是的,现在已经有 2 周的时间了,我检查了所有本教程...但是因为我不是本地编码器而感到迷茫,我尝试在此处修复我的代码。
  • @FrançoisEricKing 我将编辑我的帖子并尝试找出解决您的承诺的方法,请给我一点时间。我没有亲自使用过 NeoPixels,所以我会尽力而为,但没有承诺!如果您明确说明“第二次延迟”的用途也会有所帮助。
  • 第二个延迟是.second strip led的一系列延迟:)
  • 感谢您的澄清,让我知道我发布的代码是否适合您。
  • 我测试它哇谢谢,但不工作。每个条带的第一个像素变为蓝色,仅此而已。这是您代码的粘贴箱,我在文件中的注释中添加了更多详细信息……也许这有助于理解。 pastebin.com/d9SW62du
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-03
  • 2020-04-05
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-04
相关资源
最近更新 更多