I'm new to these addressable LED things – I just received a 149-LED WS2812B strip today, and I'm experimenting with various approaches. Adafruit's own tutorial says the FastLED library is faster than their Adafruit_NeoPixel library, so I wanted to test their performance.
I'm running this code on a Nano clone:
// Adafruit
#include <Adafruit_NeoPixel.h>
// FastLED
#include <FastLED.h>
// Common
#define NUM_LEDS 149
#define DATA_PIN 6
// Adafruit initialization
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, DATA_PIN, NEO_GRB + NEO_KHZ800);
const uint32_t adaBlack = strip.Color(0, 0, 0);
const uint32_t adaWhite = strip.Color(255, 255, 255);
// FastLED initialization
CRGB leds[NUM_LEDS];
void setup() {
// Common
delay(2000);
// Adafruit setup
strip.begin();
// FastLED setup
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
// Common
Serial.begin(57600);
}
void loop() {
unsigned int adafruitMillis = adafruitLoop();
unsigned int fastLedMillis = fastLedLoop();
// Yes, this is ugly -- but it works every time
Serial.print(adafruitMillis);
Serial.print(",");
Serial.println(fastLedMillis);
}
unsigned int adafruitLoop()
{
long unsigned startMillis = millis();
for (int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++) {
strip.setPixelColor(whiteLed, adaWhite);
strip.show();
strip.setPixelColor(whiteLed, adaBlack);
}
return (unsigned int) millis() - startMillis;
}
unsigned int fastLedLoop()
{
long unsigned startMillis = millis();
for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++) {
leds[whiteLed] = CRGB::White;
FastLED.show();
leds[whiteLed] = CRGB::Black;
}
return (unsigned int) millis() - startMillis;
}
The results are totally disastrous for FastLED (shown in red):
Am I doing something wrong, or is FastLED winning on features, rather than on speed?
For the record, I did check whether the performance hit comes from setting the pixel values or from sending the data – FastLED is faster at setting the pixel values than Adafruit's library, but that doesn't matter because setting two pixel values takes 3 ms or less for the entire strip, regardless of the library. Sending the data to the strip takes most of the time, and that's where FastLED lags behind big time (900 ms compared to Adafruit's 200 ms).