Running LED patterns with IRremote

HI guys,
Im trying to make a simple on/off button for my LEDs and it works but i don’t know how to make this pattern run after releasing a button. I guess I have to somehow simulate continous pressing this button but i don’t know how to do it because my coding skills are far, far from perfect.

#include <FastLED.h>
#include <IRremote.h>

#define NUM_LEDS_PER_STRIP 17
#define ON_button 16236607
#define OFF_button 16203967

CRGB leds1[NUM_LEDS_PER_STRIP];
CRGB leds2[NUM_LEDS_PER_STRIP];
CRGB leds3[NUM_LEDS_PER_STRIP];
CRGB leds4[NUM_LEDS_PER_STRIP];
CRGB leds5[NUM_LEDS_PER_STRIP];
CRGB leds6[NUM_LEDS_PER_STRIP];

int IRpin = 2;
 IRrecv irrecv(IRpin);
decode_results results;
uint8_t paletteIndex = 0;
int patternCounter = 0;

DEFINE_GRADIENT_PALETTE (heatmap_gp) {
  0, 255, 0, 0,
  82, 0, 255, 0,
  168, 0, 0, 255,
  245, 255, 0, 0,
  255, 255, 0, 0,
};

CRGBPalette16 myPal = heatmap_gp ;




void rainbow() {
  fill_palette(leds6, NUM_LEDS_PER_STRIP, paletteIndex, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds5, NUM_LEDS_PER_STRIP, paletteIndex + 42, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds4, NUM_LEDS_PER_STRIP, paletteIndex + 84, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds3, NUM_LEDS_PER_STRIP, paletteIndex + 126, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds2, NUM_LEDS_PER_STRIP, paletteIndex + 168, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds1, NUM_LEDS_PER_STRIP, paletteIndex + 210, 255 / 40, myPal, 255, LINEARBLEND);

EVERY_N_MILLISECONDS(13){
  paletteIndex++;
}
  FastLED.show();
   }

void ledsoff() {
 FastLED.clear();
 FastLED.show();
}

  

void setup() {
  FastLED.addLeds<WS2812B, 6, GRB>(leds1, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 7, GRB>(leds2, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 8, GRB>(leds3, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 9, GRB>(leds4, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 10, GRB>(leds5, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 11, GRB>(leds6, NUM_LEDS_PER_STRIP);
  FastLED.setBrightness(115);


  irrecv.enableIRIn();
}

void loop() {
  static byte patternCounter;
  if (irrecv.decode(&results)) {
    Serial.println(results.value);
      irrecv.resume();
   
 switch(results.value){
  case OFF_button: patternCounter = 0; break;
  case ON_button: patternCounter = 1; break;
 }
  switch(patternCounter){
    case 0: ledsoff(); break;
    case 1: rainbow(); break;
    }}}

Test_4.ino (2.18 KB)

If you release the button, irrecv.decode(&results) will return false. So you can e.g. add an else.

  if (irrecv.decode(&results)) {
    ...
    ...
  }
  else
  {
    ...
    ...
  }

In the else yoiu can e.g. modify the patterCounter and possibly adjust if needed

The below should not be in the if as the result will be that it will only be executed when a button is pressed.

    switch (patternCounter) {
      case 0: ledsoff(); break;
      case 1: rainbow(); break;
    }

If it exactly does what you want is another question, but you can have a look at below

void loop() {
  static byte patternCounter;
  if (irrecv.decode(&results))
  {
    Serial.println(results.value);
    irrecv.resume();

    switch (results.value)
    {
      case OFF_button: patternCounter = 0; break;
      case ON_button: patternCounter = 1; break;
    }
  }
  else
  {
    patternCounter = 1;
  }

  switch (patternCounter)
  {
    case 0: ledsoff(); break;
    case 1: rainbow(); break;
  }
}

You don’t need the else clause. You should check to see if you have received a button press and then adjust patternCounter (odd name for your on/off variable, btw). And then, independent of receiving a button press, you should react to having the leds on/off.

#include <FastLED.h>
#include <IRremote.h>

#define NUM_LEDS_PER_STRIP 17
#define ON_button 16236607
#define OFF_button 16203967

CRGB leds1[NUM_LEDS_PER_STRIP];
CRGB leds2[NUM_LEDS_PER_STRIP];
CRGB leds3[NUM_LEDS_PER_STRIP];
CRGB leds4[NUM_LEDS_PER_STRIP];
CRGB leds5[NUM_LEDS_PER_STRIP];
CRGB leds6[NUM_LEDS_PER_STRIP];

int IRpin = 2;
IRrecv irrecv(IRpin);
decode_results results;
uint8_t paletteIndex = 0;

DEFINE_GRADIENT_PALETTE (heatmap_gp) {
  0, 255, 0, 0,
  82, 0, 255, 0,
  168, 0, 0, 255,
  245, 255, 0, 0,
  255, 255, 0, 0,
};

CRGBPalette16 myPal = heatmap_gp ;

void rainbow() {
  fill_palette(leds6, NUM_LEDS_PER_STRIP, paletteIndex, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds5, NUM_LEDS_PER_STRIP, paletteIndex + 42, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds4, NUM_LEDS_PER_STRIP, paletteIndex + 84, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds3, NUM_LEDS_PER_STRIP, paletteIndex + 126, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds2, NUM_LEDS_PER_STRIP, paletteIndex + 168, 255 / 40, myPal, 255, LINEARBLEND);
  fill_palette(leds1, NUM_LEDS_PER_STRIP, paletteIndex + 210, 255 / 40, myPal, 255, LINEARBLEND);

  EVERY_N_MILLISECONDS(13) {
    paletteIndex++;
  }
  FastLED.show();
}

void ledsoff() {
  FastLED.clear();
  FastLED.show();
}

void setup() {
  FastLED.addLeds<WS2812B, 6, GRB>(leds1, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 7, GRB>(leds2, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 8, GRB>(leds3, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 9, GRB>(leds4, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 10, GRB>(leds5, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<WS2812B, 11, GRB>(leds6, NUM_LEDS_PER_STRIP);
  FastLED.setBrightness(115);

  irrecv.enableIRIn();
}

void loop() {
  static bool isOn = false;

  if (irrecv.decode(&results)) {
    Serial.println(results.value);
    irrecv.resume();

    switch (results.value) {
      case OFF_button:
        ledsoff();
        isOn = false;
        break;

      case ON_button:
        // reset paletteIndex to 0 if you always want to start with the same pattern
        // or do not reset it and pick up whereever you left off from the last ON press
        // paletteIndex = 0;
        isOn = true;
        break;
    }
  }

  if ( isOn ) {
    rainbow();
  }
}