Attempt to use FastLED Library in custom Library

Hello there,

I have been trying to create a custom library for a project I am doing at work. The code will have to control one or multiple WS2811 LED Strips and multiple RGB-PWM LED Strips. I am stuck at the point of trying to create the class for the WS2811 Strips. Here's my code:

OctagonPanel.cpp

#include "..\FastLED\FastLED.h"
#include <Arduino.h>
#include "OctagonPanel.h"
#include <SoftwareSerial.h>

uint8_t _ledTypes[3][2]{
    {255, 255}, //vertikal über Vorgängerled
    {180, 180}, //diagonal über Vorgängerled
    {255, 0}    //horizontal neben Vorgängerled

};


OctagonPanel::OctagonPanel(const uint8_t dataPin, unsigned int numLeds, uint8_t ledDefinerArray[]){
    _dataPin = dataPin;
    const unsigned int _numLeds = numLeds;
    uint8_t _ledDefinerArray[_numLeds];
    for(uint8_t i = 0; i < _numLeds; i++){
        _ledDefinerArray[i] = ledDefinerArray[i];
    }
    switch(_dataPin){
        case 2:
            #define DATA_PIN 2
            break;
        case 3:
            #define DATA_PIN 3
            break;
        case 4:
            #define DATA_PIN 4
            break;
        case 5:
            #define DATA_PIN 5
            break;
        case 6:
            #define DATA_PIN 6
            break;
        case 7:
            #define DATA_PIN 7
            break;
    }
    
    for(unsigned int j = 0; j <= _numLeds; j++){
        _levelMaxTotal = _levelMaxTotal + _ledTypes[_ledDefinerArray[j]][1];
    }
}

void OctagonPanel::begin(){
    
    FastLED.addLeds<NEOPIXEL, DATA_PIN>(_leds, _numLeds);
    
}

void OctagonPanel::show(){
    FastLED.show();
}

void OctagonPanel::testFunc(){
    for(int i = 0; i <= 10; i++){
        Serial.println(_leds[i]);
        _leds[i] = CHSV(240, 255,255);
        Serial.print("i: ");
        Serial.println(i);
        Serial.println(_leds[i]);
    }
}

OctagonPanel.h

#ifndef OctagonPanel_h
#define OctagonPanel_h

#include <Arduino.h>

class OctagonPanel{
    public:
        OctagonPanel(const uint8_t dataPin, unsigned int numLeds, uint8_t ledDefinerArray[]);
        int getLevelActual();
        void setLevel(uint8_t level);
        void setHue(uint8_t hue);
        void update();
        void begin();
        void show();
        void testFunc();
    private:
        int _x, _level, _hue, _dataPin;
        int _ledsFull, _lastLedFillValue;
        int _levelActual, _levelTarget, _numLeds, _levelMax, _levelMaxTotal;
        CRGB _leds[16];
        int _ledDefinerArray[16];
};


#endif

main.ino

#include "src\FastLED\FastLED.h"
#include "src\LightingControl\OctagonPanel.h"

const unsigned int NUM_LEDS = 16;
const uint8_t DELAY = 10, DATA_PIN = 6, HUE = 240;

uint8_t ledDefinerArray[NUM_LEDS]{0,0,0,0,1,1,1,1,2,2,2,2,0,0,0,0};
OctagonPanel panel1(DATA_PIN, NUM_LEDS, ledDefinerArray);

void setup(){
    Serial.begin(9600);
    panel1.begin();
    Serial.println("Good Morning");
    panel1.testFunc();
    panel1.show();   
}

void loop(){

}

When I compile this code no errors occur and it uploads just fine but the Strip I attached does nothing at all. I have checked the hardware setup by uploading a sketch for the Strip that doesn't use a custom library (so everything just in one sketch) and it works without problems.
I have found a thread that had a very similar problem but his solution didn't work for me: He put the the constructor for the existing Library inside a separate function and not in the constructor of his own library. I did exactly that but nothing changed.

The Serial.print lines I added for debugging give me the expected results so the code runs as intended except for the FastLED library.

My guess is, that I am missing out on some crucial part about how to use a library wthin another but I can't point it down.

Here's a minimal example for the FastLED library i'm trying to use:

#include <FastLED.h>

// How many leds in your strip?
#define NUM_LEDS 1

// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires - data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN 3
#define CLOCK_PIN 13

// Define the array of leds
CRGB leds[NUM_LEDS];

void setup() { 

    FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed

}

void loop() { 
  // Turn the LED on, then pause
  leds[0] = CRGB::Red;
  FastLED.show();
  delay(500);
  // Now turn the LED off, then pause
  leds[0] = CRGB::Black;
  FastLED.show();
  delay(500);
}

Any help and hints are greatly appreciated!

P.S.: There are seemingly unused variables and functions which are used in other parts of my code I didn't put on here as the provide no relevant information to the problem at hand

Hi Smagel,

Which function won't work when called? Is it "testFunc()"?

Thanks,

Zeb

ZebH:
Hi Smagel,

Which function won't work when called? Is it "testFunc()"?

Thanks,

Zeb

Hi,

Thank you for your answer.

"In testFunc()" I am setting the first ten LEDs of the led strip to be turned on by changing the values in the _leds array/struct and "in show()" the library actually turns on the leds based on the values in _leds.

I don't really know which part of my program does not work. It could be a wrong initialization of the FastLED library, it could be an error in the code itself that the values of _leds won't be changed or something entirely else. I am guessing that "testFunc()" works fine and its some kind of mistake I made while trying to set up the class/library.

The first thing I would suggest you do is put a Serial.println in each function in the .cpp file. That will show you if they are even running.

Zeb

Also I find this link here very helpful for the basic structure of a library.
https://www.arduino.cc/en/Hacking/libraryTutorial

Let us know what the result is of the test using Serial.println in the functions!

Zeb

Ok, so outputting all variable values with Serial.println gave me some insight:

This piece of code right here:

switch(_dataPin){
        case 2:
            #define DATA_PIN 2
            break;
        case 3:
            #define DATA_PIN 3
            break;
        case 4:
            #define DATA_PIN 4
            break;
        case 5:
            #define DATA_PIN 5
            break;
        case 6:
            #define DATA_PIN 6
            break;
        case 7:
            #define DATA_PIN 7
            break;
    }

does not work at all.
_dataPin is 6 but DATA_PIN gets set to 7. So I plugged the LED-Strip into Digital Pin 7. Now it lights up. I feel stupid. I suppose this happens because of the way #define works.

Thank you for having helped me!

No worries! Glad it's working!

Zeb