NeoPixel Ring rotator

Zápisník experimentátora

Hierarchy: WS2812

In this article, we will explain how we can program the c++ class Rotator, which will make it easier for us to control the NeoPixel Ring. This class has several parameters that define the color and saturation on the ring. The Rotator class itself creates the gradual darkening of colors into the shape of a tail and also provides color animation around the circumference of the ring.


The class Rotator includes four variables and has two functions. These variables define a pointer to the Adafruit_NeoPixel object, the position of the starting point, the hue, and the saturation. The variables are set in the constructor. Usually, the initialization of variables in the class constructor is written after the colon, as you can see in the example, but you can also use the assignment operator. This way it is shorter and usually clearer. The constructor also has implicit parameters written in the definition, which allows us to omit little-changing parameters when creating an object. Therefore, usually the input parameters to a function are written by sorting them from the most important to the less important.

The second function is called Step and takes care of setting the animation to NeoPixel Ring. Sets a gradually darkening color from the starting point. Finally, it shifts the starting point in the variable position.

Using the class Rotator is easy. You can see this in the last lines of the program. We have created an object rt and regularly call its function Step.

#include <Adafruit_NeoPixel.h>

// data pin
#define PIN 6
// led count
#define CNT 24
// max Hue
#define MAXHUE 65535

Adafruit_NeoPixel ring(CNT, PIN, NEO_GRB + NEO_KHZ800);

class Rotator
    Adafruit_NeoPixel *strip;
    int position;
    uint16_t hue;
    uint8_t saturation;

    Rotator(Adafruit_NeoPixel *npx, int pos = 0, uint16_t h = 0, uint8_t sat = 255);
    void Step();

Rotator::Rotator(Adafruit_NeoPixel *npx, int pos, uint16_t h, uint8_t sat)
  : strip(npx), position(pos), hue(h), saturation(sat)

void Rotator::Step()
  // hue - 0-65535
  // saturation - 0-255
  // value - 0-255
  for (int i = 0; i < CNT; i++)
      (i + position) % CNT,
      strip->ColorHSV(hue, saturation, strip->gamma8(i * (255 / CNT))));
  position %= CNT;

//Rotator rt(&ring, 0, 0, 255); // red
//Rotator rt(&ring, 0, 0, 200); // pink
Rotator rt(&ring, 0, 40000L, 200); // lightblue

void setup() {

void loop() {

Source code

The source code is located on the GitHub server.