#include <Servo.h>
Servo myServo; //name servo
int photocellPin = 0; //define photo cell reading input
int photocellReading; //define photo cell reading variable
void setup()
{
Serial.begin(9600); //initiate serial @ 9600 baud
myServo.attach(11); //define pin 11 as servo signal pin
}
void loop()
{
Serial.print("Brightness = "); //print "Brightness = "
Serial.println(photocellReading); //print the photocell reading
photocellReading = analogRead(photocellPin); //define photocellReading as pin 0 input from LDR
photocellReading = map(photocellReading, 0, 1023, 0, 179); //map the LDR input to a value between 1-180 so the servo can understand it
{
if (photocellReading <= 70){ //if the LDR is showing darkness
myServo.write(20); //then tell the servo to rotate forwards at a steady rate (close shade)
delay(2500); //2.5 second delay while shade is closing?
myServo.write(20);
} //stop rotation after dalay
else if (photocellReading >= 110){ //if the LDR is showing light
myServo.write(180); //then tell the servo to rotate backwards at a steady rate (open shade)
delay(2500); //2.5 second delay while shade is opening?
myServo.write(180);
} //stop rotation after dalay
else(photocellReading >= 71 && photocellReading <= 109);
myServo.write(90);
delay(15);
}
}