External Interrupt and millis() conflict

#include <SoftwareSerial.h>             //Use other pins for Serial
#include <RBDdimmer.h>                  //Dimmer Library

SoftwareSerial BTSerial(8, 9); // RX, TX
                                       
#define Light  6 //AC Load

dimmerLamp dimmer1(Light); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int data_pin = 7;
int interval1 = 2000;// Initialize Receiver value as 0.
unsigned long millis_1 = 0;
boolean result[41]; //holds the result
unsigned int temp; //in celcius
char c;

void setup()
{ 
  BTSerial.begin(9600);
  Serial.begin(9600);
 dimmer1.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
 dimmer1.setState(OFF);

}

void loop()
{
  
  if(millis() > millis_1 + interval1)
  {
    millis_1 = millis(); 
    pinMode(data_pin, OUTPUT);
    digitalWrite(data_pin,LOW);
    delay(18);
    digitalWrite(data_pin,HIGH);
    pinMode(data_pin, INPUT_PULLUP);

    //read 41 bits of signal
 for(int i=0;i<=40;i++)
    {
      result[i]=(pulseIn(data_pin, HIGH)>40);
    }    
    //Extract Temperature (from Byte 3)
    temp=0;
    for (int i=17;i<=24;i++)
    {
      temp=temp<<1;
      if (result[i]) temp|=1;
    }
    BTSerial.print("*T"+String(temp)+"*");
    Serial.print("*T"+String(temp)+"*");  
}
 

  }


}

I’m dimming a AC light using the RBDimmer.h library and I’m also using a DHT11 Sensor. The Dimmer library has an external interrupt attached to pin 2 and this is preventing my serial monitor from displaying the temperature sensor value as the interrupt happens faster than i could extract my data from the DHT11 data pin. It only displays T0 every 2 seconds. I have tried to disable interrupt during the 18ms required for data aquisition from the DHT11 and then reenabling it after serial print. Unfortunately, I’m still getting the same results.

millis_1 = millis(); 
    pinMode(data_pin, OUTPUT);
    digitalWrite(data_pin,LOW);
cli();
    delay(18);
    digitalWrite(data_pin,HIGH);
    pinMode(data_pin, INPUT_PULLUP);

I am only able to print the sensor values on the serial terminal when i comment this code

 dimmer1.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)

How do I prevent the external interrupt from screwing up my sensor read?

I'm not familiar with the library you are using - I presume it is linked to an external interrupt but we can't see the code it is using.

The pulseIn() function blocks and you have a blocking FOR loop that uses 40 of them.

...R

I see an immediate issue with your timing:

int interval1 = 2000;// Initialize Receiver value as 0.

should be:

unsigned long interval1 = 2000;// Initialize Receiver value as 0.

Also:

  if (millis() > millis_1 + interval1)

should be:

  if (millis() - millis_1 > interval1)

How do I prevent the external interrupt from screwing up my sensor read?

Disabling interrupts during the reading is what both you and all the dht libraries do.

Your placement of cli() before a delay is wrong, as delay will not function with interrupts disabled.

Using the dht in the same sketch as a zero cross interrupt for ac dimming may be a loosing proposition, but I would suggest using the Adafruit libraries for the dht.