I'm new to Arduino programming and haven't programmed in C++ in the past. I'm trying to create a custom library to cycle a rainbow on my NeoPixel LED strip. I want to pass a reference or pointer or whatever into the library class so I can keep the code in my project file as simple as possible.

Here is my current project code:

#include <YaNeoPixelCycle.h>
#include <Adafruit_NeoPixel.h>

#define CONTROL_PIN 3
#define LED_COUNT 8
Adafruit_NeoPixel neoPixel(LED_COUNT, CONTROL_PIN, NEO_RGBW + NEO_KHZ800);
YaNeoPixelCycle cycle = YaNeoPixelCycle(neoPixel,255,100,200);

void setup() {

void loop() {

And here is the library I tried to create:

    Youkai's Artificery NeoPixel cycle library.
    A super basic RGB LED cycle.

#ifndef YaNeoPixelCycle_h
#define YaNeoPixelCycle_h

#include "Arduino.h"
#include <Adafruit_NeoPixel.h>

class YaNeoPixelCycle{
        YaNeoPixelCycle(Adafruit_NeoPixel& neoPixel, int saturation, int brightness, int hueStep);
        void tryStep();
        Adafruit_NeoPixel& _neoPixel;
        unsigned int _ledHue = 0;
        int _saturation;
        int _brightness;
        int _hueStep;

    Youkai's Artificery timer library.
    Some basic non-blocking timer functions.

#include "Arduino.h"
#include "YaNeoPixelCycle.h"

YaNeoPixelCycle::YaNeoPixelCycle(Adafruit_NeoPixel& neoPixel, int saturation, int brightness, int hueStep){
    _neoPixel = neoPixel;
    _saturation = saturation;
    _brightness = brightness;
    _hueStep = hueStep;

void YaNeoPixelCycle::tryStep(){
    _ledHue += _hueStep;
    _neoPixel.fill(_neoPixel.ColorHSV(_ledHue, _saturation, _brightness));

The first step would be getting this to function at all. Uploading this to my Sparkfun ProMicro bricks it and I have to reset it to upload code again.

After that it would be awesome if it was possible to declare the NeoPixel in the project file but not actually initialize it until the library file. I'd love to pass CONTROL_PIN and LED_COUNT as variables to the library with YaNeoPixelCycle(..., int controlPin, int ledCount) so I don't have to set up a bunch of duplicate variables in my main file if I'm running more than one NeoPixel strip.

This is my own custom library so I'm not too worried about it being obscure. If it's possible/easier I would be fine just moving all of the NeoPixel code entirely into the library and never creating it at all in the main file. Just passing the control pin and LED count as parameters and having to "know" that those refer to a NeoPixel would be fine too.

  • If you want to initialize it in the library, why do you need an instance of NeoPixel, that is external to the library? You could simply put the complete declaration of it into the library. I currently don't see your problem
    – chrisl
    Mar 5, 2020 at 9:15
  • I wasn't thinking originally of declaring it in the library. But if that works it would be fine too.
    – William
    Mar 5, 2020 at 23:24
  • Yes, it works. You can simply have it only there
    – chrisl
    Mar 6, 2020 at 7:54

1 Answer 1


You call _neoPixel.begin() and _neoPixel.show() in the constructor of your class. This constructor runs before the Arduino is initialized. This is why begin() functions are used. Add a begin() function to your class and call cycle.begin() in setup().

void YaNeoPixelCycle::begin() {

Of course remove the _neoPixel.begin() and _neoPixel.show() from the constructor.

  • 1
    This helped. I also needed to apparently use a "member initializer list" instead of doing the assignment in the constructor. I guess my NeoPixel object wasn't getting set properly the way I was doing it above.
    – William
    Mar 6, 2020 at 17:19
  • Thanks for this! I also had my members being set in the ctor which didn't work. I was going crazy trying to figure out why it didn't work. Switched to member initializer list and it's working fine now.
    – Kevin Aung
    Apr 30, 2021 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.