I am controlling the activation and brightness of a AC lamp automatically with the use of a PIR sensor, and a TRIAC with zero detector circuit. I'm using the RBDimmer.h library to implement this. Basically, the interrupt pin configured in the RBDimmer library is pin 2. Thus, whenever the arduino detects the zerocrossing on pin 2, it sets the firing angle for the triac based on the value it received from the outVal. The library has a function called dimmer.setPower() to help me set the brightness. I also have a dht11 sensor. Right now, I'm facing a problem between my AC lamp and the dht sensor. During data acquisition from the dht11 sensor, it is required for interrupts to be disabled. I have used the noInterrputs(); and interrupts(); codes during data acquisition as such:
void temperatureRead()
{
pinMode(data_pin, OUTPUT);
digitalWrite(data_pin, LOW);
delay(18);
digitalWrite(data_pin, HIGH);
pinMode(data_pin, INPUT_PULLUP);
noInterrupts();
//read 41 bits of signal
for (int i = 0; i <= 40; i++)
{
result[i] = (pulseIn(data_pin, HIGH) > 40);
}
interrupts();
//Extract Temperature (from Byte 3)
temp = 0;
for (int i = 17; i <= 24; i++)
{
temp = temp << 1;
if (result[i]) temp |= 1;
}
BTSerial.print(String(temp) + "#");
Serial.print(String(temp) + "*");
}
However, this is causing flickering of my AC Lamp and the temperature reading is wrong for every 3-4 values. Whenever there is a wrong value in the serial terminal, a flickering is observed on my AC lamp.
I believe this has to do with interrput handling. Is there any way my AC Lamp's brightness be fixed once the firing angle is set so that it does not flicker until the next change in firing angle is received?
Following is my full code:
#include <RBDdimmer.h> //Dimmer Library
#include <AltSoftSerial.h>
AltSoftSerial BTSerial(8, 9); // RX, TX
/*****************INPUTS AND OUTPUTS************************/
#define zerocross 2 // for boards with CHANGEBLE input pins
#define PIR_Sensor 3
#define Light 6
#define LDR_Sensor A0 // Assign LDR sensor to Pin A0.
/************************************************************/
dimmerLamp dimmer1(Light); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero
/************************VARIABLES****************************/
int data_pin = 7;
int Receiver = 0;
int LDR_sensorVal = 0; // Set a variable to store all the values coming from the LDR sensor.
int interval1 = 2000;// Initialize Receiver value as 0.
unsigned char outVal = 0;
unsigned long millis_1 = 0;
boolean result[41]; //holds the result
unsigned int temp; //in celcius
unsigned char receiver;
unsigned long currentMillis;
boolean Auto = false;
boolean PIR_State = false;
float nVPP; // Voltage measured across resistor
float nCurrThruResistorPP; // Peak Current Measured Through Resistor
float nCurrThruResistorRMS; // RMS current through Resistor
float nCurrentThruWire; // Actual RMS current in Wire
/*************************************************************/
void setup()
{
BTSerial.begin(9600);
Serial.begin(9600);
dimmer1.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
dimmer1.setState(OFF);
pinMode(PIR_Sensor, INPUT);
pinMode(LDR_Sensor, INPUT);
}
void loop()
{
currentMillis = millis();
if (currentMillis - millis_1 >= interval1)
{
millis_1 = currentMillis;
temperatureRead();
}
if (digitalRead(PIR_Sensor) == HIGH)
{
PIR_State = true;
}
else if (digitalRead(PIR_Sensor) == LOW)
{
PIR_State = false;
}
Automatic();
if (BTSerial.available() > 0)
{
receiver = BTSerial.read();
if (receiver == 'A')
{
Auto = true;
}
else
{
Auto = false;
}
}
}
void Automatic ()
{
if (Auto == true)
{
if (PIR_State == true)
{
int value = analogRead(LDR_Sensor);
outVal = map(value, 1000, 10, 3, 79);
if (outVal >= 30)
{
dimmer1.setState(ON);
dimmer1.setPower(outVal); // name.setPower(0%-100%)
}
else
{
dimmer1.setState(OFF);
}
}
}
}
void temperatureRead()
{
pinMode(data_pin, OUTPUT);
digitalWrite(data_pin, LOW);
delay(18);
digitalWrite(data_pin, HIGH);
pinMode(data_pin, INPUT_PULLUP);
noInterrupts();
//read 41 bits of signal
for (int i = 0; i <= 40; i++)
{
result[i] = (pulseIn(data_pin, HIGH) > 40);
}
interrupts();
//Extract Temperature (from Byte 3)
temp = 0;
for (int i = 17; i <= 24; i++)
{
temp = temp << 1;
if (result[i]) temp |= 1;
}
BTSerial.print(String(temp) + "#");
Serial.print(String(temp) + "*");
}
The temperature sensor's reading is shown in the image attached.