#include <Servo.h>
// Define pin connections
const int potentiometerPin = 35;
const int ldrPin = 26;
const int servoPin = 5;
// Define servo object
Servo myServo;
// Variables to store sensor readings
int potentiometerValue = 0;
int ldrValue = 0;
int threshold = 2000; // Example threshold value for LDR
int servoPosition = 0;
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Attach the servo to the servo pin
myServo.attach(servoPin);
// Initialize servo position
myServo.write(0);
}
void loop() {
// Read the potentiometer value
potentiometerValue = analogRead(potentiometerPin);
// Map the potentiometer value to the desired servo position range (0-180 degrees)
int mappedPotentiometerValue = map(potentiometerValue, 0, 4095, 0, 180);
// Read the LDR value
ldrValue = analogRead(ldrPin);
// Print values for debugging
Serial.print("Potentiometer: ");
Serial.print(potentiometerValue);
Serial.print("\tLDR: ");
Serial.print(ldrValue);
Serial.print("\tServo Position: ");
Serial.println(mappedPotentiometerValue);
// Control the servo based on the potentiometer and LDR values
if (ldrValue < threshold) { // If it is dark (LDR value is below threshold)
myServo.write(mappedPotentiometerValue); // Set the servo position based on potentiometer
} else { // If it is bright (LDR value is above threshold)
myServo.write(0); // Close the roof
}
// Delay to avoid spamming the serial monitor
delay(500);
}