i am trying to write a class for an arduino projekt, i am using the information in http://paulmurraycbr.github.io/ArduinoTheOOWay.html as a guide.
I want to set up a lighstrip for further use and constantly keep getting the errormessage: error: use of 'this' in a constant expression.
My code looks like this:
#include <FastLED.h>
#define LED_STRIP_PIN 2
#define LED_STRIP_NUM_LEDS 10
//const unsigned char LED_STRIP_PIN = 2;
//const int LED_STRIP_NUM_LEDS = 10;
CRGB leds[LED_STRIP_NUM_LEDS];
class LedStrip {
unsigned char pin;
public:
LedStrip(unsigned char attachTo) :
pin(attachTo)
{
};
void setup() {
FastLED.addLeds<NEOPIXEL, pin>(leds, 10);
};
};
//LedStrip ledstrip(LED_STRIP_PIN, LED_STRIP_NUM_LEDS);
LedStrip ledstrip(LED_STRIP_PIN);
void setup() {
}
void loop() {
}
I have tried reading up on what might cause this error, but frankly i do not understand any of it. As far as i have understood it seems like i cannot use a const there (which i am not, i think) since it might be modified during code execution.
the complete error looks like thissketch_feb03b.ino: In member function 'void LedStrip::setup()':
sketch_feb03b:20:33: error: use of 'this' in a constant expression
FastLED.addLeds<NEOPIXEL, pin>(leds, 10);
^~~
Your issue is that pin
is not a compile-time constant, and all template arguments must be compile-time constants.
There might be other options, but the (probably) easiest one is passing pin
as template argument itself:
template<int pin> // make sure type of pin is correct, I don't know what addLeds expect
class LedStrip {
public:
LedStrip() //not really needed now
{
};
void setup() {
FastLED.addLeds<NEOPIXEL, pin>(leds, 10);
};
};
Usage:
//if LED_STRIP_PIN is a compile-time constant, i.e. a macro or a const(expr) value
LedStrip<LED_STRIP_PIN> ledstrip;
//if LED_STRIP_PIN is obtained at runtime, you cannot it use it at all.
LedStrip<7> ledstrip;