0

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);

                                 ^~~
xBarns
  • 291
  • 1
  • 3
  • 12
  • 1
    Template arguments (in this case `pin`) must be `constexpr`. In your case, the value of `pin` is not known until runtime, which is not allowed. – Cory Kramer Feb 03 '20 at 14:13

2 Answers2

1

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;
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
0
#include <FastLED.h>
const int NUM_LEDS01 = 49; //Missing one LED from string of 50 LEDS
const int DATA_PIN01 = 2;  //White wire to GND, Red wire to 5V, Black Wire is data. 
CRGB leds01[NUM_LEDS01]; //First strng of LEDS
 

const int NUM_LEDS02 = 50;
const int DATA_PIN02 = 3;  //White wire to GND, Red wire to 5V, Black Wire is data. 
CRGB leds02[NUM_LEDS02];  //Second String of LEDS


//#include "Lstring.h"

template<int dataPin>
class Lstring{
  private:
  int nleds = 0; //initialise to some number
  CRGB *ledS;
  
  public:
  Lstring(){}
  Lstring(CRGB *lds, int nleds){
    this->nleds = nleds;
   ledS = lds;
  }


  void ledOn(int ledNum){
    ledS[ledNum] = CRGB::Blue;
    FastLED.show();
  }

    void ledOff(int ledNum){
    ledS[ledNum] = CRGB::Black;
    FastLED.show();
  }


void initialise(){
  FastLED.addLeds<WS2811,dataPin>(ledS,nleds);
}


};

//---------------------------------

Lstring<DATA_PIN01> lstring01(leds01, NUM_LEDS01);
Lstring<DATA_PIN02> lstring02(leds02, NUM_LEDS02);


void setup() {
lstring01.initialise();
lstring02.initialise();
}

void loop() {

lstring01.ledOn(8);
delay(200);
lstring01.ledOff(8);
delay(200);

lstring02.ledOn(8);
delay(200);
lstring02.ledOff(8);
delay(200);

}
Malzon
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Semo Dec 20 '22 at 08:04
  • It would be nice to give a brief explanation of how this works / how it solves the problem, and how it's different than existing answers. – starball Dec 21 '22 at 07:38