int LDRInput=A0; //Set Analog Input A0 for LDR.
const int directionPin = 2;
const int stepPin = 4;
const int SWITCH_PIN = 8;
int stepsPerRotation = 200;
int stepDelayMicros = 1000;
int totalSteps = 4 * stepsPerRotation;
int statostart;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
Serial.begin(9600);
pinMode(LDRInput,INPUT);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(SWITCH_PIN, INPUT);
}
void loop() {
int value = analogRead(LDRInput);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value); //prints the values coming from the sensor on the screen
float voltage = value / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
Serial.println("Resistance value: ");
Serial.println(resistance);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
if (lux >=800) {
Serial.println("Direct sunlight! ");
} else {
Serial.println("No direct sunlight!");
}
delay(1000);
int switchState = digitalRead(SWITCH_PIN);
// If the library is closed (slide switch is in the LOW state)
if (switchState == LOW) {
// Keep the blinds rolled down
rolldown();
} else {
// The library is open, so check the light intensity
// Check if direct sunlight is detected
if (lux >= 800) {
// Roll down the blinds
rolldown();
} else {
// Roll up the blinds
rollup();
}
}
}
void rolldown(){
// Loop to generate pulses for the specified number of steps
digitalWrite(directionPin, HIGH);
for (int i = 0; i < totalSteps; ++i) {
// Pulse the STEP pin to generate a step
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelayMicros);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelayMicros);
}
}
void rollup(){
// Loop to generate pulses for the specified number of steps
digitalWrite(directionPin, LOW);
for (int i = 0; i < totalSteps; ++i) {
// Pulse the STEP pin to generate a step
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelayMicros);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelayMicros);
}
}