Addressable LED / NeoPixel
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.
Callback Events
Section titled “Callback Events”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.
Includes
Section titled “Includes”Include the library in the callbacks file.
#include "BottangoArduinoCallbacks.h"#include "src/AbstractEffector.h"#include "src/Outgoing.h"#include "src/BottangoCore.h"#include <Adafruit_NeoPixel.h> // included the library#include "BottangoArduinoCallbacks.h"#include "src/AbstractEffector.h"#include "src/Outgoing.h"#include "src/BottangoCore.h"#include <FastLED.h> // include the libraryDefine the strand
Section titled “Define the strand”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...namespace Callbacks{ // add to the top of the Callbacks namespace // creating a data array the size of the strand (just one pixel in our case) CRGB leds[1]; // and a state tracker to only initialize once bool strandInitialized = false;
// rest of callbacks file etc...Enable the strand
Section titled “Enable the strand”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.
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(); }}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) { // <chipset, pin, color order>(data array, number of LEDs) FastLED.addLeds<WS2812B, 6, GRB>(leds, 1); strandInitialized = true; }
FastLED.clear(); FastLED.show(); }}Set Color
Section titled “Set Color”Set the color on the NeoPixel when it is animated to a new color.
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(); }}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) { leds[0] = CRGB(newRed, newGreen, newBlue); FastLED.show(); }}Disable the strand
Section titled “Disable the strand”Clear the strand when the color event is set to not live
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(); }}void onEffectorDeregistered(AbstractEffector *effector){ // Clear the strand when the effector is deregistered
char effectorIdentifier[9]; effector->getIdentifier(effectorIdentifier, 9);
if (strcmp(effectorIdentifier, "myRGB") == 0) { leds[0] = CRGB::Black; FastLED.show(); }}