This is why I don’t use for loops in my functions. I keep the hue variable outside, as a global variable, and increment it inside of the function as it’s being used. So something like:
// maxEffects is the maximum number of effects available, for example 5 effects
const uint8_t maxEffects = 5;
// state defines which effect is currently being displayed and is a number between 1 to maxEffects
volatile uint8_t state = 1;
// global hue variable
volatile uint8_t hue = 0;
void setup() {
// FastLED setup
// Other setup stuff
}
void loop() {
// This loop will run as fast as it can, always checking the button with every loop. If you need it slower, add an appropriate millis() check - never use a blocking delay()
// check button state
if (button has changed) {
// increment state
state++;
if (state > maxEffects) state = 1;
}
displayEffect(state);
}
static displayEffect(uint8_t effect) {
switch (effect) {
case 1: effectOne(); break;
case 2: effectTwo(); break;
case 3: effectThree(); break;
// etc., etc.
default: LEDS.showColors(CRGB::Black); break;
}
}
// effectOne - Rainbow
static void effectOne() {
FastLED.clear();
fill_rainbow( &(leds[0]), NUM_LEDS /led count/, hue /starting hue/);
hue++;
FastLED.show();
}
Now if you need to run the rainbow slower, stick a millis() check in the function (make sure you initialize patternLastRun and pause accordingly):
// effectOne - Rainbow
static void effectOne() {
if (millis() - patternLastRun > pause) {
FastLED.clear(); // why are you clearing it?
fill_rainbow( &(leds[0]), NUM_LEDS /led count/, hue /starting hue/);
hue++;
FastLED.show();
patternLastRun = millis();
}
}