-1

I am trying to create a custom Arduino library that uses another library. I want to be able to initialize a library within my library this is what I got so far. It works well but I need to be able to make multiple instances and change the variable that are required when initializing the library.

The .ino

#include "audiolux.h"
#include <FastLED.h>

Audiolux al1;

void setup() {
}
void loop() {
  al1.runs();
}

The .ccp file:

#include "Arduino.h"
#include "audiolux.h"
#include <FastLED.h>

Audiolux::Audiolux()
{
  FastLED.addLeds<NEOPIXEL, 6>(leds, 100);
}

void Audiolux::runs(){
  leds[2] = CRGB::Red;
  FastLED.show();
}

The .h file

#ifndef audiolux_h
#define audiolux_h


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

class Audiolux
{
  public:
    Audiolux();
    void runs();
  private:
    CRGB leds[100];

};

#endif

For example, in the header file the CRGB is initialized as leds[100], how would I change it to be a variable I could address from the ino?

2
  • You could add a constructor with an argument that specifies the number of LEDs.
    – uint128_t
    Apr 12, 2016 at 6:26
  • How would that look? As I understand I cannot just replace the 100 with a variable. I'm a bit new to writing libraries so I would be grateful if I got guidance in the right direction, thanks! Apr 12, 2016 at 7:16

1 Answer 1

0

Ok, so you want to make a variable size array. You have two ways of doing this.

The first one is to statically allocate it (like you do) using a maximum number of supported leds, then simply use part of that array. This is quite heavy on RAM, since you will need to fully allocate the array.

Some code can be:

#ifndef audiolux_h
#define audiolux_h

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

#define MAX_LEDS 100

class Audiolux
{
    public:
        Audiolux(uint8_t nLeds);
        void runs();
    private:
        uint8_t _nLeds;
        CRGB leds[MAX_LEDS];
};

#endif

and

#include "audiolux.h"

Audiolux::Audiolux(uint8_t nLeds)
{
    if (nLeds <= MAX_LEDS)
    {
        _nLeds = nLeds;
        FastLED.addLeds<NEOPIXEL, 6>(leds, _nLeds);
    }
}

void Audiolux::runs(){
    // If you need to know the length of leds, look for _nLeds
    leds[2] = CRGB::Red;
    FastLED.show();
}

The other method is to dynamically allocate the array at runtime. Note that in this case the function can fail, since if you run out of memory you will not be able to allocate it. Moreover it's always a good habit to write the destructor, even if you will never call #ifndef audiolux_h

#define audiolux_h

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

class Audiolux
{
    public:
        Audiolux(uint8_t nLeds);
        ~Audiolux();
        void runs();
    private:
        uint8_t _nLeds;
        CRGB *leds = NULL;
};

#endif

and

#include "audiolux.h"

Audiolux::Audiolux(uint8_t nLeds)
{
    _nLeds = nLeds;
    leds = new CRGB[_nLeds];
    FastLED.addLeds<NEOPIXEL, 6>(leds, _nLeds);
}

Audiolux::~Audiolux()
{
    delete[] leds;
}

void Audiolux::runs(){
    leds[2] = CRGB::Red;
    FastLED.show();
}

My personal advice is to use the first solution whenever possible: dealing with dynamic allocation with very poor devices like microcontrollers can lead to a lot of troubles. At least according to my personal experience...

Your Answer

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

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