#include <ESP32Servo.h>
#include "RTClib.h"
#include <Wire.h>
#include "SPI.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd (0x27, 16,2);
const float GAMMA = 0.7;
const float RL10 = 50;
const int relayPinUV = 34;
RTC_DS1307 rtc;
int pos;
#define Trig 27
#define Echo 14
#define push 13
Servo myservo1;
Servo myservo2;
void setup() {
Serial.begin(115200);
// print the Message on the LCD.
lcd. init ();
// Turn on the backlight on LCD.
lcd. backlight ();
myservo1.attach(19);
myservo2.attach(18);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(relayPinUV, OUTPUT);
pinMode(push, INPUT);
// put your setup code here, to run once:
Wire.begin(21,22);
rtc.begin();
}
float readDistanceCM() {
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(10);
digitalWrite(Trig, LOW);
int duration = pulseIn(Echo, HIGH);
return duration * 0.034 / 2;
}
void loop() {
DateTime now = rtc.now();
// put your main code here, to run repeatedly:
int analogValue = analogRead(12);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
float distance = readDistanceCM();
if(lux >3800 & distance >=200){
myservo1.write(90);
myservo2.write(90);
digitalWrite( relayPinUV, LOW);
}
else if (lux <3800 & lux >2800 & distance >=200) {
myservo1.write(0);
myservo2.write(90);
}
else if (lux <2800 & lux >0 & distance <=200) {
myservo1.write(90);
myservo2.write(0);
delay( 600);
}
else if (lux <3600 ) {
digitalWrite( relayPinUV, HIGH);
}
else if (lux <3600 ) {
digitalWrite( relayPinUV, LOW);
}
else {
myservo1.write(0);
myservo2.write(90);
}
if (digitalRead (push) == HIGH)
myservo1.write(30);
//Here cursor is placed on first position (col: 0) of the second line (row: 1)
lcd. setCursor (0, 1);
// We write the number of seconds elapsed
lcd.println(lux);
delay (100);
Serial.print("Lux: ");
Serial.print(lux);
}