我看到您正在使用 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;
}