Custom library for Arduino not working with NeoPixel - Page 1
Forum section: electronics / general

author: Youkai
author_rank: Regular Contributor
timestamp: March 06, 2020, 12:27:25 am
content: I'm working on a project that is going to have a couple NeoPixel strips attached to it. I want to have one of the strips run a rainbow cycle. First I wrote a simple Arduino program to make what I wanted work (attached; rename to ".ino").Then I tried to move as much of the logic to my library as possible. It seems that I ether screwed something up or you can't access a NeoPixel from within a library. I tried a bunch of stuff including using a pointer (which I don't really understand) and couldn't get it to work. Please help!I've had all kinds of problems with my program compiling but not being valid and bricking my ProMicro. Fortunately resetting the pro micro lets me upload a new code. This code will upload without bricking the ProMicro but it doesn't do anything. The LED is off.New INO file:Code:#include <Adafruit_NeoPixel.h>#include <YaNeoPixelCycle.h>YaNeoPixelCycle cycle(8, 3, 255, 100, 20, 200, false, 10);void setup() {Serial.begin(9600);cycle.init();}void loop() {cycle.tryStep();}Library .h fileCode:/*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{public:YaNeoPixelCycle(int neoPixelNumLed, int neoPixelControlPin, int saturation, int brightness, int hueStep, int waveStep, boolean useWave, int stepDelay);void tryStep();void init();private:Adafruit_NeoPixel _neoPixel;unsigned int _ledHue;int _saturation;int _brightness;int _hueStep;int _waveStep;boolean _useWave;};#endifLibrary .cpp fileCode:/*Youkai's Artificery NeoPixel cycle library.A super basic RGB LED cycle.*/#include "Arduino.h"#include "Adafruit_NeoPixel.h"#include "YaNeoPixelCycle.h"YaNeoPixelCycle::YaNeoPixelCycle(int neoPixelNumLed, int neoPixelControlPin, int saturation, int brightness, int hueStep, int waveStep, boolean useWave, int stepDelay){Adafruit_NeoPixel _neoPixel(neoPixelNumLed, neoPixelControlPin, NEO_RGBW + NEO_KHZ800);_ledHue = 0;_saturation = saturation;_brightness = brightness;_hueStep = hueStep;_waveStep = waveStep;_useWave = useWave;}void YaNeoPixelCycle::init(){_neoPixel.begin();_neoPixel.show();}void YaNeoPixelCycle::tryStep(){Serial.println(_ledHue);_ledHue += _hueStep;_neoPixel.fill(_neoPixel.ColorHSV(_ledHue, _saturation, _brightness));_neoPixel.show();}

author: brucehoult
author_rank: Super Contributor
timestamp: March 06, 2020, 12:56:54 am
content: Code:YaNeoPixelCycle::YaNeoPixelCycle(int neoPixelNumLed, int neoPixelControlPin, int saturation, int brightness, int hueStep, int waveStep, boolean useWave, int stepDelay){Adafruit_NeoPixel _neoPixel(neoPixelNumLed, neoPixelControlPin, NEO_RGBW + NEO_KHZ800);// ...}This is creating a new Adafruit_NeoPixel object, initializing it with the different parameters, and then throwing it away.You need to be calling the constructor for the Adafruit_NeoPixel object you have defined in your class. Look up "member initializer list" in your C++ manual. For example herehttps://en.cppreference.com/w/cpp/language/initializer_list

author: Youkai
author_rank: Regular Contributor
timestamp: March 06, 2020, 02:05:23 am
content: Quote from: brucehoult on March 06, 2020, 12:56:54 amAha! Thank you. I updated my code to look like this and it works:Code:/*Youkai's Artificery NeoPixel cycle library.A super basic RGB LED cycle.*/#include "Arduino.h"#include "Adafruit_NeoPixel.h"#include "YaNeoPixelCycle.h"YaNeoPixelCycle::YaNeoPixelCycle(int neoPixelNumLed, int neoPixelControlPin, int saturation, int brightness, int hueStep, int waveStep, boolean useWave, int stepDelay):_neoPixel(neoPixelNumLed, neoPixelControlPin, NEO_RGBW + NEO_KHZ800),_ledHue(0),_saturation(saturation),_brightness(brightness),_hueStep(hueStep),_waveStep(waveStep),_useWave(useWave){}void YaNeoPixelCycle::init(){_neoPixel.begin();_neoPixel.show();}void YaNeoPixelCycle::tryStep(){Serial.println(_ledHue);_ledHue += _hueStep;_neoPixel.fill(_neoPixel.ColorHSV(_ledHue, _saturation, _brightness));_neoPixel.show();}

author: brucehoult
author_rank: Super Contributor
timestamp: March 06, 2020, 02:10:37 am
content: Yup, that's better style.It doesn't make any difference for the things that aren't classes. Unless they're declared const. But for members that are themselves classes with non-default constructors it's the only way.