【问题标题】:blinking WS2812 fastLED with milis arduino用millis arduino闪烁WS2812 fastLED
【发布时间】:2020-04-30 21:42:31
【问题描述】:

我正在尝试使用毫秒让我的 LED 每 2 秒闪烁一次。 延迟是不可能的,因为我正在运行其他传感器。

到目前为止,我得到了这个,但它似乎不起作用

#include "FastLED.h"
#define NUM_LEDS 12 // number of LEDS in neopixel ring
#define DATA_PIN 10 // for neopixel ring
CRGB leds[NUM_LEDS];

long period = 2000;        
long currentMillis = 0;
long startMillis = 0;

void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {

 currentMillis = millis();

  if (currentMillis - startMillis >= period) {
    startMillis = currentMillis;
    leds[7]=CRGB(255,0,0);
    FastLED.show();

  }

}

【问题讨论】:

  • 发布的代码无法编译。您缺少设置和循环功能。仅通过查看正确完成的一个 sn-p 是不可能知道为什么您的代码不起作用的。如果您需要帮助发布一个实际编译并展示问题的示例。这些变量声明真的位于 if 语句之前吗?如果是这样,那么我不希望这会起作用,因为每次调用函数时 startMillis 都会不断设置回 0。如果这不是问题,那么希望它至少说明了您需要发布代码的原因。
  • 我认为设置和循环是隐含的
  • 在我看来,您的时间变量应该是无符号的。我可以发布一个答案,解释为什么如果这样可以解决您的问题。
  • 你现在能解释一下你所说的“不工作”是什么意思吗?灯不亮?延迟时间不对?不上传?只是说“它不起作用”是你在这里可以说的最没用的事情。如果您希望有人能够帮助您,那么您必须告诉他们出了什么问题。
  • @jstarr 那太好了,谢谢!

标签: arduino led fastled


【解决方案1】:

这会让你更接近一点吗?

#include "FastLED.h"
#define NUM_LEDS 12 // number of LEDS in neopixel ring
#define DATA_PIN 10 // for neopixel ring
CRGB leds[NUM_LEDS];

unsigned long period = 2000;        
unsigned long currentMillis = 0;
unsigned long startMillis = 0;
boolean ledOn = false;

void setup() {
FastLED.addLeds<WS2812, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {

 currentMillis = millis();

  if (currentMillis - startMillis >= period) {
    startMillis = currentMillis;
    ledOn = !ledOn;
    if(ledOn){
      leds[7]=CRGB(255,0,0);
    }
    else {
      leds[7]=CRGB(0,0,0);
    }
    FastLED.show();
  }

}

【讨论】:

  • 它不起作用。只有快速点亮并关闭
  • 这听起来很强大。代码应该闪烁它。您遗漏的更多细节。看看您希望我们“暗示”的那些缺失的细节最终是如何拖慢一切的?
  • 不不,对不起。 LED 灯亮起并停止。不是它自己的arduino。
  • 所以led亮了,两秒钟后熄灭,再也没有亮起?你确定你的代码复制对了吗?确定你没有把支架弄错地方吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多