#include "DHT.h"
#include <ESP32Servo.h>
// Déclaration des constantes pour les seuils de luminosité
const int DARK = 200;
const int LIGHT = 500;
const int BRIGHT = 800;
const int VERY_BRIGHT = 1000;
// Déclaration des broches pour les composants
#define PIN_RED 12
#define PIN_GREEN 14
#define PIN_BLUE 27
#define digital_In 34
#define analog_In 35
#define POT_PIN 33
#define SERVO_PIN 22
#define DHT_PIN 13
//Seuil CO2 (Scenario 1)
const int UNDER_7500 = 45;
const int UNDER_10000 = 90;
const int UNDER_15000 = 135;
//Seuil temperature (Scenario 2)
const int SEUIL_TEMP = 23;
// Déclaration des objets
DHT dht(DHT_PIN, DHT22);
Servo servo;
// Déclaration du tableau de broches LED
const int ledCount = 10;
int ledPins[] = {23, 22, 21, 19, 18, 5, 17, 16, 4, 2};
// Variable pour l'angle du servo
int CURRENT_ANGLE = 0;
void luminosite() {
int analogValue = analogRead(analog_In);
float voltage = analogValue / 4096.0 * 5.0;
float resistance = 2000.0 * voltage / (1.0 - voltage / 5.0);
float lux_a = pow(50.0 * 1000.0 * pow(10.0, 0.7) / resistance, 1.0 / 0.7);
Serial.print("Analog: Lux ");
Serial.println(lux_a);
// Allumer les LED en fonction de la luminosité
if (lux_a <= DARK) {
Serial.println("Dark");
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
}
} else if (lux_a <= LIGHT) {
Serial.println("Light");
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
}
} else if (lux_a <= BRIGHT) {
Serial.println("Bright");
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], LOW);
}
} else {
Serial.println("Very Bright");
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[0], HIGH);
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
}
}
delay(3000);
}
void fenetre() {
int potValue = analogRead(POT_PIN);
potValue = map(potValue, 0, 4095, 0, 17000);
int angle = 0;
if (potValue < 400) {
angle = 0;
Serial.println("CO2 concentration below 400ppm, close window");
} else if (potValue < 7500) {
angle = UNDER_7500;
Serial.print("CO2 concentration below 7500ppm, open window by ");
Serial.println(UNDER_7500);
} else if (potValue < 10000) {
angle = UNDER_10000;
Serial.print("CO2 concentration below 10000ppm, open window by ");
Serial.println(UNDER_10000);
} else if (potValue < 15000) {
angle = UNDER_15000;
Serial.print("CO2 concentration below 15000ppm, open window by ");
Serial.println(UNDER_15000);
} else {
Serial.println("CO2 concentration over 15000ppm, open window fully");
angle = 180;
}
int difference = CURRENT_ANGLE - angle;
if (difference < 0) {
ouvrirServo(abs(difference));
} else {
fermerServo(difference);
}
CURRENT_ANGLE = angle;
}
void ouvrirServo(int angle) {
for (int i = 0; i <= angle; i++) {
servo.write(CURRENT_ANGLE + i);
delay(15);
}
}
void fermerServo(int angle) {
for (int i = 0; i <= angle; i++) {
servo.write(CURRENT_ANGLE - i);
delay(15);
}
}
void setup() {
Serial.begin(9600);
dht.begin();
servo.attach(SERVO_PIN);
servo.write(0);
pinMode(POT_PIN, INPUT);
pinMode(PIN_RED, OUTPUT);
pinMode(PIN_GREEN, OUTPUT);
pinMode(PIN_BLUE, OUTPUT);
pinMode(digital_In, INPUT);
pinMode(analog_In, INPUT);
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
pinMode(ledPins[thisLed], OUTPUT);
}
delay(2000);
}
void loop() {
fenetre();
luminosite();
delay(5000);
}