-2

我正在尝试使用毫秒让我的 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();

  }

}
4

1 回答 1

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();
  }

}
于 2020-04-30T23:03:54.897 回答