#include <Servo.h>
Servo umbrellaServo; // Define servo motor
const int ldrPin = A0; // LDR sensor pin
const int rainPin = 2; // Rain sensor pin
int brightThreshold = 500; // Adjust this value based on your environment
bool isOpen = false; // Variable to track umbrella state
void setup() {
Serial.begin(9600);
umbrellaServo.attach(9); // Attach the servo to pin 9
pinMode(ldrPin, INPUT);
pinMode(rainPin, INPUT);
}
void loop() {
int brightness = analogRead(ldrPin);
int rainValue = digitalRead(rainPin);
Serial.print("Brightness: ");
Serial.println(brightness);
Serial.print("Rain: ");
Serial.println(rainValue);
if (rainValue == HIGH) {
// It's raining, open the umbrella
openUmbrella();
} else if (brightness < brightThreshold) {
// It's not really bright, open the umbrella
openUmbrella();
} else {
// No rain and bright, close the umbrella
closeUmbrella();
}
}
void openUmbrella() {
if (!isOpen) {
umbrellaServo.write(180); // Rotate servo clockwise to open umbrella
isOpen = true;
Serial.println("Opening umbrella");
delay(1000); // Adjust delay based on the time it takes to open the umbrella
}
}
void closeUmbrella() {
if (isOpen) {
umbrellaServo.write(0); // Rotate servo counterclockwise to close umbrella
isOpen = false;
Serial.println("Closing umbrella");
delay(1000); // Adjust delay based on the time it takes to close the umbrella
}
}