Author Topic: Custom library for Arduino not working with NeoPixel  (Read 1230 times)

0 Members and 1 Guest are viewing this topic.

Offline YoukaiTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: us
Custom library for Arduino not working with NeoPixel
« on: March 06, 2020, 12:27:25 am »
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: [Select]
#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 file
Code: [Select]
/*
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;
};

#endif

Library .cpp file
Code: [Select]
/*
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();
}
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: Custom library for Arduino not working with NeoPixel
« Reply #1 on: March 06, 2020, 12:56:54 am »
Code: [Select]
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 here https://en.cppreference.com/w/cpp/language/initializer_list
 
The following users thanked this post: Youkai

Offline YoukaiTopic starter

  • Regular Contributor
  • *
  • Posts: 229
  • Country: us
Re: Custom library for Arduino not working with NeoPixel
« Reply #2 on: March 06, 2020, 02:05:23 am »
Look up "member initializer list"

Aha! Thank you. I updated my code to look like this and it works:
Code: [Select]
/*
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();
}
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 5153
  • Country: nz
Re: Custom library for Arduino not working with NeoPixel
« Reply #3 on: March 06, 2020, 02:10:37 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf