#include <Arduino.h>
#include <AccelStepper.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int stepPin = 5;
const int dirPin = 4;
const int enablePin = 12;
const int photoResistorPin = A0;
const float GAMMA = 0.7;
const float RL10 = 50;
const long MAX_POSITION = 10000;
const long MIN_POSITION = 0;
float lux;
AccelStepper stepper(1, stepPin, dirPin);
long currentPosition = 0;
void setup() {
lcd.init();
lcd.backlight();
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
rtc.begin();
stepper.setCurrentPosition(0);
}
void loop() {
int analogValue = analogRead(photoResistorPin);
float voltage = analogValue / 1024.0 * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
DateTime now = rtc.now();
lcd.setCursor(0, 0);
lcd.print("Light: ");
lcd.print(lux);
lcd.setCursor(0, 1);
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
currentPosition = stepper.currentPosition();
if (lux > 700 && currentPosition < MAX_POSITION) {
digitalWrite(enablePin, LOW);
stepper.setSpeed(100);
} else if (lux < 300 && currentPosition > MIN_POSITION) {
digitalWrite(enablePin, LOW);
stepper.setSpeed(-100);
} else {
digitalWrite(enablePin, HIGH);
stepper.setSpeed(0);
}
stepper.runSpeed();
}