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);
}