#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?