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() {
cycle.tryStep();
}
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{
public:
YaNeoPixelCycle(Adafruit_NeoPixel& neoPixel, int saturation, int brightness, int hueStep);
void tryStep();
private:
Adafruit_NeoPixel& _neoPixel;
unsigned int _ledHue = 0;
int _saturation;
int _brightness;
int _hueStep;
};
#endif
/*
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;
_neoPixel.begin();
_neoPixel.show();
}
void YaNeoPixelCycle::tryStep(){
_ledHue += _hueStep;
_neoPixel.fill(_neoPixel.ColorHSV(_ledHue, _saturation, _brightness));
_neoPixel.show();
}
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.