Hi there,
i have a project that could control light intensity as well as temperature in a room using Pir sensor, an LDR and a DHT11.
however even if the light intensity is enough, my LED switches on.
help please.
I am using the following as my code :
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302)
#define fan 4
#define photocellPin 0 // photocell attach to A0
#define pirPin 36 //PIR attach to pin 36
#define lightPin 13 // attach light to pin 13
int lightState = 0;
int maxHum = 40;
int maxTemp = 35;
int Threshold = 400;
int isLightOn = LOW;
int isFanOn = LOW;
int val = 0; // variable for reading the pin status
int sensorValue = analogRead(photocellPin); // Read the sensor pin
#define uint8 unsigned char
DHT dht(DHTPIN, DHTTYPE);
uint8 pirValue = 0; //store the PIR state
void setup() {
pinMode(fan, OUTPUT);
pinMode(pirPin, INPUT);
pinMode(photocellPin,INPUT);
pinMode(lightPin,OUTPUT);
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
val = digitalRead(pirPin); // read input value
if (val == HIGH) { // check if motion
if (isLightOn == LOW) {
if (sensorValue > Threshold) // If light level is low is detected
{
digitalWrite(lightPin, HIGH); //switch light on
isLightOn = HIGH;
}
}
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(h > maxHum || t > maxTemp) {
digitalWrite(fan, HIGH);
isFanOn = HIGH;
} else {
if (val == LOW) // check if no motion
if(isLightOn == true) {
digitalWrite(lightPin, LOW); //switch off light
isLightOn = LOW;
}
if (isFanOn == HIGH) {
}
digitalWrite(fan, LOW);
isFanOn = LOW;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
pirValue = digitalRead(pirPin);
Serial.println("Pir State: ");
Serial.println(pirValue);
digitalWrite(lightPin, pirValue);
Serial.print("Photocell Value: ");
Serial.println(analogRead(photocellPin));
}
}
smart_room.ino (2.28 KB)