I'm doing a project in electronics that is suppose to be an Automatic Fan and Lighting system on a study area(outdoor). The sensors we are using are PIR(2 pcs.), LDR, and DHT22. Our project is suppose to turn the bulb/s (there are 2) On or Off, dependent on the ambient light and human presence, and at the same time turn the electric fan On or Off, and its speed using CBB61 capacitor, dependent on the temperature and human presence as well. This project's aim is to turn the fan/bulb/s off when not in use to conserve electricity and make the system fully automatic. But as we run the code on the Arduino Mega attached with 2 way relays(we are using 3pcs. of 2 way relays, 1 for the bulbs and 2 for the fan speed control) connecting the load to AC, some issues came up. The code are as follows: (There is no error upon uploading the program)
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 22 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define fan 10
#define fan1 5
#define fan2 6
#define fan3 7
DHT dht(DHTPIN, DHTTYPE);
int bulb = 11;
int bulb2 = 12;
int ldr = A0;
int pir = 24;
int pir2 = 26;
float t = 0;
unsigned long current = 0;
unsigned long previous = 0;
unsigned long previous2 = 0;
unsigned long previous3 = 0;
unsigned long previous4 = 0;
const long interval = 5000;
int pirState = 0;
int pirState2 = 0;
int ldrStatus = 0;
int tempStatus = 0;
void setup() {
pinMode(pir, INPUT); // declare sensor as input
pinMode(pir2, INPUT); // declare sensor as input
pinMode(ldr, INPUT); // ldr
pinMode(bulb, OUTPUT); // declare LED as output
pinMode(bulb2, OUTPUT); // declare LED as output
pinMode(fan, OUTPUT);
pinMode(fan1, OUTPUT);
pinMode(fan2, OUTPUT);
pinMode(fan3, OUTPUT);
digitalWrite(bulb, HIGH);
digitalWrite(bulb2, HIGH);
digitalWrite(fan, HIGH);
digitalWrite(fan1, HIGH);
digitalWrite(fan2, HIGH);
digitalWrite(fan3, HIGH);
Serial.begin(9600);
dht.begin();
}
void loop() {
////wait for a few seconds to a minute for the system to start up and for the sensors to calibrate
ldrStatus = analogRead(ldr);
pirState = digitalRead(pir);
pirState2 = digitalRead(pir2);
current = millis();
////////////fan
if (pirState == HIGH || pirState2 == HIGH)
{
if (t <= 22) {
if (current - previous4 > interval) {
tempStatus = 0;
Serial.println("fan off");
}
}
else if (t <= 24 && t > 22) {
tempStatus = 1;
Serial.println("fan 1");
previous4 = current;
}
else if (t <= 26 && t > 24) {
tempStatus = 2;
Serial.println("fan 2");
previous4 = current;
}
else {
tempStatus = 3;
Serial.println("fan 3");
previous4 = current;
}
}
else {
if (current - previous4 > interval) {
tempStatus = 0;
Serial.println("*******************************************************************************"); //no presence
}
}
/////////////bulb 1
if (pirState == HIGH && ldrStatus < 200) {
digitalWrite(bulb, LOW);
Serial.println("bulb ON");
previous = current;
}
if (pirState == LOW || ldrStatus > 200) {
if (current - previous > interval) {
digitalWrite(bulb, HIGH);
Serial.println("bulb OFF");
}
}
////////////bulb2
if (pirState2 == HIGH && ldrStatus < 200) {
digitalWrite(bulb2, LOW);
Serial.println("bulb 2 ON");
previous2 = current;
}
if (pirState2 == LOW || ldrStatus > 200) {
if (current - previous2 > interval) {
digitalWrite(bulb2, HIGH);
Serial.println("bulb 2 OFF");
}
}
switch (tempStatus) { //we are using CBB61 capacitor for this one
case 0:
digitalWrite(fan, HIGH);
digitalWrite(fan3, HIGH); //fan = 0
digitalWrite(fan2, HIGH);
digitalWrite(fan1, HIGH);
break;
case 1:
digitalWrite(fan, LOW);
digitalWrite(fan3, HIGH); //fan = 1
digitalWrite(fan2, HIGH);
digitalWrite(fan1, LOW);
break;
case 2:
digitalWrite(fan, LOW);
digitalWrite(fan3, HIGH); //fan = 2
digitalWrite(fan2, LOW); //on the rotary switch, we noticed the connection of fan 2 to be conducting fan 1 at the same time
digitalWrite(fan1, LOW);
break;
case 3:
digitalWrite(fan, LOW);
digitalWrite(fan3, LOW); //fan =3
digitalWrite(fan2, HIGH);
digitalWrite(fan1, HIGH);
break;
default: break;
}
if (current - previous3 > interval) { //reads the temperature only every 5 sec.
t = dht.readTemperature(); // Read temperature as Celsius
previous3 = current;
/*
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
} else{}
*/
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
}
} //closing bracket of Void Loop
So I tried to trace back all the brackets and everything that I thought would give me the expected output, but...
*On COM5, it sometimes print consecutive "bulb ON" so many times, or any other serial prints consecutively, I thought it would be alternating with other serial prints since the code goes from top to bottom right? And if it does that specific line once, it will then check whether to perform the other lines depending on the bracketing and if's. But most of the times it does print it alternately.
*On COM5, (even on the actual relay base on the led indicator) it sometimes pauses. Like inconsistent and random pauses. The program stops for a while, its on auto scroll so pausing would be easy to observe.
*I also think theres a flaw in my coding -_- the fan relay usually displays no presence even if the PIRs are high (base on the relay led).
thanks for the time--