IR remote does not work as desired

**On a led ring, the level of a smoke sensor is displayed. When the level goes up, leds will be added and when the level goes down, leds will go out. This worked perfectly until I added my IR. **

The IR works, and my values are still read, but when I want to turn my led ring on with the remote, the level (on the led ring) stays at the value that was read at the exact moment when I pressed the on button. The ledring freezes, so to speak. Turning it on and off is no problem.

How can I best solve this?

int alarm = 10;
const int smokeA0 = A5;
#include <FastLED.h>
#define LED_PIN     12
#define NUM_LEDS    24
CRGB leds[NUM_LEDS];

#include <IRremote.h>

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean Power = false;

const int range1 = 50;
const int range2 = 60;
const int range3 = 70;
const int range4 = 80;
const int range5 = 90;
const int range6 = 100;
const int range7 = 110;
const int range8 = 120;
const int range9 = 130;
const int range10 = 140;
const int range11 = 150;
const int range12 = 160;
const int range13 = 180;
const int range14 = 200;
const int range15 = 220;
const int range16 = 240;
const int range17 = 260;
const int range18 = 280;
const int range19 = 300;
const int range20 = 320;
const int range21 = 340;
const int range22 = 360;
const int range23 = 380;
const int range24 = 400;

const int maxLEDs = 24;

void setup()
{
  pinMode(alarm, OUTPUT);
  pinMode(smokeA0, INPUT);
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  irrecv.enableIRIn(); // Start the receiver
}

int calculateLEDAmount(int analogSensor)
{
  if (analogSensor < range1)
  {
    return 1;
  }
  else if (analogSensor < range2)
  {
    return 2;
  }
  else if (analogSensor < range3)
  {
    return 3;
  }
  else if (analogSensor < range4)
  {
    return 4;
  }
  else if (analogSensor < range5)
  {
    return 5;
  }
  else if (analogSensor < range6)
  {
    return 6;
  }
  else if (analogSensor < range7)
  {
    return 7;
  }
  else if (analogSensor < range8)
  {
    return 8;
  }
  else if (analogSensor < range8)
  {
    return 8;
  }
  else if (analogSensor < range9)
  {
    return 9;
  }
  else if (analogSensor < range10)
  {
    return 10;
  }
  else if (analogSensor < range11)
  {
    return 11;
  }
  else if (analogSensor < range12)
  {
    return 12;
  }
  else if (analogSensor < range13)
  {
    return 13;
  }
  else if (analogSensor < range14)
  {
    return 14;
  }
  else if (analogSensor < range15)
  {
    return 15;
  }
  else if (analogSensor < range16)
  {
    return 16;
  }
  else if (analogSensor < range17)
  {
    return 17;
  }
  else if (analogSensor < range18)
  {
    return 18;
  }
  else if (analogSensor < range19)
  {
    return 19;
  }
  else if (analogSensor < range20)
  {
    return 20;
  }
  else if (analogSensor < range21)
  {
    return 21;
  }
  else if (analogSensor < range22)
  {
    return 22;
  }
  else if (analogSensor < range23)
  {
    return 23;
  }
  else
  {
    return 24;
  }
}
void loop()
{
  int analogSensor = analogRead(smokeA0);

  int ledAmount = calculateLEDAmount( analogSensor );

  Serial.print("Pin A0: ");
  Serial.println(analogSensor);

  if (irrecv.decode(&results)) {
    //   Serial.println(results.value, HEX);
    switch (results.value)
    {
      case 0x20DF10EF:
        // power on/OFF
        Power = !Power;
        if (Power)
        {

          if (analogSensor < 161)
          {
            for (int i = 1; i <= ledAmount; i++)
            {
              leds[i] = CRGB ( 0, 255, 0);
              FastLED.show();
              delay(100);
              noTone(alarm);
            }
          }

          if ((analogSensor >= 161) && (analogSensor < 321))
          {
            for (int i = 1; i <= ledAmount; i++)
            {
              leds[i] = CRGB ( 255, 127, 0);
              FastLED.show();
              delay(100);
              noTone(alarm);
            }
          }
          if (analogSensor >= 400)
          {
            for (int i = 1; i <= ledAmount; i++)
            {
              leds[i] = CRGB ( 255, 0, 0);
              FastLED.show();
              delay(100);
              tone(alarm, 1000, 200);
            }
          }


          //24x HIGH

          for (int i = ledAmount; i < maxLEDs; i++)
          {

            leds[i] = CRGB ( 0, 0, 0);
            FastLED.show();
            delay(100);
          }

        }

        if (!Power)
        {
          for (int i = 1; i <= ledAmount; i++) {
            leds[i] = CRGB ( 0, 0, 0);
            FastLED.show();
          }
        }
        delay(1500);
        break;
    }
    irrecv.resume(); // Receive the next value
  }


}

