/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
https://create.arduino.cc/cloud/things/96791fb1-61b5-43ea-b5be-32816ed4f92a
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
String corLed;
float temperatura; -> leitura
int luminosidade; -> leitura
bool estadoLed;
bool estadoTempLdr;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
#include "thingProperties.h"
#include <DHT.h>
#include <DHT_U.h>
// --- Mapeamento de Pinos ---
#define POT_PIN 14
#define LDR_PIN 32
#define BUTTON_PIN 16
#define DHTPIN 15
#define BUZZER_PIN 4
#define LED_BUILTIN_PIN 48
#define PIN_R 25
#define PIN_G 26
#define PIN_B 27
#define LED_PIN 17
#define DHTTYPE DHT22
#define LUX_THRESHOLD 2200
DHT dht(DHTPIN, DHTTYPE);
const float GAMMA = 0.7;
const float RL10 = 50;
const int brightness = 4095;
int lux_value = 0;
int analogValue;
// Variáveis globais
volatile bool led_enabled = true;
volatile unsigned long last_interrupt_time = 0;
volatile bool temp_enabled = true;
volatile bool buzzer_enabled = true;
volatile bool lux_enabled = true;
String corEscolhida = ""; // cor recebida do Cloud
bool mostrandoCorCloud = false;
float temperature = 0.0;
int pot_value = 0;
bool temp_warning = false;
void IRAM_ATTR handleButtonInterrupt() {
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200) {
led_enabled = !led_enabled;
estadoLed = led_enabled;
last_interrupt_time = interrupt_time;
}
}
void setLedColorFromPot(int value) {
int r=0, g=0, b=0;
if (value < 820) { // Vermelho -> Laranja
r = 4095;
g = map(value, 0, 819, 0, 2650);
b = 0;
} else if (value < 1640) { // Laranja -> Amarelo
r = 4095;
g = map(value, 820, 1639, 2650, 4095);
b = 0;
} else if (value < 2460) { // Amarelo -> Branco
r = 4095;
g = 4095;
b = map(value, 1640, 2459, 0, 4095);
} else if (value < 3280) { // Branco -> Azul Claro
r = map(value, 2460, 3279, 4095, 0);
g = map(value, 2460, 3279, 4095, 3200);
b = 4095;
} else { // Azul Claro -> Azul
r = 0;
g = map(value, 3280, 4095, 3200, 0);
b = 4095;
}
setRGB(r,g,b);
}
void setRGB(uint8_t red, uint8_t green, uint8_t blue) {
analogWrite(PIN_R, red);
analogWrite(PIN_G, green);
analogWrite(PIN_B, blue);
}
void readSensors() {
if (temp_enabled) {
temperature = dht.readTemperature();
temp_warning = (temperature < 0 || temperature > 23);
analogValue = analogRead(LDR_PIN);
float volts = analogValue / 4095.0 * 3.3;
float resistance = 2000 * volts / (3.3 - volts);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1.0 / GAMMA));
lux_value = lux / 10;
} else {
temperature = 0.0;
analogValue = 0;
}
pot_value = analogRead(POT_PIN);
}
void updateLed() {
bool alert = (temp_warning || lux_value > LUX_THRESHOLD); // Verifica se há alerta
if (!led_enabled) {
setLedColorFromPot(pot_value); // LED embutido desligado se o sistema estiver desligado
digitalWrite(LED_PIN, LOW); // LED de alerta também desligado ao desativar sistema
} else {
if (alert) {
setRGB(0,0,0);
digitalWrite(LED_PIN, HIGH); // LED de alerta ligado apenas durante alerta
}else{
setLedColorFromPot(pot_value);
}
}
}
void updateBuzzer() {
if (led_enabled && temp_warning && (lux_enabled || temp_enabled)) {
tone(BUZZER_PIN, 2000);
} else {
noTone(BUZZER_PIN);
}
}
void setup() {
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
//delay(1500);
// Defined in thingProperties.h // <-------EXTREMAMENTE IMPORTANTE USAR ESSE COMENTARIO
//initProperties();
// Connect to Arduino IoT Cloud
// ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
//setDebugMessageLevel(2);
//ArduinoCloud.printDebugInfo();
delay(500);
Serial.begin(115200);
dht.begin();
pinMode(LDR_PIN, INPUT);
pinMode(POT_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_BUILTIN_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(PIN_R, OUTPUT);
pinMode(PIN_G, OUTPUT);
pinMode(PIN_B, OUTPUT);
Serial.println("Controle do LED RGB via potenciômetro iniciado!");
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, LOW);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), handleButtonInterrupt, FALLING);
}
void loop() {
// ArduinoCloud.update();
updateLed();
if (led_enabled) {
readSensors();
updateBuzzer();
updateLed();
if (Serial.available() > 0) {
corEscolhida = Serial.readStringUntil('\n'); // lê até ENTER
corEscolhida.trim(); // remove espaços extras
Serial.println(corEscolhida);
}
// if de teste
Serial.println("Temp: " + String(temperature));
Serial.println("Lux: " + String(lux_value));
temperatura = temperature;
luminosidade = lux_value;
if (corEscolhida != "" ) {
if (corEscolhida == "vermelho") setRGB(brightness,0,0);
else if (corEscolhida == "amarelo") setRGB(brightness,brightness,0);
else if (corEscolhida == "azul") setRGB(0,0,brightness);
corEscolhida = "";
delay(1000);
setLedColorFromPot(pot_value);
}
} else {
updateBuzzer();
setRGB(0,0,0);
digitalWrite(LED_PIN, LOW);
Serial.println("LED OFF");
temperatura =0;
luminosidade = 0;
Serial.println("Temp: " + String(temperatura));
Serial.println("Lux: " + String(luminosidade));
}
delay(100);
}
/*
Since LigarLed is READ_WRITE variable, onLigarLedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onEstadoLedChange() {
// Add your code here to act upon LigarLed change
led_enabled = estadoLed; // Define conforme o valor recebido da nuvem
}
/*
Since CorLed is READ_WRITE variable, onCorLedChange() is
executed every time a new value is received from IoT Cloud.
*/
void onCorLedChange() {
// Add your code here to act upon CorLed change
// Gera cores dinâmicas conforme o valor
// (vermelho → verde → azul)
corEscolhida = corLed;
}
//temp e ldr
void onEstadoTempLdrChange() {
// Add your code here to act upon AtivarTemp change
temp_enabled = estadoTempLdr; // Define conforme o valor recebido da nuvem
}