#include <Servo.h>
const int LDRPin = A0; // Pin connected to LDR
const int PotPin = A1; // Pin connected to potentiometer
const int LEDPin = 13; // Pin connected to LED
const int ServoPin = 5; // Pin connected to servo
Servo myServo; // Create a servo object
void setup() {
pinMode(LDRPin, INPUT);
pinMode(PotPin, INPUT);
pinMode(LEDPin, OUTPUT);
myServo.attach(ServoPin);
Serial.begin(9600); // Begin serial communication
}
void loop() {
int LDRValue = analogRead(LDRPin); // Read the value from the LDR
int PotValue = analogRead(PotPin); // Read the value from the potentiometer
int threshold = map(PotValue, 0, 1023, 0, 1023); // Map potentiometer value to threshold range
Serial.print("LDR Value: ");
Serial.print(LDRValue);
Serial.print(" - Potentiometer Value: ");
Serial.print(PotValue);
Serial.print(" - Threshold: ");
Serial.println(threshold);
if (LDRValue < threshold) {
// Rain detected (simulated by darkness)
Serial.println("Rain detected. Roof OPEN and Light ON");
myServo.write(0); // Turn on the servo (open the roof)
digitalWrite(LEDPin, HIGH); // Turn on LED
} else {
// No rain detected
Serial.println("No rain detected. Roof CLOSE and Light OFF");
myServo.write(90); // Turn off the servo (close the roof)
digitalWrite(LEDPin, LOW); // Turn off LED
}
delay(1000); // Small delay for stability
}