FastLED and calling functions within a class

aminabudahab:
You're right,I was able to achieve the desired result when I put all the code in the main sketch but using classes will also make the code tidier and easier to read.

Maybe my question should be: why my greenAlternate() won't operate properly when I put it inside the class?

P.s. Trust me, in 5 days I read all sorts of things :slight_smile:

this was done wiht NeoPixel library, but you should be able to do it similarly with FastLed:

#include <Adafruit_NeoPixel.h>

constexpr size_t PIXEL_COUNT = 10;
constexpr uint8_t PIXEL_PIN = 7;

struct LedColor {
  LedColor(){color = 0x00000000;}
  LedColor(uint32_t c) : color(c){}
  LedColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t white = 0) : b(blue), g(green), r(red), w(white){}
  union {
    uint32_t color;
    struct {
      uint8_t b;
      uint8_t g;
      uint8_t r;
      uint8_t w;
    };
  };
  bool operator==(const LedColor& that) const {
    return this->color == that.color;
  }
};

class NeopixelFader : public  Adafruit_NeoPixel{
  public:
    NeopixelFader(uint16_t num_pixels, uint8_t pixel_pin, uint8_t desc) : Adafruit_NeoPixel(num_pixels, pixel_pin, desc){}
    void update(void) {
      uint32_t currentMillis = millis();
      if (currentMillis - lastUpdateMillis > fadeInterval) {
        if (target == current) {
          return;
        }
        if (target.r > current.r) {
          current.r++;
        } else if (target.r < current.r) {
          current.r--;
        }
        // green
        if (target.g > current.g) {
          current.g++;
        } else if (target.g < current.g) {
          current.g--;
        }
        // blue
        if (target.b > current.b) {
          current.b++;
        } else if (target.b < current.b) {
          current.b--;
        }
        //white
        if (target.w > current.w) {
          current.w++;
        } else if (target.w < current.w) {
          current.w--;
        }
//        Serial.print(F("Curret Color:\t"));
//        Serial.println(current.color, HEX);
        for (size_t i = 0; i < numLEDs; i++) {
          setPixelColor(i, current.color);
        }
        lastUpdateMillis = currentMillis;
        show();
      }
    }
    void fadeStripToColor(LedColor c){
      target = c;
    }
    void fadeStripToColor(uint8_t r, uint8_t g, uint8_t b, uint8_t w){
      target.r = r;
      target.g = g;
      target.b = b;
      target.w = w;
    }
    LedColor getTargetColor(void) {
      return target;
    }
    LedColor getCurrentColor(void) {
      return current;
    }
  private:
    LedColor target;
    LedColor current;
    uint32_t lastUpdateMillis;
    uint32_t fadeInterval = 3;
};

NeopixelFader strip(PIXEL_COUNT, PIXEL_PIN, NEO_RGB + NEO_KHZ800);

LedColor red    (0x00FF0000);  // 0xWWRRGGBB
LedColor green  (0x0000FF00);
LedColor blue   (0x000000FF);
LedColor orange (0x00FF9B00);
LedColor someColor(197, 255, 200);  // alternate construction (r, g, b, w)

void setup() {
  Serial.begin(9600);
  pinMode(3, INPUT_PULLUP);
  Serial.println(someColor.r);
  Serial.println(someColor.g);
  Serial.println(someColor.b);
  Serial.println(someColor.w);
  strip.begin();
  strip.show();
  strip.fadeStripToColor(blue);
}

void loop() {
  strip.update();
  auto checkButton = [](uint8_t pin, uint16_t debounceMillis){
    bool lastState = true;
    static uint32_t lastMillis = 0;
    int currentState = digitalRead(pin);
    if (currentState != lastState and millis() - lastMillis > debounceMillis) {
      lastState = currentState;
      lastMillis = millis();
      Serial.println(F("Pressed"));
      return true;
    }
    return false;
  };
  if(checkButton(3, 100)) {
    if (strip.getTargetColor() == red) {
      strip.fadeStripToColor(green);
    } else if (strip.getTargetColor() == green) {
      strip.fadeStripToColor(blue);
    } else if (strip.getTargetColor() == blue) {
      strip.fadeStripToColor(red);
    }
  }
}