Doubt about how to generate an interrupt in nodeMCU based on an analog variable.

Hello, I am starting in arduino IDE and I have a question.

I'm programming a nodeMCU with the IDE and I need to generate an interrupt to turn on a led when any of the three analog sensors exceeds the threshold.

I don't know how it could be done, the only thing that has occurred to me is to set a digital pin of the nodeMCU to high when the threshold is exceeded and use this to generate the interruption but I do not know if this is the correct / adequate solution.
I leave you here the code that I am testing.

PD: I am using a mux because the micro only has an analog input and I need to read three sensors.

#define led1 16
#define led2 14
#define bot1 15

const int muxSIG = A0;
const int muxS0 = 2;
const int muxS1 = 0;
const int muxS2 = 4;
const int muxS3 = 5;

int SetMuxChannel(byte channel)
{
  digitalWrite(muxS0, bitRead(channel, 0));
  digitalWrite(muxS1, bitRead(channel, 1));
  digitalWrite(muxS2, bitRead(channel, 2)); 
  digitalWrite(muxS3, bitRead(channel, 3));
  attachInterrupt(digitalPinToInterrupt(bot1),apagar_led1,RISING);
}

ICACHE_RAM_ATTR void apagar_led1()
{
   digitalWrite(led1,HIGH);
   digitalWrite(bot1,LOW);
}

void setup()
{
  Serial.begin(115200);
  pinMode(muxS0, OUTPUT);
  pinMode(muxS1, OUTPUT);
  pinMode(muxS2, OUTPUT);
  pinMode(muxS3, OUTPUT);
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(bot1,OUTPUT);
}

void loop()
{
  for (byte i = 0; i < 3; i++)
  {
    SetMuxChannel(i);
    byte muxValue = analogRead(muxSIG);

    Serial.print(muxValue);
    Serial.print("\t");
    if((i==0&&muxValue>50)){
      digitalWrite(bot1,HIGH);
    }
    else if((i==1&&muxValue>50)){
      digitalWrite(led2,HIGH);
    }
    else if((i==2&&muxValue>50)){
      digitalWrite(led2,LOW);
      digitalWrite(led1,LOW);
    }
  }
  Serial.println();
  delay(10);
}

I need to generate an interrupt to turn on a led when any of the three analog sensors exceeds the threshold.

Are you sure that you need to use an interrupt ? What will the ISR do ?

UKHeliBob:
Are you sure that you need to use an interrupt ? What will the ISR do ?

My idea is that the interrupt activates a flag that in the loop turns off the LEDs, making the program stop what it is doing at that moment.

ivan1919:
My idea is that the interrupt activates a flag that in the loop turns off the LEDs, making the program stop what it is doing at that moment.

You don't need an interrupt for that. Just check the analogs at the beginning of the loop() function and turn off the LEDs if the threshold is exceeded.

ToddL1962:
You don't need an interrupt for that. Just check the analogs at the beginning of the loop() function and turn off the LEDs if the threshold is exceeded.

Okay, I'll try without interruption, but I don't know if it can cause problems (shutdown delay) if the final loop () has much more code. Thanks

ivan1919:
Okay, I'll try without interruption, but I don't know if it can cause problems (shutdown delay) if the final loop () has much more code. Thanks

I really don't see the difference. If an interrupt sets a flag then the amount of delay all depends on when your loop checks the flag. You can make a function that you can call to check the thresholds at critical times during your processing just like you would check the flag if you had an interrupt.

An alternative design might be to split your processing into phases. Each time through loop you can check the thresholds and execute the current phase of processing. When you are done with all phases you just start again.