/*
======================================================
CLIMATIZADOR INTELIGENTE - VERSÃO OTIMIZADA 2026
======================================================
Recursos:
✔ Controle automático de temperatura
✔ Controle automático de umidade
✔ Controle manual
✔ Sistema não bloqueante (millis)
✔ Proteção anti-falha
✔ Histerese anti-oscilação
✔ LCD I2C
✔ Sensor DHT22
✔ Controle de ventilador
✔ Controle de bomba
✔ Controle de iluminação
✔ Menu simples
✔ Compatível com Arduino UNO/Nano
======================================================
*/
#include <Arduino.h>
#include <Wire.h>
#include "LiquidCrystal_I2C.h"
#include <DHT.h>
// ======================================================
// DEFINIÇÕES
// ======================================================
#define DHTPIN 4
#define DHTTYPE DHT22
#define FAN_PIN 5
#define PUMP_PIN 6
#define LIGHT_PIN 7
#define BUZZER_PIN 8
#define BTN_UP 9
#define BTN_DOWN 10
#define BTN_MODE 11
// ======================================================
// OBJETOS
// ======================================================
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHTPIN, DHTTYPE);
// ======================================================
// VARIÁVEIS GLOBAIS
// ======================================================
float temperatura = 0.0;
float umidade = 0.0;
float tempMax = 28.0;
float tempMin = 24.0;
float umiMin = 55.0;
float umiMax = 75.0;
bool fanState = false;
bool pumpState = false;
bool lightState = false;
bool modoAutomatico = true;
unsigned long previousSensor = 0;
unsigned long previousLCD = 0;
unsigned long previousBlink = 0;
unsigned long previousSafety = 0;
const unsigned long sensorInterval = 2000;
const unsigned long lcdInterval = 1000;
const unsigned long safetyInterval = 10000;
bool blinkState = false;
byte tela = 0;
// ======================================================
// SETUP
// ======================================================
void setup() {
pinMode(FAN_PIN, OUTPUT);
pinMode(PUMP_PIN, OUTPUT);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BTN_UP, INPUT_PULLUP);
pinMode(BTN_DOWN, INPUT_PULLUP);
pinMode(BTN_MODE, INPUT_PULLUP);
digitalWrite(FAN_PIN, LOW);
digitalWrite(PUMP_PIN, LOW);
digitalWrite(LIGHT_PIN, LOW);
Serial.begin(9600);
lcd.init();
lcd.backlight();
dht.begin();
telaInicial();
}
// ======================================================
// LOOP PRINCIPAL
// ======================================================
void loop() {
unsigned long currentMillis = millis();
leituraSensores(currentMillis);
controleClimatico();
atualizarLCD(currentMillis);
leituraBotoes();
sistemaSeguranca(currentMillis);
}
// ======================================================
// TELA INICIAL
// ======================================================
void telaInicial() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("CLIMATIZADOR");
lcd.setCursor(0, 1);
lcd.print("INICIALIZANDO");
tone(BUZZER_PIN, 1200, 200);
delay(1500);
lcd.clear();
}
// ======================================================
// LEITURA DOS SENSORES
// ======================================================
void leituraSensores(unsigned long currentMillis) {
if (currentMillis - previousSensor >= sensorInterval) {
previousSensor = currentMillis;
float t = dht.readTemperature();
float h = dht.readHumidity();
if (!isnan(t) && !isnan(h)) {
temperatura = t;
umidade = h;
Serial.print("Temperatura: ");
Serial.println(temperatura);
Serial.print("Umidade: ");
Serial.println(umidade);
}
else {
alarmeSensor();
}
}
}
// ======================================================
// CONTROLE CLIMÁTICO
// ======================================================
void controleClimatico() {
if (!modoAutomatico) {
return;
}
// --------------------------------------------------
// CONTROLE DO VENTILADOR
// --------------------------------------------------
if (temperatura >= tempMax && !fanState) {
fanState = true;
digitalWrite(FAN_PIN, HIGH);
}
if (temperatura <= tempMin && fanState) {
fanState = false;
digitalWrite(FAN_PIN, LOW);
}
// --------------------------------------------------
// CONTROLE DA BOMBA
// --------------------------------------------------
if (umidade <= umiMin && !pumpState) {
pumpState = true;
digitalWrite(PUMP_PIN, HIGH);
}
if (umidade >= umiMax && pumpState) {
pumpState = false;
digitalWrite(PUMP_PIN, LOW);
}
}
// ======================================================
// LCD
// ======================================================
void atualizarLCD(unsigned long currentMillis) {
if (currentMillis - previousLCD >= lcdInterval) {
previousLCD = currentMillis;
lcd.clear();
switch (tela) {
case 0:
lcd.setCursor(0, 0);
lcd.print("TEMP:");
lcd.print(temperatura, 1);
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("UMI:");
lcd.print(umidade, 0);
lcd.print("%");
break;
case 1:
lcd.setCursor(0, 0);
lcd.print("VENT:");
lcd.print(fanState ? "ON " : "OFF");
lcd.setCursor(0, 1);
lcd.print("BOMBA:");
lcd.print(pumpState ? "ON " : "OFF");
break;
case 2:
lcd.setCursor(0, 0);
lcd.print("MODO:");
lcd.print(modoAutomatico ? "AUTO" : "MANUAL");
lcd.setCursor(0, 1);
lcd.print("LAMP:");
lcd.print(lightState ? "ON" : "OFF");
break;
}
}
}
// ======================================================
// BOTÕES
// ======================================================
void leituraBotoes() {
static unsigned long debounce = 0;
if (millis() - debounce < 200) {
return;
}
if (!digitalRead(BTN_MODE)) {
debounce = millis();
tela++;
if (tela > 2) {
tela = 0;
}
tone(BUZZER_PIN, 1000, 80);
}
if (!digitalRead(BTN_UP)) {
debounce = millis();
if (tela == 2) {
modoAutomatico = !modoAutomatico;
tone(BUZZER_PIN, 1400, 100);
}
}
if (!digitalRead(BTN_DOWN)) {
debounce = millis();
if (!modoAutomatico) {
lightState = !lightState;
digitalWrite(LIGHT_PIN, lightState);
tone(BUZZER_PIN, 800, 100);
}
}
}
// ======================================================
// SEGURANÇA
// ======================================================
void sistemaSeguranca(unsigned long currentMillis) {
if (currentMillis - previousSafety >= safetyInterval) {
previousSafety = currentMillis;
// Temperatura crítica
if (temperatura >= 40.0) {
digitalWrite(FAN_PIN, HIGH);
digitalWrite(PUMP_PIN, HIGH);
alarmeCritico();
}
// Umidade crítica
if (umidade <= 20.0) {
digitalWrite(PUMP_PIN, HIGH);
alarmeCritico();
}
}
}
// ======================================================
// ALARME SENSOR
// ======================================================
void alarmeSensor() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ERRO SENSOR");
tone(BUZZER_PIN, 2000, 500);
}
// ======================================================
// ALARME CRÍTICO
// ======================================================
void alarmeCritico() {
for (int i = 0; i < 3; i++) {
tone(BUZZER_PIN, 1800, 200);
delay(250);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ALARME CRIT");
lcd.setCursor(0, 1);
lcd.print("VERIFICAR");
}