#include <Servo.h>
const int PIN_LDR = A0;
const int PIN_MOTOR = 9;
const int UMBRAL_OSCURIDAD = 500;
Servo ventilador;
void setup() {
ventilador.attach(PIN_MOTOR);
Serial.begin(9600);
}
void loop() {
int valorLuz = analogRead(PIN_LDR);
Serial.print("Nivel de luz: ");
Serial.println(valorLuz);
if (valorLuz < UMBRAL_OSCURIDAD) {
for (int pos = 0; pos <= 180; pos += 1) {
ventilador.write(pos);
delay(15);
}
for (int pos = 180; pos >= 0; pos -= 1) {
ventilador.write(pos);
delay(15);
}
} else {
ventilador.write(0);
}
}