#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Servo.h>
const int SCREEN_WIDTH = 128;
const int SCREEN_HEIGHT = 64;
const int SCREEN_ADDRESS =0x3C;
const int led = 6;
const int servo1Pin = 8;
const int servo2Pin = 9;
const int lightSensor1Pin = A0;
const int lightSensor2Pin = A1;
Servo myservo1;
Servo myservo2;
Adafruit_SSD1306 display( SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int count =1;
int currentAngle = 90;
int targetAngle = 90;
int smoothingFactor = 10;
int minAngle = 0;
int maxAngle = 180;
int maxValue = 500;
const float GAMMA = 0.7;
const float RL10 = 50;
void setup() {
pinMode(led, OUTPUT);
pinMode(lightSensor1Pin, INPUT);
pinMode(lightSensor2Pin, INPUT);
myservo1.attach(servo1Pin);
myservo2.attach(servo2Pin);
myservo1.write(currentAngle);
myservo2.write(currentAngle);
display.begin( SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
display.display();
delay(1000);
}
void loop() {
int lightLevel1 = analogRead(lightSensor1Pin);
float voltage1 = lightLevel1 / 1024. * 5;
float resistance1 = 2000 * voltage1 / (1 - voltage1 / 5);
float lux1 = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance1, (1 / GAMMA));
int targetAngle1 = map(lux1, 0, 1023, minAngle, maxAngle);
myservo1.write(targetAngle1);
int lightLevel2 = analogRead(lightSensor2Pin);
float voltage2 = lightLevel2 / 1024. * 5;
float resistance2 = 2000 * voltage2 / (1 - voltage2 / 5);
float lux2 = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance2, (1 / GAMMA));
int targetAngle2 = map(lux2, 0, 1023, minAngle, maxAngle);
myservo2.write(targetAngle2);
display.clearDisplay();
display.setTextSize(1.8);
display.setTextColor(SSD1306_WHITE,SSD1306_BLACK);
display.setCursor(0,10);
display.print("Value1:"+String((int)lux1));
display.setCursor(0,20);
display.print("Value2:"+String((int)lux2));
display.setCursor(0,30);
if(lux1>maxValue&&lux2>maxValue){
digitalWrite(led,HIGH);
display.print("Open");
}else{
digitalWrite(led,LOW);
display.print("Close");
}
display.display();
}