#include <Servo.h>
const int ldrPin = A0; // Pin del fotoresistor (salida analógica)
const int servoPin = 9; // Pin del servomotor
const int lightThreshold = 500; // Umbral de luz para controlar las persianas (ajustar según necesidad)
Servo myServo;
enum State { OPEN, CLOSED };
State currentState = CLOSED; // Estado inicial de la persiana
void setup() {
myServo.attach(servoPin);
Serial.begin(9600);
myServo.write(0); // Inicialmente en posición cerrada
}
void loop() {
int ldrValue = analogRead(ldrPin); // Lee el valor del fotoresistor
Serial.print("Luz detectada: ");
Serial.println(ldrValue);
// Control de la persiana basado en la lectura del fotoresistor
if (ldrValue < lightThreshold && currentState == CLOSED) {
abrirPersiana();
currentState = OPEN;
}
else if (ldrValue >= lightThreshold && currentState == OPEN) {
cerrarPersiana();
currentState = CLOSED;
}
delay(1000); // Espera 1 segundo antes de la siguiente lectura
}
void abrirPersiana() {
for (int pos = 0; pos <= 180; pos += 1) {
myServo.write(pos);
delay(15); // Ajustar la velocidad de apertura
}
Serial.println("Persiana abierta.");
}
void cerrarPersiana() {
for (int pos = 180; pos >= 0; pos -= 1) {
myServo.write(pos);
delay(15); // Ajustar la velocidad de cierre
}
Serial.println("Persiana cerrada.");
}