#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// --- LCD ---
LiquidCrystal_I2C lcd(0x27, 16, 2);
// --- Pines ---
const int pinLDR = 32;
const int pinBoton = 33;
const int pinR = 27;
// --- Variables ---
int umbralLuz = 2000; // Ajustar según condiciones de luz
int estadoBoton = 0;
int ultimoEstadoBoton = 0;
int modoColor = 0;
void setup() {
Serial.begin(115200);
// Configuración de pines
pinMode(pinBoton, INPUT_PULLDOWN);
pinMode(pinR, OUTPUT);
// Inicializar LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Sistema de Luz Automatico");
apagarLED();
}
void loop() {
int valorLuz = analogRead(pinLDR);
estadoBoton = digitalRead(pinBoton);
// --- Control automático por luz ---
if (valorLuz < umbralLuz) {
if (modoColor == 0) {
encenderLED(255, 255, 255); // Blanco
mostrarLCD("Auto: rojo");
} else {
cambiarColor(modoColor);
}
} else {
apagarLED();
mostrarLCD("Ambiente iluminado");
}
// --- Detección del botón ---
if (estadoBoton == HIGH && ultimoEstadoBoton == LOW) {
modoColor++;
if (modoColor > 3) modoColor = 0; // 0=auto, 1=rojo, 2=verde, 3=azul
delay(200);
}
ultimoEstadoBoton = estadoBoton;
delay(100);
}
// --- Funciones auxiliares ---
void apagarLED() {
analogWrite(pinR, 0);
}
void encenderLED(int r, int g, int b) {
analogWrite(pinR, r);
}
void cambiarColor(int modo) {
switch (modo) {
case 1:
encenderLED(255, 0, 0);
mostrarLCD("Color: Rojo, verde");
break;
}
}
void mostrarLCD(String texto) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(texto);
}