#include <DHT.h>
// --- Definição dos Pinos ---
#define PIN_LED1 16
#define PIN_LED2 17
#define PIN_BTN1 1
#define PIN_BTN2 2
#define PIN_DHT 35
#define PIN_LDR 4
#define PIN_POT 13
// --- Configuração do DHT ---
#define DHTTYPE DHT22
DHT dht(PIN_DHT, DHTTYPE);
// --- Variáveis de Controle ---
bool estadoLed1 = false;
bool estadoLed2 = false;
// Variáveis para controle de tempo (millis)
unsigned long previousMillis = 0;
const long interval = 1000; // Atualiza sensores a cada 1 segundo (mais rápido agora)
void setup() {
Serial.begin(115200);
// Configura Saídas
pinMode(PIN_LED1, OUTPUT);
pinMode(PIN_LED2, OUTPUT);
// Configura Entradas Digitais (Botões)
pinMode(PIN_BTN1, INPUT_PULLUP);
pinMode(PIN_BTN2, INPUT_PULLUP);
// Entradas Analógicas (LDR e POT) não precisam de pinMode explícito no ESP32,
// mas configurar como INPUT não faz mal.
pinMode(PIN_POT, INPUT);
dht.begin();
Serial.println("--- Sistema de Monitoramento Completo ---");
Serial.println("Sensores: DHT22, LDR, Potenciometro");
}
void loop() {
// ==========================================
// 1. LÓGICA DOS BOTÕES (Resposta Imediata)
// ==========================================
// Botão 1
if (digitalRead(PIN_BTN1) == LOW) {
delay(50); // Debounce
if (digitalRead(PIN_BTN1) == LOW) {
estadoLed1 = !estadoLed1;
digitalWrite(PIN_LED1, estadoLed1);
Serial.println(">> Botao 1: Alterou LED 1");
while(digitalRead(PIN_BTN1) == LOW); // Espera soltar
}
}
// Botão 2
if (digitalRead(PIN_BTN2) == LOW) {
delay(50);
if (digitalRead(PIN_BTN2) == LOW) {
estadoLed2 = !estadoLed2;
digitalWrite(PIN_LED2, estadoLed2);
Serial.println(">> Botao 2: Alterou LED 2");
while(digitalRead(PIN_BTN2) == LOW);
}
}
// ==========================================
// 2. LEITURA DOS SENSORES (Intervalo Definido)
// ==========================================
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// --- Leitura do DHT22 ---
float hum = dht.readHumidity();
float temp = dht.readTemperature();
// --- Leitura do LDR ---
int valorLDR = analogRead(PIN_LDR);
// --- Leitura do Potenciômetro (NOVO) ---
int valorPot = analogRead(PIN_POT); // Lê valor entre 0 e 4095
// Converte o valor bruto (0-4095) para Porcentagem (0-100%)
int porcentagemPot = map(valorPot, 0, 4095, 0, 100);
// Converte para Voltagem real (apenas para demonstração matemática)
float voltagem = (valorPot * 3.3) / 4095.0;
// --- Exibição no Serial Monitor ---
if (!isnan(hum) && !isnan(temp)) {
Serial.println("\n--- STATUS DOS SENSORES ---");
// Exibe Temperatura e Umidade
Serial.print("Ambiente: ");
Serial.print(temp, 1); // 1 casa decimal
Serial.print("C | ");
Serial.print(hum, 1);
Serial.println("%");
// Exibe Luz
Serial.print("Luz (LDR): ");
Serial.print(valorLDR);
if(valorLDR < 1000) Serial.print(" (Escuro)");
else if(valorLDR > 3000) Serial.print(" (Claro)");
Serial.println();
// Exibe Potenciômetro (Destaque da aula)
Serial.print("Potenc.: ");
Serial.print(valorPot);
Serial.print(" (Bruto) | ");
Serial.print(porcentagemPot);
Serial.print("% | ");
Serial.print(voltagem);
Serial.println("V");
Serial.println("---------------------------");
}
}
}Loading
esp32-devkit-c-v4
esp32-devkit-c-v4