/* THIS PROGRAM IS DESIGNED TO OPEN CURTAINS IF THE BRGHTNESS IS ABOVE
100 LUX AND CLOSE CURTAINS IF THE BRIGHTNESS IS BELOW 100 LUX. */
// Importing stepper motor and LCD libraries
#include <Stepper.h>
#include <LiquidCrystal.h>
#define STEPS_PER_REVOLUTION 200
// Pins connected to stepper motor
#define STEPPER_PIN_1 5
#define STEPPER_PIN_2 4
#define STEPPER_PIN_3 3
#define STEPPER_PIN_4 2
// Pin connected to light sensor
#define LIGHT_SENSOR_PIN A0
// Threshold value for light level (set to 511 i.e 100 lux)
#define LIGHT_THRESHOLD 511
//Initialize Stepper module
Stepper stepper(STEPS_PER_REVOLUTION, STEPPER_PIN_1, STEPPER_PIN_2, STEPPER_PIN_3, STEPPER_PIN_4);
//Initialize LCD module
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
bool curtainsClosed = false;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
stepper.setSpeed(30);
lcd.begin(16, 2);
lcd.print("Curtains");
}
void loop() {
curtains();
}
// Function to compare light level with threshold
void curtains() {
// Convert the analog value from LDR into lux value:
int lightLevel = analogRead(LIGHT_SENSOR_PIN);
float voltage = lightLevel / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
lcd.clear();
//Display brightness on LCD
lcd.setCursor(0,1);
lcd.print("Brightness:");
lcd.print(int(lux));
if (lightLevel <= LIGHT_THRESHOLD && !curtainsClosed) {
lcd.clear();
closeCurtains();
}
else if (lightLevel > LIGHT_THRESHOLD && curtainsClosed) {
lcd.clear();
openCurtains();
}
delay(100); //Time taken to check again
}
// Function to open curtains
void openCurtains() {
lcd.print("Curtains Closed");
stepper.step(-200);
curtainsClosed=false;
}
// Function to close curtains
void closeCurtains() {
lcd.print("Curtains Opened");
stepper.step(200);
curtainsClosed=true;
}