FastLED fade_h cycles through colours have to stop at button push

Hello everyone,

Via an IR remote control I start an LED color fade by all colors.
I want to stop him at the next press on the same button.
So that the lamp can be stopped at any color.

In my opinion it is not possible with fastled library, but hope you can prove the opposite

Thanks for the effort,
Johan

#include <Wire.h>
#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 85
#define DATA_PIN 6


// Mode enumeration - if you want to add additional party or colour modes, add them here; you'll need to map some IR codes to them later; 
// and add the modes into the main switch loop
enum Mode { OFF,WHITE,FADE};
Mode mode = FADE;  
Mode lastMode = FADE;
  
// used to make basic mood lamp colour fading feature
int fade_h;
int fade_direction = 1;


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

void setup() { 
  // this line sets the LED strip type - refer fastLED documeantion for more details https://github.com/FastLED/FastLED
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  Serial.begin(115200);
  Wire.begin(9);                // Start I2C Bus as a Slave (Device Number 9)
  Wire.onReceive(receiveEvent); // register event
}

void receiveEvent(int bytes) {
  
  while(Wire.available())
   { 
      unsigned int received = Wire.read();
      Serial.print("Receiving IR hex: ");
      Serial.println(received,HEX);
      lastMode = mode;
      switch(received){
        case 0xBF:
          mode = OFF; break;
        case 0x37:
          mode = FADE; break;
        case 0xDF:
          mode = WHITE; break;  
      }
   }
}
 
void loop() { 
  
  // Maps mode names to code functions. 
  switch(mode){
    case OFF:reset();break;
    case WHITE: single_colour(0);break;
    case FADE: colour_fade();break;
  }
}

// Makes all the LEDs a single colour, see https://raw.githubusercontent.com/FastLED/FastLED/gh-pages/images/HSV-rainbow-with-desc.jpg for H values
void single_colour(int H){
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( H, 0, 255);
  }
  //avoid flickr which occurs when FastLED.show() is called - only call if the colour changes
  if(lastMode != mode){
    FastLED.show(); 
    lastMode = mode;
  }
  delay(50);
}

//This is the part of the sketch that contains the fade

void colour_fade(){
  //mood mood lamp that cycles through colours
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( fade_h, 255, 255);
  }
  if(fade_h >254){
    fade_direction = -1; //reverse once we get to 254
  }
  else if(fade_h < 0){
    fade_direction = 1;
  }
    
  fade_h += fade_direction;
  FastLED.show();
  delay(100);
}

// utility function to turn all the lights off.  
void reset(){
  for (int i=0;i<NUM_LEDS;i++){
    leds[i] = CHSV( 0, 0, 0);
  }
  FastLED.show();
   
}

It is possible with the FastLED library but not with the way the function is currently written.

You need to break the fade function in small bits and instead of delay(100) use the millis() technique and take advantage of not blocking the code to check the button press

This technique is called a state machine:-

See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0