#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
// Define pins
#define DHTPIN 2
#define MOTIONPIN A0
#define PHOTOCELLPIN 3
#define LEDPIN 4
// Define DHT sensor type
#define DHTTYPE DHT22
// Define servo driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
// Define variables
int motionState = 0;
int previousMotionState = 0;
int lightValue = 0;
int ledValue = 0;
int threshold = 50;
int ledBrightness = 0;
float temperature, humidity;
int coolingState = 0;
int heatingState = 0;
int coolingPin = 0;
int heatingPin = 1;
// Initialize sensors
DHT dht(DHTPIN, DHTTYPE);
//////////////////////////////////////// Warning error in this part of code
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize servo driver
pwm.begin();
pwm.setPWMFreq(60);
// Set LED pin as output
pinMode(LEDPIN, OUTPUT);
/************************* cooling and heating pin serve as input to system then control temperature of climatisor ************************/
///////////////////////////////////////// warning fiw erreur --> input
// Set cooling and heating pins as output
pinMode(coolingPin, OUTPUT);
pinMode(heatingPin, OUTPUT);
}
void loop() {
///////////////////////////////// where is the function readTemperature() - readHumidity()
// Read temperature and humidity values
temperature = dht.readTemperature();
humidity = dht.readHumidity();
// Read motion sensor value
motionState = analogRead(MOTIONPIN);
// Read photoresistor value
lightValue = analogRead(PHOTOCELLPIN);
// Calculate LED brightness based on photoresistor value
ledBrightness = map(lightValue, 0, 1023, 0, 255);
// Turn on LED if motion is detected and it's dark
if (motionState > threshold && lightValue < 100) {
digitalWrite(LEDPIN, HIGH);
ledValue = 1;
}
else {
digitalWrite(LEDPIN, LOW);
ledValue = 0;
}
// Control cooling system based on temperature value
if (temperature > 30 && coolingState == 0) {
digitalWrite(coolingPin, HIGH); // Turn on cooling
coolingState = 1;
}
else if (temperature < 25 && coolingState == 1) {
digitalWrite(coolingPin, LOW); // Turn off cooling
coolingState = 0;
}
// Control heating system based on temperature and humidity values
if (temperature < 20 || humidity < 40) {
digitalWrite(heatingPin, HIGH); // Turn on heating
heatingState = 1;
}
else if (temperature > 25 && heatingState == 1) {
digitalWrite(heatingPin, LOW); // Turn off heating
heatingState = 0;
}
// Print sensor values to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C, Humidity: ");
Serial.print(humidity);
Serial.print("%, Light: ");
Serial.print(lightValue);
Serial.print(", Motion: ");
Serial.print(motionState);
Serial.print(", LED: ");
Serial.print(ledValue);
Serial.print(", Heating: ");
Serial.println(pwm.getPWM(0));
// Delay to prevent flooding the serial monitor
delay(1000);
}