FastLED Library won't play nice with millis()

I've cut my code down as simple as I can to demonstrate my issue.
If I run the code as shown below, millis will output a predictable, increasing value. The loop will run about 12x per millisecond.
However, if I uncomment the second for loop in the function ledShiftLight(), the output of millis is just 38, no matter what.

I've tried many different kinds of code, but trying to run any code utilizing the FastLED library mor complex than the most basic blink type programs while using millis() always seems to break it, and it's all due to this issue with a bad millis() output. What's going on? the program that i'm hoping to use this library in is fairly complex and I MUST be able to use the millis function.

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 32

#define WARN_LEDS 6
#define SHIFT_LEDS 2 

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN 7
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

long unsigned timer = 0;
long unsigned rpmRate = 50;
int RPM = 4000;
bool rpmSwitch = 0;

void setup() { 
  Serial.begin(115200);
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);  // GRB ordering is typical

}

void loop() { 

  Serial.println(millis());
  ledShiftLight(RPM);

}


void ledShiftLight(int ledRPM){
  int blackout_val = map(ledRPM, 4000, 6500, NUM_LEDS/2, 0);


  //tach normal range
    for (int i = 0;i <= (NUM_LEDS/2) - WARN_LEDS; i++){
      leds[i] = CRGB(100, 38 , 0);
    }
//    for (int i = (NUM_LEDS/2) + WARN_LEDS + 1; i <= NUM_LEDS; i++){
//      leds[i] = CRGB(100, 38 , 0);
//    }
  

}

you overflow your array because of <= (indexes go from 0 to NUM_LEDS-1)

try with i < NUM_LEDS

(you could use the fill_solid function instead of the for loop)

1 Like

D’oh! Thank you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.