// Bibliotecas
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
// Constantes
const int OneWireBus = 4;
const int DHT_PIN = 15;
const float BETA = 3950;
const int pinoNTC = 0;
// Pinos dos LEDs
#define led1R 13
#define led1G 12
#define led1B 14
#define led2R 27
#define led2G 23
#define led2B 25
// Objetos dos sensores
OneWire oneWire(OneWireBus);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
DHTesp dhtSensor;
// Variáveis auxiliares
float somaCelsius = 0, mediaCelsius = 0;
unsigned long previousMillis = 0;
const long interval = 500;
const int numMedidas = 10;
// Variáveis para armazenar o estado anterior dos LEDs
int estadoLed1R = LOW, estadoLed1G = LOW, estadoLed1B = LOW;
int estadoLed2R = LOW, estadoLed2G = LOW, estadoLed2B = LOW;
// Variáveis para controle do LCD com millis()
unsigned long previousLCDMillis = 0;
const long lcdInterval = 2000;
bool mostrarTela1 = true;
void setup() {
//Inicialização monitor serial, sensores e LCD:
Serial.begin(115200);
sensors.begin();
lcd.init();
lcd.backlight();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
//Definição do modo de cada pino:
pinMode(led1R, OUTPUT);
pinMode(led1G, OUTPUT);
pinMode(led1B, OUTPUT);
pinMode(led2R, OUTPUT);
pinMode(led2G, OUTPUT);
pinMode(led2B, OUTPUT);
pinMode(pinoNTC, INPUT);
}
void atualizarLCD(float temperaturaC, float mediaCelsius, TempAndHumidity data) {
//Tempo Atual:
unsigned long currentMillis = millis();
if (currentMillis - previousLCDMillis >= lcdInterval) {
previousLCDMillis = currentMillis;
lcd.clear();
if (mostrarTela1) {
lcd.setCursor(0, 0);
lcd.print("DS18B20:");
lcd.print(temperaturaC);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("DHT22: " + String(data.temperature, 2));
lcd.print("C");
}
else {
lcd.setCursor(0, 0);
lcd.print("NTC: ");
lcd.print(mediaCelsius);
lcd.print("C");
}
mostrarTela1 = !mostrarTela1; // Alterna entre telas
}
}
void loop() {
//Millis = tempo atual:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Leitura do NTC
somaCelsius = 0;
for (int i = 0; i < numMedidas; i++) {
int analogValue = analogRead(pinoNTC);
float celsius = 1 / (log(1 / (4095.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
somaCelsius += celsius;
}
mediaCelsius = somaCelsius / numMedidas;
// Leituras dos sensores
TempAndHumidity data = dhtSensor.getTempAndHumidity();
sensors.requestTemperatures();
float temperaturaC = sensors.getTempCByIndex(0);
// Atualiza LEDs SOMENTE se o estado mudar
int novoLed1R = LOW, novoLed1G = LOW, novoLed1B = LOW;
int novoLed2R = LOW, novoLed2G = LOW, novoLed2B = LOW;
// LED1 - Baseado na temperatura DS18B20
if (temperaturaC > 30) {
novoLed1R = HIGH;
}
if (temperaturaC > 20 && temperaturaC < 30) {
novoLed1G = HIGH;
}
if(temperaturaC < 19) {
novoLed1B = HIGH;
}
// LED2 - Comparação entre NTC e DS18B20
if (mediaCelsius > temperaturaC - 0.15) {
novoLed2R = HIGH;
}
if (mediaCelsius > temperaturaC - 0.15 && mediaCelsius < temperaturaC + 0.15) {
novoLed2G = HIGH;
}
if(mediaCelsius < temperaturaC + 0.15) {
novoLed2B = HIGH;
}
// Atualiza apenas se houver mudanca de estado
if (novoLed1R != estadoLed1R || novoLed1G != estadoLed1G || novoLed1B != estadoLed1B) {
digitalWrite(led1R, novoLed1R);
digitalWrite(led1G, novoLed1G);
digitalWrite(led1B, novoLed1B);
estadoLed1R = novoLed1R;
estadoLed1G = novoLed1G;
estadoLed1B = novoLed1B;
}
if (novoLed2R != estadoLed2R || novoLed2G != estadoLed2G || novoLed2B != estadoLed2B) {
digitalWrite(led2R, novoLed2R);
digitalWrite(led2G, novoLed2G);
digitalWrite(led2B, novoLed2B);
estadoLed2R = novoLed2R;
estadoLed2G = novoLed2G;
estadoLed2B = novoLed2B;
}
// Exibe leituras no Serial Monitor
Serial.println("Leituras DS18B20:");
Serial.println(String(temperaturaC) + "ºC");
Serial.println("Leituras DHT22:");
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("Leitura NTC:");
Serial.println("Temp: " + String(mediaCelsius) + "°C");
// Chama função para atualizar LCD sem delay()
atualizarLCD(temperaturaC, mediaCelsius, data);
}
}