#include <ESP32Servo.h>
const float GAMMA = 0.7;
const float RL10 = 50;
int pos;
#define Trig 27
#define Echo 14
Servo myservo1;
Servo myservo2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
myservo1.attach(19);
myservo2.attach(21);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
}
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() {
// 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));
if(lux >3800 ){
myservo1.write(90);
}
else {
myservo1.write(0);
}
float distance = readDistanceCM();
if (distance <200) {
myservo2.write(90);
}
else {
myservo2.write(0);
}
}