0

I am trying to blink my led strip without using a delay()

So far i have somethin like this:

#include <FastLED.h>

#define NUM_LEDS 60
#define BRIGHTNESS  32
#define LED_TYPE    WS2811
#define DATA_PIN 6

CRGB leds[NUM_LEDS];
unsigned long actTime = 0;
unsigned long remTime = 0;
const unsigned long period = 1000;


void setup() { 
      FastLED.setMaxPowerInVoltsAndMilliamps(5,1000);
      FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
      FastLED.setBrightness(BRIGHTNESS);
}

void loop() {
  actTime = millis();
  if (actTime - remTime >= period){
    remTime = actTime;
    fill_solid(leds, NUM_LEDS, CRGB::Red);
    FastLED.show();
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    FastLED.show();
  }
}

But it is not working like i want it to. For example if i change fill_solid(leds, NUM_LEDS, CRGB::Black); to CRGB::Green i will only see green color and hard to see red blinking. I want to make it look like this for example: green for 1s -> red for 1s -> green etc.

How my loop should look like?

Kentaro Okuda
  • 1,557
  • 2
  • 12
  • 16
b0bgetout
  • 25
  • 2
  • 7

2 Answers2

1

Your timing code is correct, but it does the wrong thing every second--it sets the color to red, shows it, and then immediately sets the color to black and shows it. Add an additional state variable to track the current color:

bool is_red = false;

void loop() {
  actTime = millis();
  if (actTime - remTime >= period){
    remTime = actTime;
    if(!is_red) {
        is_red = true;
        fill_solid(leds, NUM_LEDS, CRGB::Red);
        FastLED.show();
    } else {
        is_red = false;
        fill_solid(leds, NUM_LEDS, CRGB::Black);
        FastLED.show();
    }
  }
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

I'm not a programmer so this is probably not proper but should work and is much shorter, not necessarily better for the compiler, definitely not as clever. There's a better way to do even/odd with an expression that checks for a "1" in the first bit. This works the same and is easier to understand than the &0x00 expression if one doesn't think in bits.

unsigned long tymNow = millis();
int num2nds = (tymNow/1000);
if(num2nds % 2 == 0)fill_solid(leds, NUM_LEDS, CRGB::Red);
if(num2nds % 2 > 0)fill_solid(leds, NUM_LEDS, CRGB::Black);
FastLED.show();