Hi @jannesvn
What happens to your code is as follows:
There is a "case" that tests the amount received from the IR.
case 0x20DF10EF:
When turning on this case, it is answered and then sets the LEDs according to the "if"s in the sequence.
But in the end you erase the value of the IR,
" irrecv.resume(); // Receive the next value",
and so it no longer enters the LED light selection "if"s.
Did I make myself clear?
I modified your code, test it and tell us the result.

RV mineirin

int alarm = 10;
const int smokeA0 = A5;
#include <FastLED.h>
#define LED_PIN     12
#define NUM_LEDS    24
CRGB leds[NUM_LEDS];

#include <IRremote.h>

int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean Power = false;

const int range1 = 50;
const int range2 = 60;
const int range3 = 70;
const int range4 = 80;
const int range5 = 90;
const int range6 = 100;
const int range7 = 110;
const int range8 = 120;
const int range9 = 130;
const int range10 = 140;
const int range11 = 150;
const int range12 = 160;
const int range13 = 180;
const int range14 = 200;
const int range15 = 220;
const int range16 = 240;
const int range17 = 260;
const int range18 = 280;
const int range19 = 300;
const int range20 = 320;
const int range21 = 340;
const int range22 = 360;
const int range23 = 380;
const int range24 = 400;

const int maxLEDs = 24;

