Skip to content

Addressable LED / NeoPixel

Advanced Topic Skip if you’re new, explore when you’re ready.

You can use custom color events to drive addressable RGB LEDs like NeoPixels. The examples below show the simplest form of animating the single, first LED on the strand. If you want an event to drive more than or different than the first LED in a strand, you’ll need to build on what’s shown here for your own custom use case.

Examples are shown in both the Adafruit NeoPixel Library, as well as FastLED. Between the two, FastLED is usually more performant and is more powerful. The Adafruit NeoPixel library may be easier to understand and customize if you’re starting out. You’ll need to install either library in the Arduino IDE before using it.

You’ll use the effector lifecycle and custom event callback events to add the logic for driving addressable LEDs.

You’ll need to customize this code depending on your addressable LED hardware, for things like number of lights on the strand, and chipset/color order such as “NEO_GRB + NEO_KHZ800” for the NeoPixel library or WS2812B and GRB for the FastLED library. If you’re not sure, it’s best to take a step back and get your addressable LEDs working with basic example non-Bottango code from the library of your choice, to find the right settings for your hardware.

Include the library in the callbacks file.

BottangoArduinoCallbacks.cpp
#include "BottangoArduinoCallbacks.h"
#include "src/AbstractEffector.h"
#include "src/Outgoing.h"
#include "src/BottangoCore.h"
#include <Adafruit_NeoPixel.h> // included the library
BottangoArduinoCallbacks.cpp
namespace Callbacks
{
// add to the top of the Callbacks namespace
// Using the constructor Adafruit_NeoPixel pixels(NUMPIXELS, PIN, color order + chipset);
Adafruit_NeoPixel pixel(1, 6, NEO_GRB + NEO_KHZ800);
// and a state tracker to only initialize once
bool strandInitialized = false;
// rest of callbacks file etc...

Clear the strand when the color event is set live and initialize it (one-time only). In this example, the identifier was set to “myRGB” but you’ll use whatever you set.

BottangoArduinoCallbacks.cpp
void onEffectorRegistered(AbstractEffector *effector)
{
// Initialize a single NeoPixel on pin 6 for a color custom event with identifier "myRGB";
char effectorIdentifier[9];
effector->getIdentifier(effectorIdentifier, 9);
if (strcmp(effectorIdentifier, "myRGB") == 0)
{
if (strandInitialized == false)
{
pixel.begin();
strandInitialized = true;
}
pixel.clear();
pixel.show();
}
}

Set the color on the NeoPixel when it is animated to a new color.

BottangoArduinoCallbacks.cpp
void onColorCustomEventColorChanged(AbstractEffector *effector, byte newRed, byte newGreen, byte newBlue)
{
// Set the first pixel on the strand to the given red, green, and blue values
char effectorIdentifier[9];
effector->getIdentifier(effectorIdentifier, 9);
if (strcmp(effectorIdentifier, "myRGB") == 0)
{
pixel.setPixelColor(0, pixel.Color(newRed, newGreen, newBlue));
pixel.show();
}
}

Clear the strand when the color event is set to not live

BottangoArduinoCallbacks.cpp
void onEffectorDeregistered(AbstractEffector *effector)
{
// Clear the strand when the effector is deregistered
char effectorIdentifier[9];
effector->getIdentifier(effectorIdentifier, 9);
if (strcmp(effectorIdentifier, "myRGB") == 0)
{
pixel.clear();
pixel.show();
}
}