//-------------------------------------------------------------------------------
void setup()
{
  pinMode(alarm, OUTPUT);
  pinMode(smokeA0, INPUT);
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  irrecv.enableIRIn(); // Start the receiver
}
//-------------------------------------------------------------------------------
int calculateLEDAmount(int analogSensor)
{
  if (analogSensor < range1)
  {
    return 1;
  }
  else if (analogSensor < range2)
  {
    return 2;
  }
  else if (analogSensor < range3)
  {
    return 3;
  }
  else if (analogSensor < range4)
  {
    return 4;
  }
  else if (analogSensor < range5)
  {
    return 5;
  }
  else if (analogSensor < range6)
  {
    return 6;
  }
  else if (analogSensor < range7)
  {
    return 7;
  }
  else if (analogSensor < range8)
  {
    return 8;
  }
  else if (analogSensor < range8)
  {
    return 8;
  }
  else if (analogSensor < range9)
  {
    return 9;
  }
  else if (analogSensor < range10)
  {
    return 10;
  }
  else if (analogSensor < range11)
  {
    return 11;
  }
  else if (analogSensor < range12)
  {
    return 12;
  }
  else if (analogSensor < range13)
  {
    return 13;
  }
  else if (analogSensor < range14)
  {
    return 14;
  }
  else if (analogSensor < range15)
  {
    return 15;
  }
  else if (analogSensor < range16)
  {
    return 16;
  }
  else if (analogSensor < range17)
  {
    return 17;
  }
  else if (analogSensor < range18)
  {
    return 18;
  }
  else if (analogSensor < range19)
  {
    return 19;
  }
  else if (analogSensor < range20)
  {
    return 20;
  }
  else if (analogSensor < range21)
  {
    return 21;
  }
  else if (analogSensor < range22)
  {
    return 22;
  }
  else if (analogSensor < range23)
  {
    return 23;
  }
  else
  {
    return 24;
  }
}
//-------------------------------------------------------------------------------
void loop()
{
  int analogSensor = analogRead(smokeA0);
  int ledAmount = calculateLEDAmount( analogSensor );
  Serial.print("Pin A0: ");
  Serial.println(analogSensor);
  if (irrecv.decode(&results)) 
  {
    //   Serial.println(results.value, HEX);
    //    switch (results.value)
    //   {
    if ( results.value == 0x20DF10EF)   // power on/OFF
    {
      Power = !Power;
      irrecv.resume(); // Receive the next value
    }
  }
  if (Power)
  {
    if (analogSensor < 161)
    {
      for (int i = 1; i <= ledAmount; i++)
      {
        leds[i] = CRGB ( 0, 255, 0);
        FastLED.show();
        delay(100);
        noTone(alarm);
      }
    }
    if ((analogSensor >= 161) && (analogSensor < 321))
    {
      for (int i = 1; i <= ledAmount; i++)
      {
        leds[i] = CRGB ( 255, 127, 0);
        FastLED.show();
        delay(100);
        noTone(alarm);
      }
    }
    if (analogSensor >= 400)
    {
      for (int i = 1; i <= ledAmount; i++)
      {
        leds[i] = CRGB ( 255, 0, 0);
        FastLED.show();
        delay(100);
        tone(alarm, 1000, 200);
      }
    }
    //24x HIGH
    for (int i = ledAmount; i < maxLEDs; i++)
    {
      leds[i] = CRGB ( 0, 0, 0);
      FastLED.show();
      delay(100);
    }
  }
  if (!Power)
  {
    for (int i = 1; i <= ledAmount; i++) {
      leds[i] = CRGB ( 0, 0, 0);
      FastLED.show();
    }
  }
  delay(1500);
  ///        break;
}

Hi @ruilviana
The led ring turns on and shows the level properly, but it does not turn off again after this.

Hi
Is this the routine you wrote to shut down?

RV mineirin

  if (!Power)
  {
    for (int i = 1; i <= ledAmount; i++) {
      leds[i] = CRGB ( 0, 0, 0);
      FastLED.show();
    }
  }

Hi @ruilviana
Nevermind.
I thought the led ring wouldn't turn off, but this was because I didn't have enough patience.
I reduced the delay.

Thanks for the clear answer!

Hi @
Please, if it was resolved, mark as resolved in the topic that resolved the issue.
That way other users can see the topic as a solution to their problem.
Sincerely thank you.

RV mineirin

Hi @ruilviana
There is still a small problem.
I can turn the led ring on and off, but only when the sensor has not yet transmitted a value.
If I turn the led ring on and the sensor passes a value, after this I cannot turn it off.

Hi
What is connected to this sensor pin?

RV mineirin

Hi @
If "ledAmount" is "1" like "NUM_LEDS " = 24 and as the delay inside this routine below is 100 ms, it will take 100 * 24 mSec or 2.4 Sec,
And during this time your code cannot read the remote.

     for (int i = ledAmount; i < maxLEDs; i++)
     {
       leds[i] = CRGB( 0, 0, 0);
       FastLED.show();
       delay(100);
     }

RV mineirin

Okey tanks.
I changed the delay to 10 ms, and now its working.
Thanks again.

Looks like a simple formula:

int calculateLEDAmount(int analogSensor)
{
  if (analogSensor < 50)  // 0 to 49 -> 1
    return 1;

  if (analogSensor < 160)
    return (analogSensor  / 10) - 3;  // 50 to 159 -> 2-12

  if (analogSensor < 400)
   return (analogSensor / 20) + 4;  // 160 to 399 -> 13 - 23

  return 24;
}